Class: Charming::Presentation::Components::Viewport

Inherits:
Charming::Presentation::Component show all
Includes:
KeyboardHandler
Defined in:
lib/charming/presentation/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

Constants included from KeyboardHandler

KeyboardHandler::VIM_KEYMAP

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, wrap: false, keymap: :vim) ⇒ Viewport

Returns a new instance of Viewport.



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

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

Instance Attribute Details

#columnObject (readonly)

Returns the value of attribute column.



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

def column
  @column
end

#offsetObject (readonly)

Returns the value of attribute offset.



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

def offset
  @offset
end

Instance Method Details

#handle_mouse(event) ⇒ Object



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

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



37
38
39
# File 'lib/charming/presentation/components/viewport.rb', line 37

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