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 Attribute Summary collapse

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 Attribute Details

#enabledBoolean (readonly)

Returns whether the task should run (default: true).

Returns:

  • (Boolean)

    whether the task should run (default: true)



54
# File 'lib/clack/prompts/tasks.rb', line 54

Task = Data.define(:title, :task, :enabled)

#errorString? (readonly)

Returns error message if failed.

Returns:

  • (String, nil)

    error message if failed



64
# File 'lib/clack/prompts/tasks.rb', line 64

TaskResult = Data.define(:title, :status, :error)

#statusSymbol (readonly)

Returns :success or :error.

Returns:

  • (Symbol)

    :success or :error



64
# File 'lib/clack/prompts/tasks.rb', line 64

TaskResult = Data.define(:title, :status, :error)

#taskProc (readonly)

Returns the task to execute.

Returns:

  • (Proc)

    the task to execute



54
# File 'lib/clack/prompts/tasks.rb', line 54

Task = Data.define(:title, :task, :enabled)

#titleString (readonly)

Returns the task title.

Returns:

  • (String)

    the task title



54
# File 'lib/clack/prompts/tasks.rb', line 54

Task = Data.define(:title, :task, :enabled)

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