Class: Charming::Presentation::Components::TextArea
- Inherits:
-
Charming::Presentation::Component
- Object
- View
- Charming::Presentation::Component
- Charming::Presentation::Components::TextArea
- Defined in:
- lib/charming/presentation/components/text_area.rb
Overview
TextArea is a multi-line text editor component. Supports character insertion (with newline insertion via Shift+Enter or Ctrl+J), cursor movement (left/right/up/down, home/end, page up/down), deletion (backspace/delete), and scrolling for long buffers. Vertical movement preserves a “preferred column” so left/right navigation feels stable.
Instance Attribute Summary collapse
-
#cursor ⇒ Object
readonly
The current text value, cursor byte offset, top-visible row offset, and remembered column for vertical navigation, respectively.
-
#offset ⇒ Object
readonly
The current text value, cursor byte offset, top-visible row offset, and remembered column for vertical navigation, respectively.
-
#preferred_column ⇒ Object
readonly
The current text value, cursor byte offset, top-visible row offset, and remembered column for vertical navigation, respectively.
-
#value ⇒ Object
readonly
The current text value, cursor byte offset, top-visible row offset, and remembered column for vertical navigation, respectively.
Instance Method Summary collapse
-
#handle_key(event) ⇒ Object
Routes key events to the appropriate cursor/text mutation.
-
#initialize(value: "", placeholder: "", width: nil, height: nil, cursor: nil, offset: 0, preferred_column: nil) ⇒ TextArea
constructor
value is the initial text.
-
#render ⇒ Object
Renders the visible portion of the text buffer (scrolled to ‘offset`), with each visible line either clipped to `width` or padded to it.
Methods inherited from View
Constructor Details
#initialize(value: "", placeholder: "", width: nil, height: nil, cursor: nil, offset: 0, preferred_column: nil) ⇒ TextArea
value is the initial text. placeholder is shown when the value is empty. width and height constrain the rendered output. cursor defaults to the end of the value. offset is the top-visible row. preferred_column is the column to resume at on vertical movement (defaults to the current column on first use).
19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/charming/presentation/components/text_area.rb', line 19 def initialize(value: "", placeholder: "", width: nil, height: nil, cursor: nil, offset: 0, preferred_column: nil) super() @value = value.dup @placeholder = placeholder @width = width @height = height @cursor = cursor || @value.length @offset = offset @preferred_column = preferred_column clamp_position ensure_cursor_visible end |
Instance Attribute Details
#cursor ⇒ Object (readonly)
The current text value, cursor byte offset, top-visible row offset, and remembered column for vertical navigation, respectively.
13 14 15 |
# File 'lib/charming/presentation/components/text_area.rb', line 13 def cursor @cursor end |
#offset ⇒ Object (readonly)
The current text value, cursor byte offset, top-visible row offset, and remembered column for vertical navigation, respectively.
13 14 15 |
# File 'lib/charming/presentation/components/text_area.rb', line 13 def offset @offset end |
#preferred_column ⇒ Object (readonly)
The current text value, cursor byte offset, top-visible row offset, and remembered column for vertical navigation, respectively.
13 14 15 |
# File 'lib/charming/presentation/components/text_area.rb', line 13 def preferred_column @preferred_column end |
#value ⇒ Object (readonly)
The current text value, cursor byte offset, top-visible row offset, and remembered column for vertical navigation, respectively.
13 14 15 |
# File 'lib/charming/presentation/components/text_area.rb', line 13 def value @value end |
Instance Method Details
#handle_key(event) ⇒ Object
Routes key events to the appropriate cursor/text mutation. Returns :handled when the event was consumed, nil otherwise.
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/charming/presentation/components/text_area.rb', line 34 def handle_key(event) key = Charming.key_of(event) return :handled if newline_event?(event) && insert("\n") return :handled if character_event?(event) && insert(event.char) case key when :left then move_left when :right then move_right when :up then move_up when :down then move_down when :home then move_home when :end then move_end when :backspace then delete_before_cursor when :delete then delete_at_cursor when :page_up then page_up when :page_down then page_down else return nil end :handled end |
#render ⇒ Object
Renders the visible portion of the text buffer (scrolled to ‘offset`), with each visible line either clipped to `width` or padded to it.
58 59 60 |
# File 'lib/charming/presentation/components/text_area.rb', line 58 def render visible_lines.map { |line| render_line(line) }.join("\n") end |