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
# File 'lib/chef_apply/ui/terminal.rb', line 48

def init(location)
  @location = location
end

.output(msg) ⇒ Object



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

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


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

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

.render_parallel_jobs(header, jobs) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/chef_apply/ui/terminal.rb', line 60

def render_parallel_jobs(header, jobs)
      # Do not indent the topmost 'parent' spinner, but do indent child spinners
  indent_style = { top: "",
                   middle: TTY::Spinner::Multi::DEFAULT_INSET[:middle],
                   bottom: TTY::Spinner::Multi::DEFAULT_INSET[:bottom] }
      # @option options [Hash] :style
      #   keys :top :middle and :bottom can contain Strings that are used to
      #   indent the spinners. Ignored if message is blank
  multispinner = TTY::Spinner::Multi.new("[:spinner] #{header}", output: @location, hide_cursor: true, style: indent_style)
  jobs.each do |a|
    multispinner.register(spinner_prefix(a.prefix), hide_cursor: true) do |spinner|
      reporter = StatusReporter.new(spinner, prefix: a.prefix, key: :status)
      a.run(reporter)
    end
  end
  multispinner.auto_spin
end

.spinner_prefix(prefix) ⇒ Object



88
89
90
91
92
# File 'lib/chef_apply/ui/terminal.rb', line 88

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

.write(msg) ⇒ Object



52
53
54
# File 'lib/chef_apply/ui/terminal.rb', line 52

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