Module: Clack::Core::TextInputHelper
- Included in:
- Prompts::Autocomplete, Prompts::AutocompleteMultiselect, Prompts::MultilineText, Prompts::Path, Prompts::Text
- Defined in:
- lib/clack/core/text_input_helper.rb
Overview
Shared functionality for text input prompts (Text, Autocomplete, Path). Handles cursor display, placeholder rendering, and text manipulation.
By default operates on @value and @cursor. Override text_value and text_value= in your class to use a different backing store (e.g. @search_text in AutocompleteMultiselect).
Instance Method Summary collapse
-
#current_placeholder ⇒ String?
The placeholder text, or nil if none set.
-
#format_placeholder_with_cursor(text) ⇒ String
Render placeholder text with an inverse cursor on the first character.
-
#handle_text_input(key) ⇒ Boolean
Handle text input key (backspace/delete or printable character).
-
#input_display ⇒ String
Display the input field with cursor or placeholder.
-
#placeholder_display ⇒ String
Display placeholder with cursor on first character.
-
#text_value ⇒ String
The text value being edited.
-
#text_value=(val) ⇒ Object
Set the text value.
-
#value_with_cursor ⇒ String
Display value with inverse cursor at current position.
Instance Method Details
#current_placeholder ⇒ String?
Returns the placeholder text, or nil if none set.
33 34 35 |
# File 'lib/clack/core/text_input_helper.rb', line 33 def current_placeholder defined?(@placeholder) ? @placeholder : nil end |
#format_placeholder_with_cursor(text) ⇒ String
Render placeholder text with an inverse cursor on the first character.
40 41 42 43 44 45 |
# File 'lib/clack/core/text_input_helper.rb', line 40 def format_placeholder_with_cursor(text) chars = text.grapheme_clusters first = chars.first || "" rest = chars[1..].join "#{Colors.inverse(first)}#{Colors.dim(rest)}" end |
#handle_text_input(key) ⇒ Boolean
Handle text input key (backspace/delete or printable character). Uses grapheme clusters for proper Unicode handling.
67 68 69 70 71 72 73 74 75 76 |
# File 'lib/clack/core/text_input_helper.rb', line 67 def handle_text_input(key) return handle_backspace if Core::Settings.backspace?(key) return false unless Core::Settings.printable?(key) chars = text_value.grapheme_clusters chars.insert(@cursor, key) self.text_value = chars.join @cursor += 1 true end |
#input_display ⇒ String
Display the input field with cursor or placeholder.
15 16 17 18 19 |
# File 'lib/clack/core/text_input_helper.rb', line 15 def input_display return placeholder_display if text_value.empty? value_with_cursor end |
#placeholder_display ⇒ String
Display placeholder with cursor on first character. Override @placeholder in including class to customize.
25 26 27 28 29 30 |
# File 'lib/clack/core/text_input_helper.rb', line 25 def placeholder_display text = current_placeholder return cursor_block if text.nil? || text.empty? format_placeholder_with_cursor(text) end |
#text_value ⇒ String
The text value being edited. Override to use a different backing store.
80 81 82 |
# File 'lib/clack/core/text_input_helper.rb', line 80 def text_value @value end |
#text_value=(val) ⇒ Object
Set the text value. Override to use a different backing store.
86 87 88 |
# File 'lib/clack/core/text_input_helper.rb', line 86 def text_value=(val) @value = val end |
#value_with_cursor ⇒ String
Display value with inverse cursor at current position. Uses grapheme clusters for proper Unicode handling (e.g., emoji).
51 52 53 54 55 56 57 58 59 60 |
# File 'lib/clack/core/text_input_helper.rb', line 51 def value_with_cursor val = text_value chars = val.grapheme_clusters return "#{val}#{cursor_block}" if @cursor >= chars.length before = chars[0...@cursor].join current = Colors.inverse(chars[@cursor]) after = chars[(@cursor + 1)..].join "#{before}#{current}#{after}" end |