Class: Charming::Components::Viewport

Inherits:
Charming::Component show all
Includes:
KeyboardHandler
Defined in:
lib/charming/components/viewport.rb

Constant Summary collapse

ANSI_PATTERN =
/\e\[[0-9;]*m/
KEY_ACTIONS =
{
  up: :scroll_up,
  down: :scroll_down,
  page_up: :page_up,
  page_down: :page_down,
  home: :scroll_home,
  end: :scroll_end,
  left: :scroll_left,
  right: :scroll_right
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from KeyboardHandler

#handle_key

Methods inherited from View

#focused?, #layout_assigns

Constructor Details

#initialize(content:, width: nil, height: nil, offset: 0, column: 0) ⇒ Viewport

Returns a new instance of Viewport.



24
25
26
27
28
29
30
31
32
# File 'lib/charming/components/viewport.rb', line 24

def initialize(content:, width: nil, height: nil, offset: 0, column: 0)
  super()
  @content = content
  @width = width
  @height = height
  @offset = offset
  @column = column
  clamp_position
end

Instance Attribute Details

#columnObject (readonly)

Returns the value of attribute column.



22
23
24
# File 'lib/charming/components/viewport.rb', line 22

def column
  @column
end

#offsetObject (readonly)

Returns the value of attribute offset.



22
23
24
# File 'lib/charming/components/viewport.rb', line 22

def offset
  @offset
end

Instance Method Details

#handle_mouse(event) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/charming/components/viewport.rb', line 38

def handle_mouse(event)
  return nil unless height

  if event.scroll?
    scroll_delta = (event.button_name == :scroll_up) ? -1 : 1
    @offset += scroll_delta
    clamp_position
    return :handled
  end

  return nil unless event.click?

  clicked_row = event.y
  return nil if clicked_row < offset || clicked_row >= offset + viewport_height

  @offset = clicked_row
  clamp_position
  :handled
end

#renderObject



34
35
36
# File 'lib/charming/components/viewport.rb', line 34

def render
  visible_lines.map { |line| render_line(line) }.join("\n")
end