Module: Pecorb
Constant Summary collapse
- VERSION =
"1.1.0"
Constants included from Console
Console::CSI, Console::DOWN, Console::LEFT, Console::RIGHT, Console::UP
Instance Method Summary collapse
Methods included from Console
backspace, black, blue, carriage_return, clear_screen, clear_to_eol, cyan, down, green, left, load_pos, magenta, output, print, puts, read_char, red, reset_color, right, save_pos, up, white, yellow
Instance Method Details
#prompt(items, prompt = "Select an item: ") ⇒ Object
15 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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/pecorb.rb', line 15 def prompt(items, prompt="Select an item: ") @displayed_items = @items = items @prompt = prompt while c = read_char case c when /[\r]/ break when /[]/ @input = "" break when /[]/ clear_screen when "" # Backspace key next if @input.empty? || @cursor <= 0 @input.slice!(@cursor-1) replace_input(@input) replace_items { filter_items(@items, @input) } backspace @cursor -= 1 when Console::LEFT next unless @cursor > 0 print c @cursor -= 1 when Console::RIGHT next unless @cursor < @input.length print c @cursor += 1 when Console::UP @selected = (@selected - 1) % @displayed_items.size replace_items { filter_items(@items, @input) } when Console::DOWN @selected = (@selected + 1) % @displayed_items.size replace_items { filter_items(@items, @input) } else @input.insert(@cursor, c) replace_input(@input) replace_items { filter_items(@items, @input) } print c @cursor += 1 end end move_cursor_from_start_to_end output @displayed_items[@selected] @displayed_items[@selected] end |