class BBS::Widgets::TextArea
A multi-line text editor. Lines are not wrapped: a line wider than the area is cut off.
Constants
- KEYS
-
The editing keys and the method each one calls.
Attributes
Callable invoked as call(value, area) after each edit.
Public Class Methods
# File lib/bbs/widgets/text_area.rb, line 21 def initialize(value: '', max_length: 8_000, on_change: nil, **kw) super(**kw) @lines = value.empty? ? [+''] : value.split("\n", -1).map(&:dup) @max_length = max_length @on_change = on_change @row = 0 @col = 0 @scroll = 0 end
Creates an editor holding value (lines separated by LF), accepting at most max_length characters (default 8000).
BBS::Widget::new
Public Instance Methods
Source
# File lib/bbs/widgets/text_area.rb, line 32 def focusable? = true
True: a text area takes the focus.
Source
# File lib/bbs/widgets/text_area.rb, line 54 def handle_event(event) return nil unless event.key? && enabled return send(KEYS[event.key]).then { :handled } if KEYS.key?(event.key) event.char? ? insert(event.key) : nil end
Edits the text on a key event: arrows move the cursor (Left and Right wrap across lines), Home and End go to the lineβs ends, Enter splits the line, Backspace deletes or joins with the line above, and a printable character is inserted while length is under max_length. Returns :handled, or nil for other keys, mouse events, or when disabled.
Source
# File lib/bbs/widgets/text_area.rb, line 37 def length = value.length
The length of value in characters.
Source
# File lib/bbs/widgets/text_area.rb, line 41 def render(frame) return if no_room? bounds.height.times { |r| draw_row(frame, r) } draw_cursor(frame) if focused end
Draws the visible lines into the bounds of frame, with an underscore cursor when focused.
Source
# File lib/bbs/widgets/text_area.rb, line 35 def value = @lines.join("\n")
The whole text, lines joined with LF.