class BBS::Widgets::TextInput
A one-line text field that scrolls sideways when the text is wider than the field.
Constants
- KEYS
-
The editing keys and the method each one calls.
Attributes
- placeholder
-
dim text shown while the field is empty and unfocused
max_length-
the most characters the field accepts
on_submit-
callable invoked as
call(value, input)on Enter; its result (e.g. :halt) is returned fromhandle_event on_change-
callable invoked as
call(value, input)after each edit - password
-
when true, the text is shown as asterisks
- placeholder
-
dim text shown while the field is empty and unfocused
max_length-
the most characters the field accepts
on_submit-
callable invoked as
call(value, input)on Enter; its result (e.g. :halt) is returned fromhandle_event on_change-
callable invoked as
call(value, input)after each edit - password
-
when true, the text is shown as asterisks
- placeholder
-
dim text shown while the field is empty and unfocused
max_length-
the most characters the field accepts
on_submit-
callable invoked as
call(value, input)on Enter; its result (e.g. :halt) is returned fromhandle_event on_change-
callable invoked as
call(value, input)after each edit - password
-
when true, the text is shown as asterisks
- placeholder
-
dim text shown while the field is empty and unfocused
max_length-
the most characters the field accepts
on_submit-
callable invoked as
call(value, input)on Enter; its result (e.g. :halt) is returned fromhandle_event on_change-
callable invoked as
call(value, input)after each edit - password
-
when true, the text is shown as asterisks
- placeholder
-
dim text shown while the field is empty and unfocused
max_length-
the most characters the field accepts
on_submit-
callable invoked as
call(value, input)on Enter; its result (e.g. :halt) is returned fromhandle_event on_change-
callable invoked as
call(value, input)after each edit - password
-
when true, the text is shown as asterisks
The current text.
Public Class Methods
# File lib/bbs/widgets/text_input.rb, line 29 def initialize(value: '', placeholder: '', max_length: 256, on_submit: nil, on_change: nil, password: false, **kw) super(**kw) @placeholder = placeholder @max_length = max_length @on_submit = on_submit @on_change = on_change @password = password self.value = value end
Creates a text field holding a copy of value, with the cursor at its end. max_length defaults to 256.
BBS::Widget::new
Public Instance Methods
Source
# File lib/bbs/widgets/text_input.rb, line 50 def focusable? = true
True: a text field takes the focus.
Source
# File lib/bbs/widgets/text_input.rb, line 72 def handle_event(event) return nil unless event.key? && enabled return send(KEYS[event.key]) if KEYS.key?(event.key) event.char? ? insert(event.key) : nil end
Edits the text on a key event: Left, Right, Home and End move the cursor, Backspace and Delete remove a character, a printable character is inserted (up to max_length), Enter calls on_submit. Returns :handled (or on_submit‘s result on Enter), and nil for other keys, mouse events, or when disabled, so the window gets them.
Source
# File lib/bbs/widgets/text_input.rb, line 55 def render(frame) return if no_room? blank(frame, 0, sgr) if placeholder? put(frame, 0, 0, @placeholder[0, bounds.width - 1].to_s, style(:text_dim)) else put(frame, 0, 0, shown_text, sgr) end draw_cursor(frame) if focused end
Draws the visible part of the text (or the placeholder) on the first row of the bounds into frame, with an underscore cursor when focused.
Source
# File lib/bbs/widgets/text_input.rb, line 43 def value=(text) @value = text.to_s.dup @cursor = @value.length @scroll = 0 end
Replaces the text with text.to_s and puts the cursor at its end: a cursor left past the end (after a submit clears the field) would make the next key fail. Does not call on_change.