Class: Thaum::Table

Inherits:
Object
  • Object
show all
Includes:
Sigil
Defined in:
lib/thaum/sigils/table.rb

Constant Summary collapse

SelectedEvent =
Thaum::Event.define(:index, :row)
PAGE_STEP =
10

Instance Attribute Summary collapse

Attributes included from Sigil

#handler_parent, #rect, #thaum_app

Instance Method Summary collapse

Methods included from Sigil

#emit, #focusable?, #focused?, #on_blur, #on_focus, #on_mount, #on_mouse, #on_paste, #on_tick, #on_unmount, #on_update, #request_render

Constructor Details

#initialize(headers:, rows:, widths: nil) ⇒ Table

Returns a new instance of Table.



13
14
15
16
17
18
19
# File 'lib/thaum/sigils/table.rb', line 13

def initialize(headers:, rows:, widths: nil)
  @headers = headers
  @rows    = rows
  @widths  = widths
  @cursor  = 0
  @offset  = 0
end

Instance Attribute Details

#cursorObject (readonly)

Returns the value of attribute cursor.



11
12
13
# File 'lib/thaum/sigils/table.rb', line 11

def cursor
  @cursor
end

#headersObject (readonly)

Returns the value of attribute headers.



11
12
13
# File 'lib/thaum/sigils/table.rb', line 11

def headers
  @headers
end

#offsetObject (readonly)

Returns the value of attribute offset.



11
12
13
# File 'lib/thaum/sigils/table.rb', line 11

def offset
  @offset
end

#rowsObject (readonly)

Returns the value of attribute rows.



11
12
13
# File 'lib/thaum/sigils/table.rb', line 11

def rows
  @rows
end

#widthsObject (readonly)

Returns the value of attribute widths.



11
12
13
# File 'lib/thaum/sigils/table.rb', line 11

def widths
  @widths
end

Instance Method Details

#on_key(event) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/thaum/sigils/table.rb', line 21

def on_key(event)
  case event.key
  when :up        then @cursor = [@cursor - 1, 0].max
  when :down      then @cursor = [@cursor + 1, rows.length - 1].min if rows.any?
  when :home      then @cursor = 0
  when :end       then @cursor = rows.length - 1 if rows.any?
  when :page_up   then @cursor = [@cursor - PAGE_STEP, 0].max
  when :page_down then @cursor = [@cursor + PAGE_STEP, rows.length - 1].min if rows.any?
  when :enter     then emit_selected
  else                 emit event
  end
end

#render(canvas:, theme:) ⇒ Object



34
35
36
37
38
39
40
41
42
# File 'lib/thaum/sigils/table.rb', line 34

def render(canvas:, theme:)
  canvas.fill(bg: theme.bg)
  widths = effective_widths(canvas.width)
  visible_offset(canvas)

  render_header(canvas: canvas, theme: theme, widths: widths)
  render_separator(canvas: canvas, theme: theme)
  render_data_rows(canvas: canvas, theme: theme, widths: widths)
end