Class: Chamomile::Table
- Inherits:
-
Object
- Object
- Chamomile::Table
- Defined in:
- lib/chamomile/components/table.rb,
lib/chamomile/components/table/key_map.rb
Overview
Tabular data display with selection, scrolling, and focus gating.
Defined Under Namespace
Classes: Column, ColumnBuilder
Constant Summary collapse
- DEFAULT_KEY_MAP =
KeyBinding.normalize({ up: [[:up, []], ["k", []]], down: [[:down, []], ["j", []]], page_up: [[:page_up, []]], page_down: [[:page_down, []]], goto_top: [["g", []]], goto_bottom: [["G", [:shift]]], })
Instance Attribute Summary collapse
-
#columns ⇒ Object
Returns the value of attribute columns.
-
#cursor ⇒ Object
Returns the value of attribute cursor.
-
#height ⇒ Object
Returns the value of attribute height.
-
#key_map ⇒ Object
Returns the value of attribute key_map.
-
#rows ⇒ Object
Returns the value of attribute rows.
Instance Method Summary collapse
- #blur ⇒ Object
- #focus ⇒ Object
- #focused? ⇒ Boolean
- #goto_bottom ⇒ Object
- #goto_top ⇒ Object
-
#handle(msg) ⇒ Object
(also: #update)
Handle an incoming event.
-
#initialize(columns: [], rows: [], height: 10, key_map: DEFAULT_KEY_MAP, &block) ⇒ Table
constructor
Accepts columns as keyword arg or via block DSL.
- #move_down(n = 1) ⇒ Object
- #move_up(n = 1) ⇒ Object
- #selected_row ⇒ Object
- #view ⇒ Object
Constructor Details
#initialize(columns: [], rows: [], height: 10, key_map: DEFAULT_KEY_MAP, &block) ⇒ Table
Accepts columns as keyword arg or via block DSL.
# Keyword form (original)
Table.new(columns: [Column.new(title: "Name", width: 20)], rows: rows)
# Block DSL form (new)
Table.new(rows: rows) do |t|
t.column "Name", width: 20
t.column "Size", width: 10
end
# Hash form (new)
Table.new(columns: [{ title: "Name", width: 20 }], rows: rows)
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/chamomile/components/table.rb', line 24 def initialize(columns: [], rows: [], height: 10, key_map: DEFAULT_KEY_MAP, &block) @columns = normalize_columns(columns) @rows = rows @height = height @key_map = key_map @cursor = 0 @offset = 0 @focused = false return unless block builder = ColumnBuilder.new block.call(builder) @columns = builder.columns unless builder.columns.empty? end |
Instance Attribute Details
#columns ⇒ Object
Returns the value of attribute columns.
8 9 10 |
# File 'lib/chamomile/components/table.rb', line 8 def columns @columns end |
#cursor ⇒ Object
Returns the value of attribute cursor.
9 10 11 |
# File 'lib/chamomile/components/table.rb', line 9 def cursor @cursor end |
#height ⇒ Object
Returns the value of attribute height.
8 9 10 |
# File 'lib/chamomile/components/table.rb', line 8 def height @height end |
#key_map ⇒ Object
Returns the value of attribute key_map.
8 9 10 |
# File 'lib/chamomile/components/table.rb', line 8 def key_map @key_map end |
#rows ⇒ Object
Returns the value of attribute rows.
9 10 11 |
# File 'lib/chamomile/components/table.rb', line 9 def rows @rows end |
Instance Method Details
#blur ⇒ Object
82 83 84 85 |
# File 'lib/chamomile/components/table.rb', line 82 def blur @focused = false self end |
#focus ⇒ Object
77 78 79 80 |
# File 'lib/chamomile/components/table.rb', line 77 def focus @focused = true self end |
#focused? ⇒ Boolean
87 88 89 |
# File 'lib/chamomile/components/table.rb', line 87 def focused? @focused end |
#goto_bottom ⇒ Object
67 68 69 70 |
# File 'lib/chamomile/components/table.rb', line 67 def goto_bottom @cursor = [row_count - 1, 0].max clamp_offset end |
#goto_top ⇒ Object
62 63 64 65 |
# File 'lib/chamomile/components/table.rb', line 62 def goto_top @cursor = 0 clamp_offset end |
#handle(msg) ⇒ Object Also known as: update
Handle an incoming event.
92 93 94 95 96 97 98 99 100 101 |
# File 'lib/chamomile/components/table.rb', line 92 def handle(msg) return unless @focused case msg when Chamomile::KeyEvent handle_key(msg) end nil end |
#move_down(n = 1) ⇒ Object
57 58 59 60 |
# File 'lib/chamomile/components/table.rb', line 57 def move_down(n = 1) @cursor = (@cursor + n).clamp(0, [row_count - 1, 0].max) clamp_offset end |
#move_up(n = 1) ⇒ Object
52 53 54 55 |
# File 'lib/chamomile/components/table.rb', line 52 def move_up(n = 1) @cursor = (@cursor - n).clamp(0, [row_count - 1, 0].max) clamp_offset end |
#selected_row ⇒ Object
46 47 48 49 50 |
# File 'lib/chamomile/components/table.rb', line 46 def selected_row return nil if @rows.empty? @rows[@cursor] end |
#view ⇒ Object
106 107 108 109 110 111 112 |
# File 'lib/chamomile/components/table.rb', line 106 def view header = render_header separator = render_separator body = render_body [header, separator, body].compact.join("\n") end |