Class: Clack::Prompts::Text

Inherits:
Core::Prompt show all
Includes:
Core::TextInputHelper
Defined in:
lib/clack/prompts/text.rb

Overview

Single-line text input prompt with cursor navigation.

Features:

  • Arrow key cursor movement (left/right)

  • Backspace/delete support

  • Placeholder text (shown when empty)

  • Default value (used if submitted empty)

  • Initial value (pre-filled, editable)

  • Validation support

  • Tab completion (optional)

Examples:

Basic usage

name = Clack.text(message: "What is your name?")

With all options

name = Clack.text(
  message: "Project name?",
  placeholder: "my-project",
  default_value: "untitled",
  initial_value: "hello",
  validate: ->(v) { "Required!" if v.empty? }
)

With tab completion

cmd = Clack.text(
  message: "Command?",
  completions: %w[build test deploy lint format]
)

With dynamic completions

cmd = Clack.text(
  message: "Command?",
  completions: ->(input) { Dir.glob("#{input}*") }
)

Constant Summary

Constants inherited from Core::Prompt

Core::Prompt::MIN_TERMINAL_WIDTH

Instance Attribute Summary

Attributes inherited from Core::Prompt

#error_message, #state, #value, #warning_message

Instance Method Summary collapse

Methods included from Core::TextInputHelper

#current_placeholder, #format_placeholder_with_cursor, #handle_text_input, #input_display, #placeholder_display, #text_value, #text_value=, #value_with_cursor

Methods inherited from Core::Prompt

flush_resize, register, #request_redraw, #run, setup_signal_handler, unregister

Constructor Details

#initialize(message:, placeholder: nil, default_value: nil, initial_value: nil, completions: nil, **opts) ⇒ Text

Returns a new instance of Text.

Parameters:

  • message (String)

    the prompt message

  • placeholder (String, nil) (defaults to: nil)

    dim text shown when input is empty

  • default_value (String, nil) (defaults to: nil)

    value used if submitted empty

  • initial_value (String, nil) (defaults to: nil)

    pre-filled editable text

  • completions (Array<String>, Proc, nil) (defaults to: nil)

    tab completion candidates. Array of strings or a proc that receives current input and returns candidates.

  • opts (Hash)

    a customizable set of options

Options Hash (**opts):

  • :validate (Proc, nil)

    validation proc returning error string or nil

  • additional (Hash)

    options passed to Core::Prompt



52
53
54
55
56
57
58
59
# File 'lib/clack/prompts/text.rb', line 52

def initialize(message:, placeholder: nil, default_value: nil, initial_value: nil, completions: nil, **opts)
  super(message:, **opts)
  @placeholder = placeholder
  @default_value = default_value
  @completions = completions
  @value = initial_value || ""
  @cursor = @value.grapheme_clusters.length
end