class MotherBrain::CliClient
A class for encapsulating view behavior for a user of the CLI.
@example
job = MotherBrain::Job.new CliClient.new(job).display
Constants
- SPACE
- TICK
Attributes
current_status[RW]
job[R]
last_spun[RW]
Public Class Methods
new(job)
click to toggle source
@param [MotherBrain::Job] job
# File lib/mb/cli_client.rb, line 17 def initialize(job) @job = job end
Public Instance Methods
display()
click to toggle source
Block and wait for all jobs to be completed, while displaying the status of each job.
# File lib/mb/cli_client.rb, line 23 def display if debugging? wait_for_jobs else display_jobs end if job_failed? display_log_location if log_location abort end end
Private Instance Methods
application_terminated?()
click to toggle source
# File lib/mb/cli_client.rb, line 38 def application_terminated? JobManager.stopped? end
clear_line()
click to toggle source
# File lib/mb/cli_client.rb, line 42 def clear_line printf "\r#{SPACE * terminal_width}" end
debugging?()
click to toggle source
@return [Boolean]
# File lib/mb/cli_client.rb, line 47 def debugging? MB.log.info? end
display_jobs()
click to toggle source
# File lib/mb/cli_client.rb, line 55 def display_jobs until job_completed? || application_terminated? print_statuses sleep TICK end if application_terminated? print_final_terminated_status else print_final_status end end
display_log_location()
click to toggle source
# File lib/mb/cli_client.rb, line 51 def display_log_location puts "#{left_space} [motherbrain] Log written to #{log_location}" end
job_completed?()
click to toggle source
# File lib/mb/cli_client.rb, line 68 def job_completed? job.completed? end
job_failed?()
click to toggle source
# File lib/mb/cli_client.rb, line 72 def job_failed? job.failed? end
job_type()
click to toggle source
# File lib/mb/cli_client.rb, line 76 def job_type @job_type || job.type end
left_space()
click to toggle source
# File lib/mb/cli_client.rb, line 97 def left_space SPACE * spinner.peek.length end
log_location()
click to toggle source
# File lib/mb/cli_client.rb, line 80 def log_location MB::Logging.filename end
print_final_status()
click to toggle source
# File lib/mb/cli_client.rb, line 84 def print_final_status print_with_new_line current_status msg = "#{left_space} [#{job_type}] #{job.state.to_s.capitalize}" msg << ": #{job.result}" if job.result puts msg end
print_final_terminated_status()
click to toggle source
# File lib/mb/cli_client.rb, line 93 def print_final_terminated_status puts "\nmotherbrain terminated" end
print_statuses()
click to toggle source
# File lib/mb/cli_client.rb, line 101 def print_statuses last_status = status_buffer.pop if last_status if current_status && last_spun != current_status print_with_new_line current_status end self.current_status = last_status end while status = status_buffer.shift print_with_new_line status end print_with_spinner current_status end
print_with_new_line(text)
click to toggle source
# File lib/mb/cli_client.rb, line 133 def print_with_new_line(text) return unless text clear_line printf "\r#{left_space} [#{job_type}] #{text}\n" end
print_with_spinner(text)
click to toggle source
# File lib/mb/cli_client.rb, line 119 def print_with_spinner(text) return unless text string = "\r#{spinner.next} [#{job_type}] #{text}" if string.length < terminal_width clear_line printf string else print_with_new_line(text) unless text == last_spun self.last_spun = text end end
spinner()
click to toggle source
@return [Enumerator]
# File lib/mb/cli_client.rb, line 142 def spinner @spinner ||= Enumerator.new { |enumerator| characters = if ENV['SPINNER'] == 'kirby' kirby = ["(>' ')>", "(^' ')^", "<(' '<)", "^(' '^)"] frames = [0, 0, 0, 1, 2, 2, 2, 3].inject([]) { |frames, frame| frames + [frame]*3 } buffer = [0, 1, 2, 3, 3, 2, 1, 0].inject([]) { |frames, frame| frames + [frame]*3 } buffer_max = buffer.max frames.each_with_index.collect do |frame, i| left = " " * buffer[i] right = " " * (buffer_max - buffer[i]) left + kirby[frame] + right end else %w[` ' - . , . - '] end loop { characters.each do |character| enumerator.yield character end } } end
status_buffer()
click to toggle source
# File lib/mb/cli_client.rb, line 166 def status_buffer job.status_buffer end
terminal_width()
click to toggle source
# File lib/mb/cli_client.rb, line 170 def terminal_width @terminal_width ||= `tput cols`.to_i end
wait_for_jobs()
click to toggle source
# File lib/mb/cli_client.rb, line 174 def wait_for_jobs sleep TICK until job_completed? || application_terminated? end