# File lib/foreman_maintain/reporter/cli_reporter.rb, line 76 def initialize(stdout = STDOUT, stdin = STDIN, options = {}) @stdout = stdout @stdin = stdin options.validate_options!(:assumeyes) @assumeyes = options.fetch(:assumeyes, false) @hl = HighLine.new(@stdin, @stdout) @max_length = 80 @line_char = '-' @cell_char = '|' @spinner = Spinner.new(self) @last_line = '' end
# File lib/foreman_maintain/reporter/cli_reporter.rb, line 147 def after_execution_finishes(execution) puts_status(execution.status) puts(execution.output) unless execution.output.empty? hline new_line_if_needed end
# File lib/foreman_maintain/reporter/cli_reporter.rb, line 154 def after_scenario_finishes(_scenario); end
# File lib/foreman_maintain/reporter/cli_reporter.rb, line 117 def ask(message, options = {}) new_line_if_needed options.validate_options!(:password) # the answer is confirmed by ENTER which will emit a new line @new_line_next_time = false @last_line = '' # add space at the end as otherwise highline would add new line there :/ message = "#{message} " unless message =~ /\s\Z/ answer = @hl.ask(message) { |q| q.echo = false if options[:password] } answer.to_s.chomp.downcase if answer end
# File lib/foreman_maintain/reporter/cli_reporter.rb, line 94 def before_execution_starts(execution) puts(execution_info(execution, '')) end
# File lib/foreman_maintain/reporter/cli_reporter.rb, line 89 def before_scenario_starts(scenario) puts "\nRunning #{scenario.description || scenario.class}" hline('=') end
# File lib/foreman_maintain/reporter/cli_reporter.rb, line 165 def clear_line print "\r" + ' ' * @max_length + "\r" end
# File lib/foreman_maintain/reporter/cli_reporter.rb, line 129 def new_line_if_needed if @new_line_next_time @stdout.print("\n") @stdout.flush @new_line_next_time = false end end
# File lib/foreman_maintain/reporter/cli_reporter.rb, line 156 def on_next_steps(steps) return if steps.empty? if steps.size > 1 multiple_steps_decision(steps) else single_step_decision(steps.first) end end
# File lib/foreman_maintain/reporter/cli_reporter.rb, line 98 def print(string) new_line_if_needed @stdout.print(string) @stdout.flush record_last_line(string) end
# File lib/foreman_maintain/reporter/cli_reporter.rb, line 105 def puts(string) # we don't print the new line right away, as we want to be able to put # the status label at the end of the last line, if possible. # Therefore, we just mark that we need to print the new line next time # we are printing something. new_line_if_needed @stdout.print(string) @stdout.flush @new_line_next_time = true record_last_line(string) end
# File lib/foreman_maintain/reporter/cli_reporter.rb, line 137 def with_spinner(message) new_line_if_needed @spinner.activate @spinner.update(message) yield @spinner ensure @spinner.deactivate @new_line_next_time = true end
# File lib/foreman_maintain/reporter/cli_reporter.rb, line 192 def ask_decision(message) if assumeyes? print("#{message} (assuming yes)") return :yes end until_valid_decision do filter_decision(ask("#{message}, [y(yes), n(no), q(quit)]")) end ensure clear_line end
rubocop:disable Metrics/MethodLength,Metrics/AbcSize
# File lib/foreman_maintain/reporter/cli_reporter.rb, line 213 def ask_to_select(message, steps) if assumeyes? puts('(assuming first option)') return steps.first end until_valid_decision do answer = ask("#{message}, [n(next), q(quit)]") if answer =~ /^\d+$/ && (answer.to_i - 1) < steps.size steps[answer.to_i - 1] else decision = filter_decision(answer) if decision == :yes steps.first else decision end end end ensure clear_line end
# File lib/foreman_maintain/reporter/cli_reporter.rb, line 171 def assumeyes? @assumeyes end
# File lib/foreman_maintain/reporter/cli_reporter.rb, line 242 def execution_info(execution, text) prefix = "#{execution.name}:" "#{prefix} #{text}" end
# File lib/foreman_maintain/reporter/cli_reporter.rb, line 204 def filter_decision(answer) decision = nil DECISION_MAPPER.each do |options, decision_label| decision = decision_label if options.include?(answer) end decision end
# File lib/foreman_maintain/reporter/cli_reporter.rb, line 268 def hline(line_char = @line_char) puts line_char * @max_length end
# File lib/foreman_maintain/reporter/cli_reporter.rb, line 184 def multiple_steps_decision(steps) puts 'There are multiple steps to proceed:' steps.each_with_index do |step, index| puts "#{index + 1}) #{step.description}" end ask_to_select('Select step to continue', steps, &:description) end
# File lib/foreman_maintain/reporter/cli_reporter.rb, line 247 def puts_status(status) label_offset = 10 padding = @max_length - @last_line.to_s.size - label_offset if padding < 0 new_line_if_needed padding = @max_length - label_offset end @stdout.print(' ' * padding + status_label(status)) @new_line_next_time = true end
# File lib/foreman_maintain/reporter/cli_reporter.rb, line 272 def record_last_line(string) @last_line = string.lines.to_a.last end
# File lib/foreman_maintain/reporter/cli_reporter.rb, line 175 def single_step_decision(step) answer = ask_decision("Continue with step [#{step.description}]?") if answer == :yes step else answer end end
# File lib/foreman_maintain/reporter/cli_reporter.rb, line 258 def status_label(status) mapping = { :success => { :label => '[OK]', :color => :green }, :fail => { :label => '[FAIL]', :color => :red }, :running => { :label => '[RUNNING]', :color => :blue }, :skipped => { :label => '[SKIPPED]', :color => :yellow }, :warning => { :label => '[WARNING]', :color => :yellow } } properties = mapping[status] @hl.color(properties[:label], properties[:color], :bold) end
loop over the block until it returns some non-false value
# File lib/foreman_maintain/reporter/cli_reporter.rb, line 236 def until_valid_decision decision = nil decision = yield until decision decision end