Class: FileManager
- Inherits:
-
Object
- Object
- FileManager
- Defined in:
- lib/vimamsa/file_manager.rb
Class Method Summary collapse
Instance Method Summary collapse
- #chdir_parent ⇒ Object
- #copy_file ⇒ Object
- #cur_file ⇒ Object
- #cut_file ⇒ Object
- #delete_file ⇒ Object
- #delete_file_confirmed(*args) ⇒ Object
- #dir_to_buf(dirpath, b = nil) ⇒ Object
- #fullp(fn) ⇒ Object
-
#initialize ⇒ FileManager
constructor
A new instance of FileManager.
- #paste_files ⇒ Object
- #quit ⇒ Object
- #refresh ⇒ Object
- #run ⇒ Object
- #select_line ⇒ Object
- #sort_fname ⇒ Object
- #sort_mtime ⇒ Object
Constructor Details
#initialize ⇒ FileManager
Returns a new instance of FileManager.
6 7 8 9 10 |
# File 'lib/vimamsa/file_manager.rb', line 6 def initialize() @buf = nil @cut_files = [] @copied_files = [] end |
Class Method Details
.chdir_parent ⇒ Object
12 13 14 |
# File 'lib/vimamsa/file_manager.rb', line 12 def self.chdir_parent() @@cur.chdir_parent end |
.cur ⇒ Object
16 17 18 |
# File 'lib/vimamsa/file_manager.rb', line 16 def self.cur() return @@cur end |
.init ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/vimamsa/file_manager.rb', line 20 def self.init() reg_act(:start_file_selector, proc { FileManager.new.run; vma.kbd.set_mode(:file_exp); }, "File selector") reg_act(:fexp_chdir_parent, proc { FileManager.chdir_parent }, "File selector") reg_act(:fexp_select, proc { buf.module.select_line }, "") reg_act(:fexp_sort_mtime, proc { FileManager.cur.sort_mtime }, "Sort based on time") reg_act(:fexp_sort_fname, proc { FileManager.cur.sort_fname }, "Sort based on file name") bindkey "C , j f", :start_file_selector bindkey "C , f", :start_file_selector # bindkey "C o", :delete_state vma.kbd.add_minor_mode("fexp", :file_exp, :command) bindkey "fexp o m", :fexp_sort_mtime bindkey "fexp o f", :fexp_sort_fname # These are not yet safe to use if cnf.fexp.experimental? reg_act(:fexp_cut_file, proc { FileManager.cur.cut_file }, "Cut file (to paste elsewhere)") reg_act(:fexp_copy_file, proc { FileManager.cur.copy_file }, "Copy file (to paste elsewhere)") reg_act(:fexp_delete_file, proc { FileManager.cur.delete_file }, "Delete current file") reg_act(:fexp_paste_files, proc { FileManager.cur.paste_files }, "Move previously cut files here") bindkey "fexp d d", :fexp_cut_file bindkey "fexp y y", :fexp_copy_file bindkey "fexp d D", :fexp_delete_file bindkey "fexp p p", :fexp_paste_files end # bindkey "fexp l", [:fexp_right, proc { debug "==fexp_right==" }, ""] bindkey "fexp h", :fexp_chdir_parent bindkey "fexp esc", [:fexp_quit, proc { FileManager.cur.quit }, ""] bindkey "fexp enter", :fexp_select bindkey "fexp l", :fexp_select @sort_by = :name end |
Instance Method Details
#chdir_parent ⇒ Object
61 62 63 |
# File 'lib/vimamsa/file_manager.rb', line 61 def chdir_parent dir_to_buf(fullp("..")) end |
#copy_file ⇒ Object
130 131 132 133 134 135 |
# File 'lib/vimamsa/file_manager.rb', line 130 def copy_file fn = cur_file debug "COPY FILE #{fn}", 2 @copied_files << fn @cut_files = [] end |
#cur_file ⇒ Object
248 249 250 251 252 |
# File 'lib/vimamsa/file_manager.rb', line 248 def cur_file return nil if @buf.lpos < @header.size fn = fullp(@buf.get_current_line[0..-2]) return fn end |
#cut_file ⇒ Object
123 124 125 126 127 128 |
# File 'lib/vimamsa/file_manager.rb', line 123 def cut_file fn = cur_file debug "CUT FILE #{fn}", 2 @cut_files << fn @copied_files = [] end |
#delete_file ⇒ Object
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/vimamsa/file_manager.rb', line 146 def delete_file fn = cur_file if File.file?(fn) @file_to_delete = fn #TODO: set as parameter to confirm_box Gui.confirm("Delete the file? \r #{fn}", self.method("delete_file_confirmed")) elsif File.directory?(fn) @file_to_delete = fn #TODO: set as parameter to confirm_box Gui.confirm("Delete the directory? \r #{fn}", self.method("delete_file_confirmed")) else "Can't delete #{fn}" end # TODO: FileUtils.remove_dir #TODO: end |
#delete_file_confirmed(*args) ⇒ Object
137 138 139 140 141 142 143 144 |
# File 'lib/vimamsa/file_manager.rb', line 137 def delete_file_confirmed(*args) debug args, 2 fn = @file_to_delete "Deleting file #{fn}" # FileUtils.remove_file(fn) FileUtils.remove_entry_secure(fn, force = false) refresh end |
#dir_to_buf(dirpath, b = nil) ⇒ Object
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 |
# File 'lib/vimamsa/file_manager.rb', line 165 def dir_to_buf(dirpath, b = nil) # File.stat("testfile").mtime debug "last file: #{vma.buffers.last_file}", 2 lastf = vma.buffers.last_file jumpto = nil if File.dirname(lastf) == dirpath jumpto = File.basename(lastf) end vma.buffers.last_dir = dirpath dirpath = File.(dirpath) @header = [] @header << "#{dirpath}" @header << "=" * 40 @ld = dirpath # Path to current directory @dlist = Dir.children(@ld).sort @cdirs = [] # Dirs in current directory @cfiles = [] # Files in current directory for x in @dlist fpath = fullp(x) ok = true begin fstat = File.stat(fpath) rescue Errno::ENOENT # Broken link or something next end next if x[0] == "." if File.directory?(fpath) # if f.directory?(fpath) @cdirs << x else @cfiles << [x, fstat] end end @cfiles.sort_by! { |x| x[1].mtime }.reverse! if @sort_by == :mtime @cfiles.sort_by! { |x| x[1].size }.reverse! if @sort_by == :size @cfiles.sort_by! { |x| x[0] } if @sort_by == :name s = "" s << @header.join("\n") s << "\n" s << "..\n" s << @cdirs.join("\n") s << "\n" s << "\n" jumppos = nil for f in @cfiles if f[0] == jumpto jumppos = s.size end s << "#{f[0]}\n" # s << @cfiles.join("\n") end if @buf.nil? @buf = create_new_buffer(s, "filemgr") @buf.default_mode = :file_exp @buf.module = self @buf.active_kbd_mode = :file_exp else @buf.set_content(s) end if jumppos @buf.set_pos(jumppos) else @buf.set_line_and_column_pos(@header.size, 0) end end |
#fullp(fn) ⇒ Object
236 237 238 |
# File 'lib/vimamsa/file_manager.rb', line 236 def fullp(fn) "#{@ld}/#{fn}" end |
#paste_files ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/vimamsa/file_manager.rb', line 83 def paste_files if !@cut_files.empty? "MOVE FILES #{@cut_files.join(",")} TO #{@ld} " # Thread.new { for fn in @cut_files FileUtils.move(fn, @ld) debug "FileUtils.move(#{fn}, #{@ld})" end elsif !@copied_files.empty? for fn in @copied_files bn = File.basename(fn) bnwe = File.basename(fn, ".*") ext = File.extname(fn) dst = "#{@ld}/#{bn}" break if !File.exist?(fn) if dst == fn #or File.exist?(dst) i = 1 exists = true while File.exist?(dst) dst = "#{@ld}/#{bnwe}_copy#{i}#{ext}" i += 1 end elsif File.exist?(dst) ("File #{dst} already exists") break #TODO: confirm if user wants to replace existing file end "FileUtils.copy_entry(#{fn}, #{dst})" FileUtils.copy_entry(fn, dst, preserve = false, dereference_root = false, remove_destination = false) end else "Nothing to paste, cut/copy some files first!" return end # } @cut_files = [] @copied_files = [] refresh end |
#quit ⇒ Object
276 277 278 |
# File 'lib/vimamsa/file_manager.rb', line 276 def quit @buf.close end |
#refresh ⇒ Object
240 241 242 243 244 245 246 |
# File 'lib/vimamsa/file_manager.rb', line 240 def refresh # TODO: only apply diff lpos = @buf.lpos # cpos = @buf.cpos dir_to_buf(@ld) @buf.set_line_and_column_pos(lpos, 0) end |
#run ⇒ Object
65 66 67 68 69 70 71 |
# File 'lib/vimamsa/file_manager.rb', line 65 def run @@cur = self ld = buflist.get_last_dir dir_to_buf(ld) # debug "ld=#{ld}" # dlist = Dir["#{ld}/*"] end |
#select_line ⇒ Object
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 |
# File 'lib/vimamsa/file_manager.rb', line 254 def select_line # return if @buf.lpos < @header.size # debug "def select_line" # fn = fullp(@buf.get_current_line[0..-2]) fn = cur_file return if fn.nil? if File.directory?(fn) debug "CHDIR: #{fn}" dir_to_buf(fn) # elsif vma.can_open_extension?(fn) # jump_to_file(fn) elsif file_is_text_file(fn) # bufs.close_current_buffer jump_to_file(fn) # vma.buffers.set_current_buffer(idx) vma.buffers.close_other_buffer(@buf.id) else open_with_default_program(fn) end end |
#sort_fname ⇒ Object
78 79 80 81 |
# File 'lib/vimamsa/file_manager.rb', line 78 def sort_fname @sort_by = :name dir_to_buf(@ld) end |
#sort_mtime ⇒ Object
73 74 75 76 |
# File 'lib/vimamsa/file_manager.rb', line 73 def sort_mtime @sort_by = :mtime dir_to_buf(@ld) end |