Class: CobraCommander::Executor::OutputPrompt

Inherits:
Object
  • Object
show all
Defined in:
lib/cobra_commander/executor/output_prompt.rb

Overview

Runs an interactive output printer

Constant Summary collapse

ICONS =
{
  nil => pastel.dim("ā¦»"),
  success: pastel.green("āœ”"),
  skip: pastel.yellow("ā†·"),
  error: pastel.red("āœ–"),
}.freeze
CANCELLED =
pastel.dim("(cancelled)")
BYE =
pastel.bold("\n\nšŸ‘‹ Bye!")

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(jobs) ⇒ OutputPrompt

Returns a new instance of OutputPrompt.



21
22
23
24
# File 'lib/cobra_commander/executor/output_prompt.rb', line 21

def initialize(jobs)
  @jobs = jobs
  @prompt = TTY::Prompt.new(symbols: { cross: " " })
end

Class Method Details

.run(pool, output = $stdout) ⇒ Object



7
8
9
# File 'lib/cobra_commander/executor/output_prompt.rb', line 7

def self.run(pool, output = $stdout)
  new(pool.jobs).run(output)
end

Instance Method Details

#optionsObject



26
27
28
29
30
31
32
33
34
# File 'lib/cobra_commander/executor/output_prompt.rb', line 26

def options
  @options ||= @jobs.map do |job|
    {
      name: format_name(job),
      value: job,
      disabled: !job.resolved? && CANCELLED,
    }
  end
end

#run(output) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/cobra_commander/executor/output_prompt.rb', line 36

def run(output)
  return unless @jobs.any?(&:resolved?)

  selected = nil
  output.puts
  loop do
    selected = @prompt.select("Print output?", options, default: format_name(selected))
    output.puts nil, selected.output, nil
  rescue TTY::Reader::InputInterrupt
    output.puts BYE
    break
  end
end