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/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/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, 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.6.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, 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 = "Cancelled", output: $stdout) ⇒ Boolean
Check if cancelled and show cancel message if so.
-
.intro(title = 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, 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.
283 284 285 |
# File 'lib/clack.rb', line 283 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.
299 300 301 |
# File 'lib/clack.rb', line 299 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.
422 423 424 |
# File 'lib/clack.rb', line 422 def box( = "", title: "", **opts) Box.render(, title: title, **opts) end |
.cancel(message = nil, output: $stdout) ⇒ void
This method returns an undefined value.
Display a cancellation message (typically after user presses Escape).
146 147 148 149 150 |
# File 'lib/clack.rb', line 146 def cancel( = nil, output: $stdout) output.puts Colors.gray(Symbols::S_BAR) output.puts "#{Colors.gray(Symbols::S_BAR_END)} #{Colors.red()}" output.puts end |
.cancel?(value) ⇒ Boolean Also known as: cancelled?
Check if a prompt result was cancelled by the user.
92 93 94 |
# File 'lib/clack.rb', line 92 def cancel?(value) value.equal?(CANCEL) end |
.ci? ⇒ Boolean
Check if running in a CI environment
467 468 469 |
# File 'lib/clack.rb', line 467 def ci? Environment.ci? end |
.columns(output = $stdout, default: 80) ⇒ Integer
Get terminal columns (width)
488 489 490 |
# File 'lib/clack.rb', line 488 def columns(output = $stdout, default: 80) Environment.columns(output, default: default) end |
.confirm(message:, **opts) ⇒ Boolean, CANCEL
Prompt for yes/no confirmation.
199 200 201 |
# File 'lib/clack.rb', line 199 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.
367 368 369 |
# File 'lib/clack.rb', line 367 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.
504 505 506 507 508 |
# File 'lib/clack.rb', line 504 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.
350 351 352 |
# File 'lib/clack.rb', line 350 def group_multiselect(message:, options:, **opts) Prompts::GroupMultiselect.new(message:, options: , **opts).run end |
.handle_cancel(value, message = "Cancelled", output: $stdout) ⇒ Boolean
Check if cancelled and show cancel message if so. Useful for guard clauses in CLI scripts.
114 115 116 117 118 119 |
# File 'lib/clack.rb', line 114 def handle_cancel(value, = "Cancelled", output: $stdout) return false unless cancel?(value) cancel(, output: output) true end |
.intro(title = nil, output: $stdout) ⇒ void
This method returns an undefined value.
Display an intro banner at the start of a CLI session.
126 127 128 |
# File 'lib/clack.rb', line 126 def intro(title = nil, output: $stdout) output.puts "#{Colors.gray(Symbols::S_BAR_START)} #{title}" end |
.log ⇒ Module
Access the Log module for styled console output.
393 394 395 |
# File 'lib/clack.rb', line 393 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.
177 178 179 |
# File 'lib/clack.rb', line 177 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.
223 224 225 |
# File 'lib/clack.rb', line 223 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.
409 410 411 |
# File 'lib/clack.rb', line 409 def note( = "", title: nil, **opts) Note.render(, title: title, **opts) end |
.outro(message = nil, output: $stdout) ⇒ void
This method returns an undefined value.
Display an outro banner at the end of a CLI session.
135 136 137 138 139 |
# File 'lib/clack.rb', line 135 def outro( = nil, output: $stdout) output.puts Colors.gray(Symbols::S_BAR) output.puts "#{Colors.gray(Symbols::S_BAR_END)} #{}" output.puts end |
.password(message:, **opts) ⇒ String, CANCEL
Prompt for password input (masked display).
188 189 190 |
# File 'lib/clack.rb', line 188 def password(message:, **opts) Prompts::Password.new(message:, **opts).run end |
.path(message:, **opts) ⇒ String, CANCEL
Prompt for file/directory path with filesystem navigation.
310 311 312 |
# File 'lib/clack.rb', line 310 def path(message:, **opts) Prompts::Path.new(message:, **opts).run end |
.progress(total:, **opts) ⇒ Prompts::Progress
Create a progress bar for measurable operations.
319 320 321 |
# File 'lib/clack.rb', line 319 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.
386 387 388 |
# File 'lib/clack.rb', line 386 def range(message:, **opts) Prompts::Range.new(message:, **opts).run end |
.rows(output = $stdout, default: 24) ⇒ Integer
Get terminal rows (height)
496 497 498 |
# File 'lib/clack.rb', line 496 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.
210 211 212 |
# File 'lib/clack.rb', line 210 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.
328 329 330 |
# File 'lib/clack.rb', line 328 def select_key(message:, options:, **opts) Prompts::SelectKey.new(message:, options: , **opts).run end |
.settings ⇒ Hash
Access global settings
440 441 442 |
# File 'lib/clack.rb', line 440 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.
515 516 517 518 519 520 521 522 |
# File 'lib/clack.rb', line 515 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.
525 |
# File 'lib/clack.rb', line 525 def setup? = !!@setup_done |
.spin(message, success: nil, error: nil, **opts) ⇒ Object
Run a block with a spinner, handling success/error automatically.
260 261 262 263 264 265 266 267 268 269 270 271 |
# File 'lib/clack.rb', line 260 def spin(, success: nil, error: nil, **opts) s = spinner(**opts) s.start() begin result = yield(s) s.stop(success || ) result rescue => exception s.error(error || exception.) raise end end |
.spinner(**opts) ⇒ Prompts::Spinner
Create an animated spinner for async operations.
235 236 237 |
# File 'lib/clack.rb', line 235 def spinner(**opts) Prompts::Spinner.new(**opts) end |
.stream ⇒ Module
Access the Stream module for streaming output.
400 401 402 |
# File 'lib/clack.rb', line 400 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.
433 434 435 |
# File 'lib/clack.rb', line 433 def task_log(title:, **opts) TaskLog.new(title: title, **opts) end |
.tasks(tasks:, **opts) ⇒ Array<Hash>
Run multiple tasks with progress indicators.
336 337 338 |
# File 'lib/clack.rb', line 336 def tasks(tasks:, **opts) Prompts::Tasks.new(tasks: tasks, **opts).run end |
.text(message:, **opts) ⇒ String, CANCEL
Prompt for single-line text input.
163 164 165 |
# File 'lib/clack.rb', line 163 def text(message:, **opts) Prompts::Text.new(message:, **opts).run end |
.tty?(output = $stdout) ⇒ Boolean
Check if stdout is a TTY
480 481 482 |
# File 'lib/clack.rb', line 480 def tty?(output = $stdout) Environment.tty?(output) end |
.update_settings(**opts) ⇒ Hash
Update global settings
461 462 463 |
# File 'lib/clack.rb', line 461 def update_settings(**opts) Core::Settings.update(**opts) end |
.warning(message) ⇒ Warning
Create a validation warning that allows the user to proceed with confirmation.
84 85 86 |
# File 'lib/clack.rb', line 84 def warning() Warning.new() end |
.windows? ⇒ Boolean
Check if running on Windows
473 474 475 |
# File 'lib/clack.rb', line 473 def windows? Environment.windows? end |