class Chamomile::TextInput
Single-line text input with cursor movement, editing, and echo modes.
Constants
- DEFAULT_KEY_MAP
- ECHO_NONE
- ECHO_NORMAL
- ECHO_PASSWORD
Attributes
Public Class Methods
Source
# File lib/chamomile/components/text_input.rb, line 14 def initialize(prompt: "", placeholder: "", char_limit: 0, width: 0, echo_mode: ECHO_NORMAL, echo_char: "*", key_map: DEFAULT_KEY_MAP, validate: nil) @prompt = prompt @placeholder = placeholder @char_limit = char_limit @width = width @echo_mode = echo_mode self.echo_char = echo_char @key_map = key_map @validate = validate @value = "" @position = 0 @focused = false @offset = 0 @err = nil end
Public Instance Methods
Source
# File lib/chamomile/components/text_input.rb, line 39 def blur @focused = false self end
Source
# File lib/chamomile/components/text_input.rb, line 48 def echo_char=(c) @echo_char = c.to_s[0] || "*" end
Source
# File lib/chamomile/components/text_input.rb, line 34 def focus @focused = true self end
Focus management
Source
# File lib/chamomile/components/text_input.rb, line 69 def handle(msg) return unless @focused case msg when Chamomile::KeyEvent handle_key(msg) when Chamomile::PasteEvent handle_paste(msg) end nil end
Handle an incoming event. Primary API — replaces the old update name.
Also aliased as: update
Source
# File lib/chamomile/components/text_input.rb, line 63 def position=(p) @position = p.clamp(0, @value.length) recalc_offset end
Source
# File lib/chamomile/components/text_input.rb, line 54 def value=(v) v = v.to_s v = v[0, @char_limit] if @char_limit.positive? && v.length > @char_limit @value = v @position = @position.clamp(0, @value.length) run_validate recalc_offset end
Value access
Source
# File lib/chamomile/components/text_input.rb, line 85 def view return "#{@prompt}#{@placeholder}" if @value.empty? && !@placeholder.empty? && !@focused display = build_display_value visible = visible_portion(display) if @focused cursor_pos = @position - @offset if cursor_pos >= 0 && cursor_pos < visible.length before = visible[0, cursor_pos] cursor_char = visible[cursor_pos] after = visible[(cursor_pos + 1)..] "#{@prompt}#{before}\e[7m#{cursor_char}\e[0m#{after}" else # Cursor at end "#{@prompt}#{visible}\e[7m \e[0m" end else "#{@prompt}#{visible}" end end