Class: ChefApply::UI::Terminal

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

Defined Under Namespace

Classes: Job

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.locationObject

To support matching in test



30
31
32
# File 'lib/chef_apply/ui/terminal.rb', line 30

def location
  @location
end

Class Method Details

.get_multispinnerObject



84
85
86
# File 'lib/chef_apply/ui/terminal.rb', line 84

def get_multispinner
  ChefApply::Config.dev.spinner ? TTY::Spinner::Multi : PlainTextHeader
end

.get_spinnerObject



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

def get_spinner
  ChefApply::Config.dev.spinner ? TTY::Spinner : PlainTextElement
end

.init(location) ⇒ Object



32
33
34
# File 'lib/chef_apply/ui/terminal.rb', line 32

def init(location)
  @location = location
end

.output(msg) ⇒ Object



40
41
42
# File 'lib/chef_apply/ui/terminal.rb', line 40

def output(msg)
  @location.puts msg
end

.render_job(initial_msg, job) ⇒ Object



69
70
71
72
73
74
75
76
# File 'lib/chef_apply/ui/terminal.rb', line 69

def render_job(initial_msg, job)
  # TODO why do we have to pass prefix to both the spinner and the reporter?
  spinner = get_spinner.new(spinner_prefix(job.prefix), output: @location, hide_cursor: true)
  reporter = StatusReporter.new(spinner, prefix: job.prefix, key: :status)
  reporter.update(initial_msg)
  spinner.auto_spin
  job.run(reporter)
end

.render_parallel_jobs(header, jobs) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/chef_apply/ui/terminal.rb', line 44

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 = get_multispinner.new("[:spinner] #{header}",
    output: @location,
    hide_cursor: true,
    style: indent_style)
  jobs.each do |job|
    multispinner.register(spinner_prefix(job.prefix), hide_cursor: true) do |spinner|
      reporter = StatusReporter.new(spinner, prefix: job.prefix, key: :status)
      job.run(reporter)
    end
  end
  multispinner.auto_spin
ensure
  # Spinners hide the cursor for better appearance, so we need to make sure
  # we always bring it back
  show_cursor
end

.show_cursorObject



92
93
94
# File 'lib/chef_apply/ui/terminal.rb', line 92

def show_cursor
  TTY::Cursor.show
end

.spinner_prefix(prefix) ⇒ Object



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

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

.write(msg) ⇒ Object



36
37
38
# File 'lib/chef_apply/ui/terminal.rb', line 36

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