Class: ChefApply::UI::Terminal

Inherits:
Object
  • Object
show all
Defined in:
lib/chef_apply/ui/terminal.rb

Defined Under Namespace

Classes: Job

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.locationObject

To support matching in test



46
47
48
# File 'lib/chef_apply/ui/terminal.rb', line 46

def location
  @location
end

Class Method Details

.init(location) ⇒ Object



48
49
50
51
52
53
54
55
# File 'lib/chef_apply/ui/terminal.rb', line 48

def init(location)
  @location = location
  # In Ruby 2.5+ threads print out to stdout when they raise an exception. This is an agressive
  # attempt to ensure debugging information is not lost, but in our case it is not necessary
  # because we handle all the errors ourself. So we disable this to keep output clean.
  # See https://ruby-doc.org/core-2.5.0/Thread.html#method-c-report_on_exception
  Thread.report_on_exception = false
end

.output(msg) ⇒ Object



61
62
63
# File 'lib/chef_apply/ui/terminal.rb', line 61

def output(msg)
  @location.puts msg
end

.render_job(msg, prefix: "", &block) ⇒ Object

TODO update this to accept a job instead of a block, for consistency of usage

between render_job and render_parallel


78
79
80
81
82
83
84
# File 'lib/chef_apply/ui/terminal.rb', line 78

def render_job(msg, prefix: "", &block)
  klass = ChefApply::UI.const_get(ChefApply::Config.dev.spinner)
  spinner = klass.new(spinner_prefix(prefix), output: @location)
  reporter = StatusReporter.new(spinner, prefix: prefix, key: :status)
  reporter.update(msg)
  spinner.run { yield(reporter) }
end

.render_parallel_jobs(header, jobs) ⇒ Object



65
66
67
68
69
70
71
72
73
74
# File 'lib/chef_apply/ui/terminal.rb', line 65

def render_parallel_jobs(header, jobs)
  multispinner = TTY::Spinner::Multi.new("[:spinner] #{header}", output: @location)
  jobs.each do |a|
    multispinner.register(spinner_prefix(a.prefix)) do |spinner|
      reporter = StatusReporter.new(spinner, prefix: a.prefix, key: :status)
      a.run(reporter)
    end
  end
  multispinner.auto_spin
end

.spinner_prefix(prefix) ⇒ Object



86
87
88
89
90
# File 'lib/chef_apply/ui/terminal.rb', line 86

def spinner_prefix(prefix)
  spinner_msg = "[:spinner] "
  spinner_msg += ":prefix " unless prefix.empty?
  spinner_msg + ":status"
end

.write(msg) ⇒ Object



57
58
59
# File 'lib/chef_apply/ui/terminal.rb', line 57

def write(msg)
  @location.write(msg)
end