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

Instance Method Details

#current_placeholderString?

Returns the placeholder text, or nil if none set.

Returns:

  • (String, nil)

    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.

Parameters:

  • text (String)

    placeholder text to format

Returns:

  • (String)

    formatted placeholder with cursor highlight



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.

Parameters:

  • key (String)

    The key pressed

Returns:

  • (Boolean)

    true if input was handled



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_displayString

Display the input field with cursor or placeholder.

Returns:

  • (String)

    Formatted input display



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_displayString

Display placeholder with cursor on first character. Override @placeholder in including class to customize.

Returns:

  • (String)

    Formatted placeholder



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_valueString

The text value being edited. Override to use a different backing store.

Returns:

  • (String)


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.

Parameters:

  • val (String)


86
87
88
# File 'lib/clack/core/text_input_helper.rb', line 86

def text_value=(val)
  @value = val
end

#value_with_cursorString

Display value with inverse cursor at current position. Uses grapheme clusters for proper Unicode handling (e.g., emoji).

Returns:

  • (String)

    Value with cursor



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