Class: Pecorb::List
Constant Summary
Constants included from Console
Console::CSI, Console::DOWN, Console::LEFT, Console::RIGHT, Console::UP
Instance Method Summary collapse
-
#initialize(items, opts = {}) ⇒ List
constructor
A new instance of List.
- #prompt ⇒ Object
Methods included from Console
#backspace, #black, #blue, #carriage_return, #clear_screen, #clear_to_eol, #clear_to_eos, #cyan, #down, #green, #left, #load_pos, #magenta, #print, #puts, #read_char, #red, #reset_color, #right, #save_pos, #up, #white, #yellow
Constructor Details
#initialize(items, opts = {}) ⇒ List
Returns a new instance of List.
9 10 11 12 13 14 15 |
# File 'lib/pecorb/list.rb', line 9 def initialize(items, opts={}) raise "Items must be enumerable!" unless items.is_a? Enumerable @prompt = opts.fetch(:prompt, "Select an item: ") @pager = Pager.new items, IO.console.winsize.first - 2 @cursor = 0 @filter_text = "" end |
Instance Method Details
#prompt ⇒ Object
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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/pecorb/list.rb', line 17 def prompt init_ui while c = read_char case c when "", "\r" break when "", "" carriage_return clear_to_eos exit 0 when "" clear_screen print @prompt @cursor.times { right } update_ui when "" # Backspace key next if @filter_text.empty? || @cursor <= 0 @filter_text.slice!(@cursor - 1) update_ui backspace @cursor -= 1 when Console::LEFT next unless @cursor > 0 print c @cursor -= 1 when Console::RIGHT next unless @cursor < @filter_text.length print c @cursor += 1 when Console::UP, "" @pager.up update_ui when Console::DOWN, "\n" # CTRL-J enters a linefeed char in bash @pager.down update_ui else @filter_text.insert(@cursor, c) update_ui print c @cursor += 1 end end backspace(@cursor) clear_to_eos cyan puts @pager.selected_item reset_color @pager.selected_item end |