Class: TuiTui::Select
Overview
Scrollable list picker modal.
Constant Summary collapse
- MAX_ROWS =
12- MIN_INNER =
16- WHEEL =
3
Constants inherited from Modal
Instance Method Summary collapse
- #cursor ⇒ Object
- #draw(canvas, size) ⇒ Object
- #handle(key) ⇒ Object
-
#handle_mouse(event) ⇒ Object
Wheel moves the highlighted item; a click on an item picks it.
-
#initialize(title, items, default: 0, theme: Theme::DEFAULT) ⇒ Select
constructor
A new instance of Select.
Constructor Details
#initialize(title, items, default: 0, theme: Theme::DEFAULT) ⇒ Select
Returns a new instance of Select.
20 21 22 23 24 25 26 |
# File 'lib/tui_tui/select.rb', line 20 def initialize(title, items, default: 0, theme: Theme::DEFAULT) @title = DisplayText.new(title) @items = items.map { |item| DisplayText.new(item) } @list = ScrollList.new(@items.size) @list.go_to(default) @theme = theme end |
Instance Method Details
#cursor ⇒ Object
28 |
# File 'lib/tui_tui/select.rb', line 28 def cursor = @list.cursor |
#draw(canvas, size) ⇒ Object
58 59 60 61 62 63 64 65 66 |
# File 'lib/tui_tui/select.rb', line 58 def draw(canvas, size) rows = visible_rows(size) inner = [MIN_INNER, @title.width, *@items.map(&:width)].max rect, text_col = panel(canvas, inner: inner, body_rows: rows + 2) canvas.text(rect.row + 1, text_col, @title.truncate(inner), theme.title) draw_items(canvas, rect.row + 3, text_col, inner, rows) canvas end |
#handle(key) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/tui_tui/select.rb', line 30 def handle(key) case KeyIntent.for(key) when :up nudge(-1) when :down nudge(1) when :top nudge_to(0) when :bottom nudge_to(@list.last) when :cancel :cancel else @list.cursor if ["\r", " "].include?(key) end end |
#handle_mouse(event) ⇒ Object
Wheel moves the highlighted item; a click on an item picks it. Returns the chosen index on a click, otherwise nil (stay open).
49 50 51 52 53 54 55 56 |
# File 'lib/tui_tui/select.rb', line 49 def handle_mouse(event) case event.action when :wheel nudge(event. == :wheel_up ? -WHEEL : WHEEL) when :press click(event) end end |