Class: Charming::Components::Viewport
- Inherits:
-
Charming::Component
- Object
- View
- Charming::Component
- Charming::Components::Viewport
- 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
-
#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) ⇒ 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) ⇒ 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
#column ⇒ Object (readonly)
Returns the value of attribute column.
22 23 24 |
# File 'lib/charming/components/viewport.rb', line 22 def column @column end |
#offset ⇒ Object (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. == :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
34 35 36 |
# File 'lib/charming/components/viewport.rb', line 34 def render visible_lines.map { |line| render_line(line) }.join("\n") end |