Class: Liberator::View
- Inherits:
-
Object
- Object
- Liberator::View
- Defined in:
- lib/liberator/view.rb
Constant Summary collapse
- GIGABYTE =
1073741824
- MEGABYTE =
1048576
- KILOBYTE =
1024
Instance Method Summary collapse
- #capture_keystroke ⇒ Object
- #close ⇒ Object
- #confirm_delete ⇒ Object
- #formatted_size(size) ⇒ Object
-
#initialize ⇒ View
constructor
A new instance of View.
- #refresh(directory, entries, selected_index) ⇒ Object
- #update_status_bar(content) ⇒ Object
Constructor Details
#initialize ⇒ View
Returns a new instance of View.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/liberator/view.rb', line 7 def initialize # Initialize curses view. Curses.init_screen Curses.noecho Curses.curs_set 0 Curses.cbreak @height = Curses.stdscr.maxy @width = Curses.stdscr.maxx @entry_window = Curses::Window.new @height-1, @width, 0, 0 @status_bar = Curses::Window.new 1, @width, @height-1, 0 @status_bar.standout @scroll_offset = 0 end |
Instance Method Details
#capture_keystroke ⇒ Object
80 81 82 |
# File 'lib/liberator/view.rb', line 80 def capture_keystroke @entry_window.getch end |
#close ⇒ Object
89 90 91 92 |
# File 'lib/liberator/view.rb', line 89 def close # Clean up curses view. Curses.close_screen end |
#confirm_delete ⇒ Object
84 85 86 87 |
# File 'lib/liberator/view.rb', line 84 def confirm_delete "Really delete? (y to confirm)" capture_keystroke == 'y' end |
#formatted_size(size) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/liberator/view.rb', line 66 def formatted_size(size) return '-' if size.nil? if size > GIGABYTE "#{size / GIGABYTE} GB" elsif size > MEGABYTE "#{size / MEGABYTE} MB" elsif size > KILOBYTE "#{size / KILOBYTE} KB" else "#{size} bytes" end end |
#refresh(directory, entries, selected_index) ⇒ Object
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 |
# File 'lib/liberator/view.rb', line 24 def refresh(directory, entries, selected_index) height = @entry_window.maxy # Clear the screen manually, since clear function causes blinking. @entry_window.setpos 0, 0 height.times { @entry_window.deleteln } # Figure out what to draw based on the selected entry and height of the window. if selected_index < height-1 visible_range = (0...height) elsif selected_index == entries.size-1 # last item selected visible_range = (entries.size-height..entries.size) else visible_range = (selected_index-height+2..selected_index+1) end entries[visible_range].each_with_index do |entry, index| # Turn on highlighting, if this entry is selected. @entry_window.standout if index+visible_range.begin == selected_index # Get the file/directory name, without its full path. name = entry[:path][entry[:path].rindex('/')+1..-1] name += '/' if File.directory? entry[:path] # Add the row to the window, appending a right-justified human-readable size. @entry_window << name + formatted_size(entry[:size]).rjust(@entry_window.maxx - name.length) # Stop highlighting. @entry_window.standend end directory end |
#update_status_bar(content) ⇒ Object
58 59 60 61 62 63 64 |
# File 'lib/liberator/view.rb', line 58 def (content) @status_bar.setpos 0, 0 formatted_content = content.ljust @width @status_bar << formatted_content @status_bar.refresh end |