Class: FileManager

Inherits:
Object
  • Object
show all
Defined in:
lib/vimamsa/file_manager.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeFileManager

Returns a new instance of FileManager.



4
5
6
# File 'lib/vimamsa/file_manager.rb', line 4

def initialize()
  @buf = nil
end

Class Method Details

.chdir_parentObject



8
9
10
# File 'lib/vimamsa/file_manager.rb', line 8

def self.chdir_parent()
  @@cur.chdir_parent
end

.curObject



12
13
14
# File 'lib/vimamsa/file_manager.rb', line 12

def self.cur()
  return @@cur
end

.initObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/vimamsa/file_manager.rb', line 16

def self.init()
  reg_act(:start_file_selector, proc { FileManager.new.run; $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

  # 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_parentObject



44
45
46
# File 'lib/vimamsa/file_manager.rb', line 44

def chdir_parent
  dir_to_buf(fullp(".."))
end

#dir_to_buf(dirpath, b = nil) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
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 66

def dir_to_buf(dirpath, b = nil)
  # File.stat("testfile").mtime

  vma.buffers.last_dir = dirpath
  dirpath = File.expand_path(dirpath)
  @header = []
  @header << "#{dirpath}"
  @header << "=" * 40
  @ld = dirpath
  @dlist = Dir.children(@ld).sort
  @cdirs = []
  @cfiles = []
  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"
  for f in @cfiles
    s << "#{f[0]}\n"
    # s << @cfiles.join("\n")
  end

  if @buf.nil?
    @buf = create_new_file(nil, s)
    @buf.module = self
    @buf.active_kbd_mode = :file_exp
  else
    @buf.set_content(s)
  end
  @buf.set_line_and_column_pos(@header.size, 0)
end

#fullp(fn) ⇒ Object



123
124
125
# File 'lib/vimamsa/file_manager.rb', line 123

def fullp(fn)
  "#{@ld}/#{fn}"
end

#quitObject



144
145
146
# File 'lib/vimamsa/file_manager.rb', line 144

def quit
  @buf.close
end

#runObject



48
49
50
51
52
53
54
# File 'lib/vimamsa/file_manager.rb', line 48

def run
  @@cur = self
  ld = buflist.get_last_dir
  dir_to_buf(ld)
  # debug "ld=#{ld}"
  # dlist = Dir["#{ld}/*"]
end

#select_lineObject



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/vimamsa/file_manager.rb', line 127

def select_line
  return if @buf.lpos < @header.size
  # debug "def select_line"
  fn = fullp(@buf.get_current_line[0..-2])
  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)
  else
    open_with_default_program(fn)
  end
end

#sort_fnameObject



61
62
63
64
# File 'lib/vimamsa/file_manager.rb', line 61

def sort_fname
  @sort_by = :name
  dir_to_buf(@ld)
end

#sort_mtimeObject



56
57
58
59
# File 'lib/vimamsa/file_manager.rb', line 56

def sort_mtime
  @sort_by = :mtime
  dir_to_buf(@ld)
end