Class: Tuile::Component::TextInput

Inherits:
Tuile::Component show all
Defined in:
lib/tuile/component/text_input.rb

Overview

Abstract base for editable text components (TextField, TextArea).

Holds the shared state — a mutable #text buffer, a #caret index, #on_change and #on_escape callbacks — and the keyboard machinery that single-line and multi-line inputs both need: ESC handling, LEFT/RIGHT caret movement, CTRL+LEFT/CTRL+RIGHT word jumps, and the ‘focusable?`/`tab_stop?` flags.

Subclasses implement the layout-specific pieces (#cursor_position, #repaint) and add their own keys (HOME/END, ENTER, UP/DOWN, printable insertion) by overriding the protected #handle_text_input_key hook — ‘super` falls through to the common navigation handling.

The mutation pipeline is a template method: #text= and #caret= detect no-ops, mutate state, fire #on_change, and invalidate. Subclasses inject their own behavior via two protected hooks:

  • #preprocess_text — input filter (e.g. TextField truncates to fit ‘rect.width - 1`).

  • #on_text_mutated / #on_caret_mutated — post-mutation side effects (e.g. TextArea invalidates its wrap cache and scrolls to keep the caret visible).

Direct Known Subclasses

TextArea, TextField

Constant Summary collapse

ACTIVE_BG_SGR =

256-color SGR for the focused-button highlight (matches what ‘Rainbow(…).bg(:darkslategray)` emits, which is what Button#repaint uses for its focused state).

Returns:

  • (String)
"\e[48;5;59m"
INACTIVE_BG_SGR =

256-color SGR for the unfocused field’s “well”: index 238 sits in the grayscale ramp (~#444444), bright enough to stand out against non-pure-black terminal themes (Gruvbox/Solarized/OneDark base backgrounds sit in the #1d–#2d range), and still distinctly darker than the active highlight at index 59 (~#5f5f5f). Rainbow’s RGB-to-256 mapping snaps everything dark to palette index 16 (terminal black), so we emit the escape directly to reach the ramp.

Returns:

  • (String)
"\e[48;5;238m"

Instance Attribute Summary collapse

Attributes inherited from Tuile::Component

#key_shortcut, #parent, #rect

Instance Method Summary collapse

Methods inherited from Tuile::Component

#active=, #active?, #attached?, #children, #content_size, #cursor_position, #depth, #find_shortcut_component, #focus, #handle_mouse, #keyboard_hint, #on_child_removed, #on_focus, #on_tree, #repaint, #root, #screen

Constructor Details

#initializeTextInput

Returns a new instance of TextInput.



29
30
31
32
33
34
35
# File 'lib/tuile/component/text_input.rb', line 29

def initialize
  super
  @text = +""
  @caret = 0
  @on_change = nil
  @on_escape = method(:default_on_escape)
end

Instance Attribute Details

#caretInteger

Returns caret index in ‘0..text.length`.

Returns:

  • (Integer)

    caret index in ‘0..text.length`.



44
45
46
# File 'lib/tuile/component/text_input.rb', line 44

def caret
  @caret
end

#on_changeProc, ...

Optional callback fired whenever #text changes. Receives the new text as a single argument. Not fired by #caret= (text unchanged) and not fired when a setter is a no-op.

Returns:

  • (Proc, Method, nil)

    one-arg callable, or nil.



50
51
52
# File 'lib/tuile/component/text_input.rb', line 50

def on_change
  @on_change
end

#on_escapeProc, ...

Callback fired when ESC is pressed. Defaults to a closure that clears focus (‘screen.focused = nil`) so ESC visibly cancels text entry instead of bubbling to the parent — and, in particular, instead of reaching the screen’s default ESC-to-quit handler. Set to nil to let ESC fall through to the parent again; set to any other callable to replace the default.

Returns:

  • (Proc, Method, nil)

    no-arg callable, or nil.



58
59
60
# File 'lib/tuile/component/text_input.rb', line 58

def on_escape
  @on_escape
end

#textString

Returns current text contents.

Returns:

  • (String)

    current text contents.



38
39
40
# File 'lib/tuile/component/text_input.rb', line 38

def text
  @text
end

Instance Method Details

#empty?Boolean

Returns true iff #text is the empty string.

Returns:

  • (Boolean)

    true iff #text is the empty string.



41
# File 'lib/tuile/component/text_input.rb', line 41

def empty? = @text.empty?

#focusable?Boolean

Returns:

  • (Boolean)


60
# File 'lib/tuile/component/text_input.rb', line 60

def focusable? = true

#handle_key(key) ⇒ Boolean

Handles a key. Returns false when the component is inactive. Otherwise first runs the Tuile::Component#handle_key shortcut search via ‘super`, then delegates to #handle_text_input_key.

Parameters:

  • key (String)

Returns:

  • (Boolean)


111
112
113
114
115
116
# File 'lib/tuile/component/text_input.rb', line 111

def handle_key(key)
  return false unless active?
  return true if super

  handle_text_input_key(key)
end

#tab_stop?Boolean

Returns:

  • (Boolean)


62
# File 'lib/tuile/component/text_input.rb', line 62

def tab_stop? = true