Module: Clack
- Defined in:
- lib/clack.rb,
lib/clack/box.rb,
lib/clack/log.rb,
lib/clack/note.rb,
lib/clack/group.rb,
lib/clack/utils.rb,
lib/clack/colors.rb,
lib/clack/errors.rb,
lib/clack/stream.rb,
lib/clack/symbols.rb,
lib/clack/testing.rb,
lib/clack/version.rb,
lib/clack/task_log.rb,
lib/clack/validators.rb,
lib/clack/core/chrome.rb,
lib/clack/core/cursor.rb,
lib/clack/core/prompt.rb,
lib/clack/environment.rb,
lib/clack/core/ci_mode.rb,
lib/clack/prompts/date.rb,
lib/clack/prompts/path.rb,
lib/clack/prompts/text.rb,
lib/clack/transformers.rb,
lib/clack/core/settings.rb,
lib/clack/prompts/range.rb,
lib/clack/prompts/tasks.rb,
lib/clack/prompts/select.rb,
lib/clack/core/key_reader.rb,
lib/clack/prompts/confirm.rb,
lib/clack/prompts/spinner.rb,
lib/clack/prompts/password.rb,
lib/clack/prompts/progress.rb,
lib/clack/core/fuzzy_matcher.rb,
lib/clack/core/scroll_helper.rb,
lib/clack/prompts/select_key.rb,
lib/clack/core/options_helper.rb,
lib/clack/prompts/multiselect.rb,
lib/clack/prompts/autocomplete.rb,
lib/clack/core/selection_manager.rb,
lib/clack/core/text_input_helper.rb,
lib/clack/prompts/multiline_text.rb,
lib/clack/prompts/group_multiselect.rb,
lib/clack/prompts/autocomplete_multiselect.rb
Overview
Clack - Beautiful CLI prompts for Ruby
A faithful Ruby port of @clack/prompts, bringing delightful terminal aesthetics to your Ruby projects.
Defined Under Namespace
Modules: Box, Colors, Core, Environment, Log, Note, Prompts, Stream, Symbols, Testing, Transformers, Utils, Validators Classes: Group, NotATerminalError, TaskLog, TaskLogGroup, Warning
Constant Summary collapse
- CANCEL =
Sentinel value returned when user cancels a prompt (Escape or Ctrl+C)
Object.new.tap { |o| o.define_singleton_method(:inspect) { "Clack::CANCEL" } }.freeze
- VERSION =
Current gem version.
"0.7.0"
Class Method Summary collapse
-
.autocomplete(message:, options:, **opts) ⇒ Object, CANCEL
Prompt with type-to-filter autocomplete.
-
.autocomplete_multiselect(message:, options:, **opts) ⇒ Array, CANCEL
Prompt with type-to-filter autocomplete and multiselect.
-
.box(message = "", title: "", **opts) ⇒ void
Display content in a customizable box.
-
.cancel(message = nil, with_guide: nil, output: $stdout) ⇒ void
Display a cancellation message (typically after user presses Escape).
-
.cancel?(value) ⇒ Boolean
(also: cancelled?)
Check if a prompt result was cancelled by the user.
-
.ci? ⇒ Boolean
Check if running in a CI environment.
-
.columns(output = $stdout, default: 80) ⇒ Integer
Get terminal columns (width).
-
.confirm(message:, **opts) ⇒ Boolean, CANCEL
Prompt for yes/no confirmation.
-
.date(message:, **opts) ⇒ Date, CANCEL
Prompt for date selection with inline segmented input.
-
.demo ⇒ void
Run the interactive demo showcasing all Clack features.
-
.group(on_cancel: nil) {|group| ... } ⇒ Hash, Clack::CANCEL
Run a group of prompts and collect their results.
-
.group_multiselect(message:, options:, **opts) ⇒ Array, CANCEL
Prompt to select multiple options organized in groups.
-
.handle_cancel(value, message = nil, with_guide: nil, output: $stdout) ⇒ Boolean
Check if cancelled and show cancel message if so.
-
.intro(title = nil, with_guide: nil, output: $stdout) ⇒ void
Display an intro banner at the start of a CLI session.
-
.log ⇒ Module
Access the Log module for styled console output.
-
.multiline_text(message:, **opts) ⇒ String, CANCEL
Prompt for multi-line text input.
-
.multiselect(message:, options:, **opts) ⇒ Array, CANCEL
Prompt to select multiple options from a list.
-
.note(message = "", title: nil, **opts) ⇒ void
Display a note box with optional title.
-
.outro(message = nil, with_guide: nil, output: $stdout) ⇒ void
Display an outro banner at the end of a CLI session.
-
.password(message:, **opts) ⇒ String, CANCEL
Prompt for password input (masked display).
-
.path(message:, **opts) ⇒ String, CANCEL
Prompt for file/directory path with filesystem navigation.
-
.progress(total:, **opts) ⇒ Prompts::Progress
Create a progress bar for measurable operations.
-
.range(message:, **opts) ⇒ Numeric, CANCEL
Prompt for a numeric value using a slider.
-
.rows(output = $stdout, default: 24) ⇒ Integer
Get terminal rows (height).
-
.select(message:, options:, **opts) ⇒ Object, CANCEL
Prompt to select one option from a list.
-
.select_key(message:, options:, **opts) ⇒ Object, CANCEL
Prompt to select an option by pressing a key.
-
.settings ⇒ Hash
Access global settings.
-
.setup! ⇒ void
Install signal handlers for clean terminal cleanup.
-
.setup? ⇒ Boolean
Whether setup! has been called.
-
.spin(message, success: nil, error: nil, **opts) ⇒ Object
Run a block with a spinner, handling success/error automatically.
-
.spinner(**opts) ⇒ Prompts::Spinner
Create an animated spinner for async operations.
-
.stream ⇒ Module
Access the Stream module for streaming output.
-
.task_log(title:, **opts) ⇒ TaskLog
Create a streaming task log that clears on success, shows on error.
-
.tasks(tasks:, **opts) ⇒ Array<Hash>
Run multiple tasks with progress indicators.
-
.text(message:, **opts) ⇒ String, CANCEL
Prompt for single-line text input.
-
.tty?(output = $stdout) ⇒ Boolean
Check if stdout is a TTY.
-
.update_settings(**opts) ⇒ Hash
Update global settings.
-
.warning(message) ⇒ Warning
Create a validation warning that allows the user to proceed with confirmation.
-
.windows? ⇒ Boolean
Check if running on Windows.
Class Method Details
.autocomplete(message:, options:, **opts) ⇒ Object, CANCEL
Prompt with type-to-filter autocomplete.
373 374 375 |
# File 'lib/clack.rb', line 373 def autocomplete(message:, options:, **opts) Prompts::Autocomplete.new(message:, options: , **opts).run end |
.autocomplete_multiselect(message:, options:, **opts) ⇒ Array, CANCEL
Prompt with type-to-filter autocomplete and multiselect.
392 393 394 |
# File 'lib/clack.rb', line 392 def autocomplete_multiselect(message:, options:, **opts) Prompts::AutocompleteMultiselect.new(message:, options: , **opts).run end |
.box(message = "", title: "", **opts) ⇒ void
This method returns an undefined value.
Display content in a customizable box.
574 575 576 |
# File 'lib/clack.rb', line 574 def box( = "", title: "", **opts) Box.render(, title: title, **opts) end |
.cancel(message = nil, with_guide: nil, output: $stdout) ⇒ void
This method returns an undefined value.
Display a cancellation message (typically after user presses Escape).
158 159 160 161 162 163 164 165 166 |
# File 'lib/clack.rb', line 158 def cancel( = nil, with_guide: nil, output: $stdout) if Core::Settings.with_guide?(with_guide) output.puts Colors.gray(Symbols::S_BAR) output.puts "#{Colors.gray(Symbols::S_BAR_END)} #{Colors.red()}" else output.puts Colors.red() end output.puts end |
.cancel?(value) ⇒ Boolean Also known as: cancelled?
Check if a prompt result was cancelled by the user.
94 95 96 |
# File 'lib/clack.rb', line 94 def cancel?(value) value.equal?(CANCEL) end |
.ci? ⇒ Boolean
Check if running in a CI environment
633 634 635 |
# File 'lib/clack.rb', line 633 def ci? Environment.ci? end |
.columns(output = $stdout, default: 80) ⇒ Integer
Get terminal columns (width)
654 655 656 |
# File 'lib/clack.rb', line 654 def columns(output = $stdout, default: 80) Environment.columns(output, default: default) end |
.confirm(message:, **opts) ⇒ Boolean, CANCEL
Prompt for yes/no confirmation.
238 239 240 |
# File 'lib/clack.rb', line 238 def confirm(message:, **opts) Prompts::Confirm.new(message:, **opts).run end |
.date(message:, **opts) ⇒ Date, CANCEL
Prompt for date selection with inline segmented input.
Navigate between segments with Tab/arrow keys, adjust with up/down, or type digits directly.
507 508 509 |
# File 'lib/clack.rb', line 507 def date(message:, **opts) Prompts::Date.new(message:, **opts).run end |
.demo ⇒ void
This method returns an undefined value.
Run the interactive demo showcasing all Clack features. The demo implementation is in examples/demo.rb.
670 671 672 673 674 |
# File 'lib/clack.rb', line 670 def demo demo_path = File.("../examples/demo.rb", __dir__) load demo_path run_demo end |
.group(on_cancel: nil) {|group| ... } ⇒ Hash, Clack::CANCEL
Run a group of prompts and collect their results.
If any prompt is cancelled, the entire group returns Clack::CANCEL. The on_cancel callback receives partial results collected so far.
92 93 94 95 96 97 98 |
# File 'lib/clack/group.rb', line 92 def group(on_cancel: nil, &block) raise ArgumentError, "Block required for Clack.group" unless block_given? group = Group.new(on_cancel: on_cancel) block.call(group) group.run end |
.group_multiselect(message:, options:, **opts) ⇒ Array, CANCEL
Prompt to select multiple options organized in groups.
487 488 489 |
# File 'lib/clack.rb', line 487 def group_multiselect(message:, options:, **opts) Prompts::GroupMultiselect.new(message:, options: , **opts).run end |
.handle_cancel(value, message = nil, with_guide: nil, output: $stdout) ⇒ Boolean
Check if cancelled and show cancel message if so. Useful for guard clauses in CLI scripts.
118 119 120 121 122 123 |
# File 'lib/clack.rb', line 118 def handle_cancel(value, = nil, with_guide: nil, output: $stdout) return false unless cancel?(value) cancel( || Core::Settings.(:cancel), with_guide:, output:) true end |
.intro(title = nil, with_guide: nil, output: $stdout) ⇒ void
This method returns an undefined value.
Display an intro banner at the start of a CLI session.
131 132 133 134 |
# File 'lib/clack.rb', line 131 def intro(title = nil, with_guide: nil, output: $stdout) prefix = Core::Settings.with_guide?(with_guide) ? "#{Colors.gray(Symbols::S_BAR_START)} " : "" output.puts "#{prefix}#{title}" end |
.log ⇒ Module
Access the Log module for styled console output.
543 544 545 |
# File 'lib/clack.rb', line 543 def log Log end |
.multiline_text(message:, **opts) ⇒ String, CANCEL
Prompt for multi-line text input.
Enter inserts a newline, Ctrl+D submits. Useful for commit messages, notes, or any multi-line content.
199 200 201 |
# File 'lib/clack.rb', line 199 def multiline_text(message:, **opts) Prompts::MultilineText.new(message:, **opts).run end |
.multiselect(message:, options:, **opts) ⇒ Array, CANCEL
Prompt to select multiple options from a list.
274 275 276 |
# File 'lib/clack.rb', line 274 def multiselect(message:, options:, **opts) Prompts::Multiselect.new(message:, options: , **opts).run end |
.note(message = "", title: nil, **opts) ⇒ void
This method returns an undefined value.
Display a note box with optional title.
560 561 562 |
# File 'lib/clack.rb', line 560 def note( = "", title: nil, **opts) Note.render(, title: title, **opts) end |
.outro(message = nil, with_guide: nil, output: $stdout) ⇒ void
This method returns an undefined value.
Display an outro banner at the end of a CLI session.
142 143 144 145 146 147 148 149 150 |
# File 'lib/clack.rb', line 142 def outro( = nil, with_guide: nil, output: $stdout) if Core::Settings.with_guide?(with_guide) output.puts Colors.gray(Symbols::S_BAR) output.puts "#{Colors.gray(Symbols::S_BAR_END)} #{}" else output.puts .to_s end output.puts end |
.password(message:, **opts) ⇒ String, CANCEL
Prompt for password input (masked display).
213 214 215 |
# File 'lib/clack.rb', line 213 def password(message:, **opts) Prompts::Password.new(message:, **opts).run end |
.path(message:, **opts) ⇒ String, CANCEL
Prompt for file/directory path with filesystem navigation.
408 409 410 |
# File 'lib/clack.rb', line 408 def path(message:, **opts) Prompts::Path.new(message:, **opts).run end |
.progress(total:, **opts) ⇒ Prompts::Progress
Create a progress bar for measurable operations.
417 418 419 |
# File 'lib/clack.rb', line 417 def progress(total:, **opts) Prompts::Progress.new(total: total, **opts) end |
.range(message:, **opts) ⇒ Numeric, CANCEL
Prompt for a numeric value using a slider.
Navigate with left/right or up/down arrow keys. Press Enter to confirm.
536 537 538 |
# File 'lib/clack.rb', line 536 def range(message:, **opts) Prompts::Range.new(message:, **opts).run end |
.rows(output = $stdout, default: 24) ⇒ Integer
Get terminal rows (height)
662 663 664 |
# File 'lib/clack.rb', line 662 def rows(output = $stdout, default: 24) Environment.rows(output, default: default) end |
.select(message:, options:, **opts) ⇒ Object, CANCEL
Prompt to select one option from a list.
257 258 259 |
# File 'lib/clack.rb', line 257 def select(message:, options:, **opts) Prompts::Select.new(message:, options: , **opts).run end |
.select_key(message:, options:, **opts) ⇒ Object, CANCEL
Prompt to select an option by pressing a key.
Each option carries a single-character :key (defaults to the first
character of the value). Pressing that key selects the option and
submits immediately, so there is no cursor to move. Enter only does
something when :initial_value highlights a default. Option keys win
over custom key aliases (except aliases mapped to :cancel).
460 461 462 |
# File 'lib/clack.rb', line 460 def select_key(message:, options:, **opts) Prompts::SelectKey.new(message:, options: , **opts).run end |
.settings ⇒ Hash
Access global settings
593 594 595 |
# File 'lib/clack.rb', line 593 def settings Core::Settings.config end |
.setup! ⇒ void
This method returns an undefined value.
Install signal handlers for clean terminal cleanup. Call this once in your CLI entry point. Handles INT, TERM, and SIGWINCH. Without calling this, Ctrl+C may leave the cursor hidden.
681 682 683 684 685 686 687 688 |
# File 'lib/clack.rb', line 681 def setup! return if @setup_done @setup_done = true install_signal_handlers install_at_exit Core::Prompt.setup_signal_handler end |
.setup? ⇒ Boolean
Returns whether setup! has been called.
691 |
# File 'lib/clack.rb', line 691 def setup? = !!@setup_done |
.spin(message, success: nil, error: nil, **opts) ⇒ Object
Run a block with a spinner, handling success/error automatically.
If the block does not complete, the spinner still ends with a final line before the
exception or non-local exit propagates: the cancel message for exit (any status),
Ctrl+C, break, and throw; error or the exception message for anything raised.
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 |
# File 'lib/clack.rb', line 340 def spin(, success: nil, error: nil, **opts) s = spinner(**opts) s.start() begin result = yield(s) s.stop(success || ) result rescue SystemExit, SignalException s.cancel raise rescue Exception => exception # standard:disable Lint/RescueException s.error(error || exception.) raise ensure # break or throw out of the block leave no exception in flight; a # no-op when one of the clauses above already finished the spinner. s.abandon end end |
.spinner(**opts) ⇒ Prompts::Spinner
Create an animated spinner for async operations.
302 303 304 |
# File 'lib/clack.rb', line 302 def spinner(**opts) Prompts::Spinner.new(**opts) end |
.stream ⇒ Module
Access the Stream module for streaming output.
550 551 552 |
# File 'lib/clack.rb', line 550 def stream Stream end |
.task_log(title:, **opts) ⇒ TaskLog
Create a streaming task log that clears on success, shows on error. Useful for build output, npm install style streaming, etc.
586 587 588 |
# File 'lib/clack.rb', line 586 def task_log(title:, **opts) TaskLog.new(title: title, **opts) end |
.tasks(tasks:, **opts) ⇒ Array<Hash>
Run multiple tasks with progress indicators.
469 470 471 |
# File 'lib/clack.rb', line 469 def tasks(tasks:, **opts) Prompts::Tasks.new(tasks: tasks, **opts).run end |
.text(message:, **opts) ⇒ String, CANCEL
Prompt for single-line text input.
182 183 184 |
# File 'lib/clack.rb', line 182 def text(message:, **opts) Prompts::Text.new(message:, **opts).run end |
.tty?(output = $stdout) ⇒ Boolean
Check if stdout is a TTY
646 647 648 |
# File 'lib/clack.rb', line 646 def tty?(output = $stdout) Environment.tty?(output) end |
.update_settings(**opts) ⇒ Hash
Update global settings
627 628 629 |
# File 'lib/clack.rb', line 627 def update_settings(**opts) Core::Settings.update(**opts) end |
.warning(message) ⇒ Warning
Create a validation warning that allows the user to proceed with confirmation.
86 87 88 |
# File 'lib/clack.rb', line 86 def warning() Warning.new() end |
.windows? ⇒ Boolean
Check if running on Windows
639 640 641 |
# File 'lib/clack.rb', line 639 def windows? Environment.windows? end |