Class: RubyTUI::Widgets::InputField

Inherits:
Object
  • Object
show all
Includes:
StatefulWidget
Defined in:
lib/rubytui/widgets/input_field.rb

Overview

InputField renders a single-line text input with cursor.

Examples:

state = RubyTUI::Widgets::InputState.new
state.handle_key(event)  # in the event loop
field = RubyTUI::Widgets::InputField.new(placeholder: "Type here...")
frame.render_stateful_widget(field, area, state)

See Also:

Instance Method Summary collapse

Constructor Details

#initialize(style: Style::DEFAULT, cursor_style: nil, placeholder: "", placeholder_style: nil, block: nil) ⇒ InputField

Returns a new instance of InputField.

Parameters:

  • style (Style) (defaults to: Style::DEFAULT) —

    text style (default: Style::DEFAULT)

  • cursor_style (Style, nil) (defaults to: nil) —

    style of the cell under the cursor (default: reversed video)

  • placeholder (String) (defaults to: "") —

    text shown while the input is empty (default: "")

  • placeholder_style (Style, nil) (defaults to: nil) —

    placeholder style (default: bright black foreground)

  • block (Widgets::Block, nil) (defaults to: nil) —

    optional wrapping block



179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/rubytui/widgets/input_field.rb', line 179

def initialize(
  style: Style::DEFAULT,
  cursor_style: nil,
  placeholder: "",
  placeholder_style: nil,
  block: nil
)
  @style = style
  @cursor_style = cursor_style || Style.new.reversed
  @placeholder = placeholder
  @placeholder_style = placeholder_style || Style.new.fg(Color::BRIGHT_BLACK)
  @block = block
end

Instance Method Details

#render(area, buf, state) ⇒ void

This method returns an undefined value.

Render the visible part of the text, or the placeholder, on the first row of the area and apply cursor_style to the cell at the cursor.

Updates state.scroll_offset in place so the cursor stays visible. Scrolling and cursor placement count characters, not display columns.

Parameters:

  • area (Rect) —

    the rectangular area to render into

  • buf (Buffer) —

    the buffer to write cells into

  • state (InputState) —

    text, cursor and scroll state



203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/rubytui/widgets/input_field.rb', line 203

def render(area, buf, state)
  return if area.empty?

  render_area = area
  if @block
    @block.render(area, buf)
    render_area = @block.inner(area)
  end

  return if render_area.empty?

  width = render_area.width

  # Adjust scroll offset to keep cursor visible
  if state.cursor < state.scroll_offset
    state.scroll_offset = state.cursor
  elsif state.cursor >= state.scroll_offset + width
    state.scroll_offset = state.cursor - width + 1
  end

  if state.text.empty?
    # Show placeholder
    visible = @placeholder[0, width]
    buf.set_string(render_area.x, render_area.y, visible, @placeholder_style)
  else
    # Show text with scrolling
    visible = state.text[state.scroll_offset, width] || ""
    buf.set_string(render_area.x, render_area.y, visible, @style)
  end

  # Draw cursor
  cursor_screen_x = render_area.x + state.cursor - state.scroll_offset
  if cursor_screen_x >= render_area.x && cursor_screen_x < render_area.right
    cell = buf[cursor_screen_x, render_area.y]
    if cell
      char = cell.symbol == " " && state.cursor >= state.text.length ? " " : cell.symbol
      cell.set(char, @cursor_style)
    end
  end
end