Class: Chamomile::TextInput
- Inherits:
-
Object
- Object
- Chamomile::TextInput
- Defined in:
- lib/chamomile/components/text_input.rb,
lib/chamomile/components/text_input/key_map.rb
Overview
Single-line text input with cursor movement, editing, and echo modes.
Constant Summary collapse
- ECHO_NORMAL =
:normal- ECHO_PASSWORD =
:password- ECHO_NONE =
:none- DEFAULT_KEY_MAP =
KeyBinding.normalize({ character_forward: [[:right, []], ["f", [:ctrl]]], character_backward: [[:left, []], ["b", [:ctrl]]], word_forward: [[:right, [:alt]], [:right, [:ctrl]], ["f", [:alt]]], word_backward: [[:left, [:alt]], [:left, [:ctrl]], ["b", [:alt]]], delete_word_backward: [[:backspace, [:alt]], ["w", [:ctrl]]], delete_word_forward: [[:delete, [:alt]], ["d", [:alt]]], delete_after_cursor: [["k", [:ctrl]]], delete_before_cursor: [["u", [:ctrl]]], delete_char_backward: [[:backspace, []], ["h", [:ctrl]]], delete_char_forward: [[:delete, []], ["d", [:ctrl]]], line_start: [[:home, []], ["a", [:ctrl]]], line_end: [[:end_key, []], ["e", [:ctrl]]], })
Instance Attribute Summary collapse
-
#char_limit ⇒ Object
Returns the value of attribute char_limit.
-
#echo_char ⇒ Object
Returns the value of attribute echo_char.
-
#echo_mode ⇒ Object
Returns the value of attribute echo_mode.
-
#err ⇒ Object
readonly
Returns the value of attribute err.
-
#key_map ⇒ Object
Returns the value of attribute key_map.
-
#placeholder ⇒ Object
Returns the value of attribute placeholder.
-
#position ⇒ Object
Returns the value of attribute position.
-
#prompt ⇒ Object
Returns the value of attribute prompt.
-
#value ⇒ Object
Returns the value of attribute value.
-
#width ⇒ Object
Returns the value of attribute width.
Instance Method Summary collapse
- #blur ⇒ Object
-
#focus ⇒ Object
Focus management.
- #focused? ⇒ Boolean
-
#handle(msg) ⇒ Object
(also: #update)
Handle an incoming event.
-
#initialize(prompt: "", placeholder: "", char_limit: 0, width: 0, echo_mode: ECHO_NORMAL, echo_char: "*", key_map: DEFAULT_KEY_MAP, validate: nil) ⇒ TextInput
constructor
A new instance of TextInput.
- #view ⇒ Object
Constructor Details
#initialize(prompt: "", placeholder: "", char_limit: 0, width: 0, echo_mode: ECHO_NORMAL, echo_char: "*", key_map: DEFAULT_KEY_MAP, validate: nil) ⇒ TextInput
Returns a new instance of TextInput.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# 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 |
Instance Attribute Details
#char_limit ⇒ Object
Returns the value of attribute char_limit.
11 12 13 |
# File 'lib/chamomile/components/text_input.rb', line 11 def char_limit @char_limit end |
#echo_char ⇒ Object
Returns the value of attribute echo_char.
10 11 12 |
# File 'lib/chamomile/components/text_input.rb', line 10 def echo_char @echo_char end |
#echo_mode ⇒ Object
Returns the value of attribute echo_mode.
11 12 13 |
# File 'lib/chamomile/components/text_input.rb', line 11 def echo_mode @echo_mode end |
#err ⇒ Object (readonly)
Returns the value of attribute err.
10 11 12 |
# File 'lib/chamomile/components/text_input.rb', line 10 def err @err end |
#key_map ⇒ Object
Returns the value of attribute key_map.
11 12 13 |
# File 'lib/chamomile/components/text_input.rb', line 11 def key_map @key_map end |
#placeholder ⇒ Object
Returns the value of attribute placeholder.
11 12 13 |
# File 'lib/chamomile/components/text_input.rb', line 11 def placeholder @placeholder end |
#position ⇒ Object
Returns the value of attribute position.
10 11 12 |
# File 'lib/chamomile/components/text_input.rb', line 10 def position @position end |
#prompt ⇒ Object
Returns the value of attribute prompt.
11 12 13 |
# File 'lib/chamomile/components/text_input.rb', line 11 def prompt @prompt end |
#value ⇒ Object
Returns the value of attribute value.
10 11 12 |
# File 'lib/chamomile/components/text_input.rb', line 10 def value @value end |
#width ⇒ Object
Returns the value of attribute width.
11 12 13 |
# File 'lib/chamomile/components/text_input.rb', line 11 def width @width end |
Instance Method Details
#blur ⇒ Object
39 40 41 42 |
# File 'lib/chamomile/components/text_input.rb', line 39 def blur @focused = false self end |
#focus ⇒ Object
Focus management
34 35 36 37 |
# File 'lib/chamomile/components/text_input.rb', line 34 def focus @focused = true self end |
#focused? ⇒ Boolean
44 45 46 |
# File 'lib/chamomile/components/text_input.rb', line 44 def focused? @focused end |
#handle(msg) ⇒ Object Also known as: update
Handle an incoming event. Primary API — replaces the old update name.
69 70 71 72 73 74 75 76 77 78 79 80 |
# 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 |
#view ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# 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 |