Class: TuiTui::Modal::Prompt

Inherits:
Modal
  • Object
show all
Defined in:
lib/tui_tui/modal/prompt.rb

Overview

Single-line text input modal with terminal-column-aware cursor placement.

Constant Summary collapse

MIN_INNER =
24

Instance Method Summary collapse

Constructor Details

#initialize(label, value: "", theme: Theme::DEFAULT) ⇒ Prompt

Returns a new instance of Prompt.



13
14
15
16
17
# File 'lib/tui_tui/modal/prompt.rb', line 13

def initialize(label, value: "", theme: Theme::DEFAULT)
  @label = DisplayText.new(label)
  @input = TextInput.new(value: value)
  @theme = theme
end

Instance Method Details

#draw(canvas, size) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/tui_tui/modal/prompt.rb', line 40

def draw(canvas, size)
  # Grow the panel to fit the value, but never past the screen: beyond that
  # the value scrolls horizontally inside its column budget (see #viewport).
  max_inner = [size.cols - (PAD * 2) - 2, MIN_INNER].max
  desired = @label.width + 1 + DisplayText.new(value).width
  inner = desired.clamp(MIN_INNER, max_inner)
  rect, col = panel(canvas, inner: inner, body_rows: 1)

  canvas.text(rect.row + 1, col, @label, theme.title)
  @text_row = rect.row + 1
  @text_col = col + @label.width + 1
  draw_value(canvas, [inner - @label.width - 1, 1].max)
  canvas
end

#handle(key) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/tui_tui/modal/prompt.rb', line 21

def handle(key)
  case key
  when "\r"
    [:ok, value]
  when :escape, KeyCode::CTRL_C
    :cancel
  else
    @input.handle_editing(key)
    nil
  end
end

#handle_mouse(event) ⇒ Object



33
34
35
36
37
38
# File 'lib/tui_tui/modal/prompt.rb', line 33

def handle_mouse(event)
  return nil unless event.action == :press && @text_row == event.row

  @input.cursor = @input.index_at(event.col - @text_col, from: @scroll)
  nil
end

#valueObject



19
# File 'lib/tui_tui/modal/prompt.rb', line 19

def value = @input.value