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
print_final_terminated_status() click to toggle source
print_statuses() click to toggle source
print_with_new_line(text) click to toggle source
print_with_spinner(text) click to toggle source
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