Class: Clack::Prompts::Path

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

Overview

File/directory path selector with filesystem navigation.

Type to filter suggestions from the current directory. Press Tab to autocomplete the selected suggestion. Navigate suggestions with arrow keys.

Supports:

  • Absolute paths (starting with /)

  • Home directory expansion (~/…)

  • Relative paths (from root directory)

  • Directory-only filtering

Examples:

Basic usage

path = Clack.path(message: "Select a file")

Directory picker

dir = Clack.path(
  message: "Choose project directory",
  only_directories: true,
  root: "~/projects"
)

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, #input_display, #text_value, #text_value=, #value_with_cursor

Methods included from Core::OptionsHelper

#find_initial_cursor, #find_next_enabled, #first_enabled_index, #move_cursor, #move_selection, normalize_option, #normalize_options, #update_scroll, #visible_options

Methods inherited from Core::Prompt

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

Constructor Details

#initialize(message:, root: ".", only_directories: false, max_items: 5, **opts) ⇒ Path

Returns a new instance of Path.

Parameters:

  • message (String)

    the prompt message

  • root (String) (defaults to: ".")

    starting/base directory (default: “.”)

  • only_directories (Boolean) (defaults to: false)

    only show directories (default: false)

  • max_items (Integer) (defaults to: 5)

    max visible suggestions (default: 5)

  • opts (Hash)

    a customizable set of options

Options Hash (**opts):

  • :validate (Proc, nil)

    validation proc for the final path

  • additional (Hash)

    options passed to Core::Prompt



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/clack/prompts/path.rb', line 37

def initialize(message:, root: ".", only_directories: false, max_items: 5, **opts)
  super(message:, **opts)
  @root = File.expand_path(root)
  @only_directories = only_directories
  @max_items = max_items
  @value = ""
  @cursor = 0
  @option_index = 0
  @scroll_offset = 0
  @suggestions = []
  @dir_cache = {}     # directory path => sorted entries array
  update_suggestions
end