Class: Charming::Presentation::Components::Viewport
- Inherits:
-
Charming::Presentation::Component
- Object
- View
- Charming::Presentation::Component
- Charming::Presentation::Components::Viewport
- 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
Instance Attribute Summary collapse
-
#column ⇒ Object
readonly
Returns the value of attribute column.
-
#offset ⇒ Object
readonly
Returns the value of attribute offset.
Instance Method Summary collapse
- #handle_mouse(event) ⇒ Object
-
#initialize(content:, width: nil, height: nil, offset: 0, column: 0, wrap: false, keymap: :vim) ⇒ Viewport
constructor
A new instance of Viewport.
- #render ⇒ Object
Methods included from KeyboardHandler
Methods inherited from View
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
#column ⇒ Object (readonly)
Returns the value of attribute column.
23 24 25 |
# File 'lib/charming/presentation/components/viewport.rb', line 23 def column @column end |
#offset ⇒ Object (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. == :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 + @offset = clicked_row clamp_position :handled end |
#render ⇒ Object
37 38 39 |
# File 'lib/charming/presentation/components/viewport.rb', line 37 def render visible_lines.map { |line| render_line(line) }.join("\n") end |