Class: RubyCurses::DirectoryList
- Inherits:
-
Listbox
- Object
- Listbox
- RubyCurses::DirectoryList
- Includes:
- ViEditable
- Defined in:
- lib/rbcurse/experimental/widgets/directorylist.rb
Overview
Display a directory listing, allowing user to drill down on pressing Enter on a directory, or sort when pressing enter on header row.
Constant Summary collapse
- GIGA_SIZE =
1073741824.0
- MEGA_SIZE =
1048576.0
- KILO_SIZE =
1024.0
Instance Method Summary collapse
-
#[](index) ⇒ Object
2010-01-23 22:41.
- #___create_default_cell_renderer ⇒ Object
- #_get_word_under_cursor(line = @_header, pos = @curpos) ⇒ Object
- #addcol(num) ⇒ Object
- #ask_select ⇒ Object
- #ask_unselect ⇒ Object
-
#convert_value_to_text(file, crow) ⇒ Object
called by parent’s repaint.
-
#current_path(*val) ⇒ Object
changing the current path, refreshes files.
- #cursor_backward ⇒ Object
- #cursor_forward ⇒ Object
- #date_format(t) ⇒ Object
-
#edit_line(lineno = @current_index) ⇒ Object
modify a file name.
-
#end_of_line ⇒ Object
this does not work, since the value in list is not the printed value.
-
#fire_action_event ⇒ Object
on pressing Enter, execute this action.
- #init_vars ⇒ Object
-
#initialize(form, config = {}, &block) ⇒ DirectoryList
constructor
A new instance of DirectoryList.
- #invert_selection ⇒ Object
- #longest_line ⇒ Object
- #longest_line=(l) ⇒ Object
-
#populate(path) ⇒ Object
populate the list with file names.
-
#readable_file_size(size, precision) ⇒ Object
Return the file size with a readable style.
- #select_all ⇒ Object
-
#select_values(values) ⇒ Object
selects all rows with the values given, leaving existing selections intact.
-
#set_form_col(col1 = @curpos) ⇒ Object
set cursor column position if i set col1 to @curpos, i can move around left right if key mapped.
-
#sort_by(key, reverse = false) ⇒ Object
sorts entries by various parameters.
- #start_of_line ⇒ Object
-
#unselect_values(values) ⇒ Object
unselects all rows with the values given, leaving all other rows intact.
Constructor Details
#initialize(form, config = {}, &block) ⇒ DirectoryList
Returns a new instance of DirectoryList.
17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/rbcurse/experimental/widgets/directorylist.rb', line 17 def initialize form, config={}, &block @hide_dot_files = false @hide_others = false @curpos = 0 super #@current_path ||= Dir.getwd # setting it causes selection not to fire if same one # is selected first @_header_array = [ "Attr", "Size", "Modified" , "Name" , "<Order_by_Extension>" ] @_header = " %s %8s %19s %s %s " % @_header_array end |
Instance Method Details
#[](index) ⇒ Object
2010-01-23 22:41
311 312 313 |
# File 'lib/rbcurse/experimental/widgets/directorylist.rb', line 311 def [](index) @entries[index] end |
#___create_default_cell_renderer ⇒ Object
115 116 117 |
# File 'lib/rbcurse/experimental/widgets/directorylist.rb', line 115 def ___create_default_cell_renderer cell_renderer( RubyCurses::DirectoryListCellRenderer.new "", {:color=>@color, :bgcolor=>@bgcolor, :parent => self, :display_length=> @width-2-@left_margin}) end |
#_get_word_under_cursor(line = @_header, pos = @curpos) ⇒ Object
118 119 120 121 122 123 124 |
# File 'lib/rbcurse/experimental/widgets/directorylist.rb', line 118 def _get_word_under_cursor line=@_header, pos=@curpos finish = line.index(" ", pos) start = line.rindex(" ",pos) finish = -1 if finish.nil? start = 0 if start.nil? return line[start..finish] end |
#addcol(num) ⇒ Object
251 252 253 254 255 256 257 258 259 |
# File 'lib/rbcurse/experimental/widgets/directorylist.rb', line 251 def addcol num #@repaint_required = true @repaint_footer_required = true # 2010-01-23 22:41 if @form @form.addcol num else @parent_component.form.addcol num end end |
#ask_select ⇒ Object
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 |
# File 'lib/rbcurse/experimental/widgets/directorylist.rb', line 341 def ask_select prompt = "Select Files: " maxlen = 80 config={} config[:default] = @file_pattern if @file_pattern ret, str = rbgetstr(@form.window, $error_message_row, $error_message_col, prompt, maxlen, config) return if ret != 0 @file_pattern = str # 2011-09-19 doesn't work if pwd changed if Dir.pwd != @current_path Dir.chdir @current_path end values = Dir.glob(str) $log.debug "ask select dir.glob got #{values} " select_values values unless values.empty? @repaint_required = true end |
#ask_unselect ⇒ Object
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 |
# File 'lib/rbcurse/experimental/widgets/directorylist.rb', line 358 def ask_unselect prompt = "Unselect Files: " maxlen = 80 config={} config[:default] = @file_pattern if @file_pattern ret, str = rbgetstr(@form.window, $error_message_row, $error_message_col, prompt, maxlen, config) return if ret != 0 @file_pattern = str if Dir.pwd != @current_path Dir.chdir @current_path end values = Dir.glob(str) unselect_values values unless values.empty? @repaint_required = true end |
#convert_value_to_text(file, crow) ⇒ Object
called by parent’s repaint
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/rbcurse/experimental/widgets/directorylist.rb', line 92 def convert_value_to_text file, crow if @short file else if crow == @_header_row_index return file else # OUCH i don't know if its a header !! fullname = File.join(@current_path, file) fname = file stat = File::Stat.new fullname time = stat.mtime.to_s[0..18] attr = stat.directory? ? "d" : "-" attr << (stat.writable? ? "w" : "-") attr << (stat.readable? ? "r" : "-") attr << (stat.executable? ? "x" : "-") fname << "/" if stat.directory? && fname[-1] != "/" value = " %s %8d %s %s" % [attr, stat.size, time, fname] return value end end end |
#current_path(*val) ⇒ Object
changing the current path, refreshes files
54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/rbcurse/experimental/widgets/directorylist.rb', line 54 def current_path(*val) if val.empty? return @current_path else raise ArgumentError, "current_path should be a directory:#{val[0]}." unless File.directory? val[0] oldvalue = @current_path if oldvalue != val[0] @current_path = val[0] populate @current_path fire_property_change(:current_path, oldvalue, @current_path) end end self end |
#cursor_backward ⇒ Object
280 281 282 283 284 285 286 287 288 289 290 291 292 |
# File 'lib/rbcurse/experimental/widgets/directorylist.rb', line 280 def cursor_backward repeatm { if @curpos > 0 @curpos -= 1 set_form_col #addcol -1 elsif @pcol > 0 @pcol -= 1 end } #@repaint_required = true @repaint_footer_required = true # 2010-01-23 22:41 end |
#cursor_forward ⇒ Object
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 |
# File 'lib/rbcurse/experimental/widgets/directorylist.rb', line 295 def cursor_forward @curpos ||= 0 maxlen = @maxlen || @width-2 repeatm { if @curpos < @width and @curpos < maxlen-1 # else it will do out of box @curpos += 1 addcol 1 else #@pcol += 1 if @pcol <= @buffer.length # buffer not eixstent FIXME end } set_form_col #@repaint_required = true @repaint_footer_required = true # 2010-01-23 22:41 end |
#date_format(t) ⇒ Object
208 209 210 |
# File 'lib/rbcurse/experimental/widgets/directorylist.rb', line 208 def date_format t t.strftime "%Y/%m/%d" end |
#edit_line(lineno = @current_index) ⇒ Object
modify a file name. call using ‘C’
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 |
# File 'lib/rbcurse/experimental/widgets/directorylist.rb', line 324 def edit_line lineno=@current_index line = @list[lineno] fullname = File.join(@current_path, line) prompt = "Rename file [#{line}] to: " maxlen = 80 config={}; oldline = line.dup config[:default] = line ret, str = rbgetstr(@form.window, $error_message_row, $error_message_col, prompt, maxlen, config) $log.debug " rbgetstr returned #{ret} , #{str} " return if ret != 0 @list[lineno].replace(str) FileUtils.mv fullname, File.join(@current_path, str) fire_handler :CHANGE, InputDataEvent.new(0,oldline.length, self, :DELETE_LINE, lineno, oldline) fire_handler :CHANGE, InputDataEvent.new(0,str.length, self, :INSERT_LINE, lineno, str) @repaint_required = true end |
#end_of_line ⇒ Object
this does not work, since the value in list is not the printed value
275 276 277 278 |
# File 'lib/rbcurse/experimental/widgets/directorylist.rb', line 275 def end_of_line blen = current_value.rstrip.length set_form_col blen end |
#fire_action_event ⇒ Object
on pressing Enter, execute this action. Here we take the current file and if its a directory, we step into it.
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 |
# File 'lib/rbcurse/experimental/widgets/directorylist.rb', line 214 def fire_action_event if @_non_data_indices.include? @current_index Ncurses.error return end $log.debug "inside fire_action_event of directorylist #{@current_index} #{toprow} " if @_header_row_index == @current_index # user hit enter on the header row. we determine column and sort. header = _get_word_under_cursor #@reverse = false if header == @_last_header_sorted @reverse = !@reverse # clicking 2 times won't reverse again else @reverse = false end sort_by header.strip, @reverse # this next line will keep the cursor on header after sorting # earlier cursor would appear in header but selection would be data row @current_index = @_header_row_index # FFI 2011-09-16 keeping cursor synched with selection @_last_header_sorted = header set_form_row return end value = current_value value = value.split.last if value == ".." _path = File.dirname(@current_path) else _path = File.join(@current_path, value) end if File.directory? _path populate _path end $log.debug "after fire_action_event of directorylist #{@current_index} #{toprow} " super end |
#init_vars ⇒ Object
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 |
# File 'lib/rbcurse/experimental/widgets/directorylist.rb', line 28 def init_vars # which rows are not data, thus don't fire, or give error @_non_data_indices = [] @_header_row_index = 0 @_first_data_index = 1 @one_key_selection = false # this allows us to map keys to methods vieditable_init_listbox undom = SimpleUndo.new self bind_key(?\M-h, :scroll_left) bind_key(?\M-l, :scroll_right) bind_key(KEY_RIGHT, :cursor_forward) bind_key(KEY_LEFT, :cursor_backward) bind_key(?$, :end_of_line) bind_key(?\C-e, :end_of_line) bind_key(?\C-a, :start_of_line) bind_key(?a, :select_all) bind_key(?*, :invert_selection) bind_key(?u, :clear_selection) bind_key(?+, :ask_select) bind_key(?-, :ask_unselect) bind_key(?I) { @hide_dot_files = !@hide_dot_files; prune_entries; } #bind_key(?v, :view_current) # does not respond to ??? XXX bind_key(?v) {view_current} super end |
#invert_selection ⇒ Object
377 378 379 |
# File 'lib/rbcurse/experimental/widgets/directorylist.rb', line 377 def invert_selection 1.upto(row_count()){|i| toggle_row_selection i } end |
#longest_line ⇒ Object
319 320 321 |
# File 'lib/rbcurse/experimental/widgets/directorylist.rb', line 319 def longest_line @longest_line end |
#longest_line=(l) ⇒ Object
316 317 318 |
# File 'lib/rbcurse/experimental/widgets/directorylist.rb', line 316 def longest_line=(l) @longest_line = l end |
#populate(path) ⇒ Object
populate the list with file names
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/rbcurse/experimental/widgets/directorylist.rb', line 71 def populate path case path when String @current_path = path @entries = Dir.new(path).entries @entries.delete(".") # isn;t it a bit late here. it needs to happen to what's already there @entries.delete_if {|x| x =~ /^\./} if @hide_dot_files @entries.delete_if {|x| x =~ /\.bak$/ || x=~/\.swp$/} if @hide_others when Array path = @current_path # we've been passed @entries so we don't need to put it again ??? XXX end # TODO K M etc list @entries @list.insert 0, @_header @title = @current_path @current_index = @_first_data_index set_form_row if @form end |
#readable_file_size(size, precision) ⇒ Object
Return the file size with a readable style.
199 200 201 202 203 204 205 206 207 |
# File 'lib/rbcurse/experimental/widgets/directorylist.rb', line 199 def readable_file_size(size, precision) case #when size == 1 : "1 B" when size < KILO_SIZE then "%d B" % size when size < MEGA_SIZE then "%.#{precision}f K" % (size / KILO_SIZE) when size < GIGA_SIZE then "%.#{precision}f M" % (size / MEGA_SIZE) else "%.#{precision}f G" % (size / GIGA_SIZE) end end |
#select_all ⇒ Object
373 374 375 376 |
# File 'lib/rbcurse/experimental/widgets/directorylist.rb', line 373 def select_all # don't select header row add_row_selection_interval 1, row_count() end |
#select_values(values) ⇒ Object
selects all rows with the values given, leaving existing selections intact
382 383 384 385 386 387 388 |
# File 'lib/rbcurse/experimental/widgets/directorylist.rb', line 382 def select_values values return unless values values.each do |val| row = @list.index val add_row_selection_interval row, row unless row.nil? end end |
#set_form_col(col1 = @curpos) ⇒ Object
set cursor column position if i set col1 to @curpos, i can move around left right if key mapped
262 263 264 265 266 267 268 |
# File 'lib/rbcurse/experimental/widgets/directorylist.rb', line 262 def set_form_col col1=@curpos col1 ||= 0 @cols_panned ||= 0 # RFED16 2010-02-17 23:40 win_col = 0 # 2010-02-17 23:19 RFED16 col2 = win_col + @col + @col_offset + col1 + @cols_panned + @left_margin setrowcol nil, col2 # 2010-02-17 23:19 RFED16 end |
#sort_by(key, reverse = false) ⇒ Object
sorts entries by various parameters. Not optimal since it creates file stat objects each time rather than cacheing, but this is a demo of widgets not a real directory lister!
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 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 |
# File 'lib/rbcurse/experimental/widgets/directorylist.rb', line 128 def sort_by key, reverse=false # remove parent before sorting, keep at top first = @entries.delete_at(0) if @entries[0]==".." #key ||= @sort_key sort_keys = { 'Name' => :name, 'Modified' => :mtime, "Size" => :size, "<Order_by_Extension>" => :ext, 'Attr' => :attr, "Accessed" => :atime } key = sort_keys[key] if sort_keys.has_key? key #if key == @sort_key #reverse = true #end cdir=@current_path+"/" case key when :size @entries.sort! {|x,y| xs = File.stat(cdir+x); ys = File.stat(cdir+y); if reverse xs.size <=> ys.size else ys.size <=> xs.size end } when :mtime @entries.sort! {|x,y| xs = File.stat(cdir+x); ys = File.stat(cdir+y); if reverse xs.mtime <=> ys.mtime else ys.mtime <=> xs.mtime end } when :atime @entries.sort! {|x,y| xs = File.stat(cdir+x); ys = File.stat(cdir+y); if reverse xs.atime <=> ys.atime else ys.atime <=> xs.atime end } when :name @entries.sort! {|x,y| x <=> y if reverse x <=> y else y <=> x end } when :ext @entries.sort! {|x,y| if reverse File.extname(cdir+x) <=> File.extname(cdir+y) else File.extname(cdir+y) <=> File.extname(cdir+x) end } when :attr @entries.sort! {|x,y| xs = File.stat(cdir+x); ys = File.stat(cdir+y); x = xs.directory? ? "d" : "D" y = ys.directory? ? "d" : "D" if reverse x <=> y else y <=> x end } end @sort_key = key @entries.insert 0, first unless first.nil? # keep parent on top populate @entries end |
#start_of_line ⇒ Object
269 270 271 272 273 |
# File 'lib/rbcurse/experimental/widgets/directorylist.rb', line 269 def start_of_line @repaint_required = true if @pcol > 0 # tried other things but did not work set_form_col 0 @pcol = 0 end |
#unselect_values(values) ⇒ Object
unselects all rows with the values given, leaving all other rows intact
390 391 392 393 394 395 396 |
# File 'lib/rbcurse/experimental/widgets/directorylist.rb', line 390 def unselect_values values return unless values values.each do |val| row = @list.index val remove_row_selection_interval row, row unless row.nil? end end |