Class: Clack::Prompts::Tasks

Inherits:
Object
  • Object
show all
Defined in:
lib/clack/prompts/tasks.rb

Overview

Sequential task runner with spinner animation.

Runs tasks in order, showing a spinner while each runs. Displays success/error status after each task completes.

Each task is a hash with:

  • :title - display title
  • :task - Proc to execute (exceptions are caught). Optionally accepts a message-update callable to change the spinner message mid-execution.
  • :enabled - optional boolean (default true). When false, the task is skipped entirely.

Examples:

Basic usage

results = Clack.tasks(tasks: [
  { title: "Checking dependencies", task: -> { check_deps } },
  { title: "Building project", task: -> { build } },
  { title: "Running tests", task: -> { run_tests } }
])

Updating spinner message mid-task

Clack.tasks(tasks: [
  { title: "Installing", task: ->(message) {
    message.call("Step 1..."); step1
    message.call("Step 2..."); step2
  }}
])

Conditionally skipping tasks

Clack.tasks(tasks: [
  { title: "Deploy", task: -> { deploy }, enabled: ENV["DEPLOY"] == "true" }
])

Checking results

results.each do |r|
  if r.status == :error
    puts "#{r.title} failed: #{r.error}"
  end
end

Defined Under Namespace

Classes: Task, TaskResult

Instance Method Summary collapse

Constructor Details

#initialize(tasks:, output: $stdout) ⇒ Tasks

Returns a new instance of Tasks.

Parameters:

  • tasks (Array<Hash>)

    tasks with :title, :task, and optional :enabled keys

  • output (IO) (defaults to: $stdout)

    output stream (default: $stdout)



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/clack/prompts/tasks.rb', line 68

def initialize(tasks:, output: $stdout)
  @tasks = tasks.map do |task_data|
    Task.new(
      title: task_data[:title],
      task: task_data[:task],
      enabled: task_data.fetch(:enabled, true)
    )
  end
  @output = output
  @results = []
end

Instance Method Details

#runArray<TaskResult>

Run all tasks sequentially.

Returns:



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/clack/prompts/tasks.rb', line 83

def run
  @output.print Core::Cursor.hide
  @tasks.each do |task|
    next unless task.enabled

    run_task(task)
  end
  @results
ensure
  begin
    @output.print Core::Cursor.show
  rescue IOError, SystemCallError
    # output stream already closed
  end
end