class Airbrussh::ConsoleFormatter

Attributes

config[R]
context[R]
last_printed_task[RW]

Public Class Methods

new(io, config=Airbrussh.configuration) click to toggle source
Calls superclass method
# File lib/airbrussh/console_formatter.rb, line 15
def initialize(io, config=Airbrussh.configuration)
  super(io)

  @config = config
  @context = config.context.new(config)
  @console = Airbrussh::Console.new(original_output, config)

  write_banner
end

Public Instance Methods

<<(obj)
Alias for: write
log_command_data(command, stream_type, string) click to toggle source
# File lib/airbrussh/console_formatter.rb, line 37
def log_command_data(command, stream_type, string)
  return if debug?(command)
  return unless config.show_command_output?(stream_type)
  command = decorate(command)
  string.each_line do |line|
    print_indented_line(command.format_output(line))
  end
end
log_command_exit(command) click to toggle source
# File lib/airbrussh/console_formatter.rb, line 46
def log_command_exit(command)
  return if debug?(command)
  command = decorate(command)
  print_indented_line(command.exit_message, -2)
end
log_command_start(command) click to toggle source
# File lib/airbrussh/console_formatter.rb, line 29
def log_command_start(command)
  return if debug?(command)
  first_execution = register_new_command(command)
  command = decorate(command)
  print_task_if_changed
  print_indented_line(command.start_message) if first_execution
end
write(obj) click to toggle source
# File lib/airbrussh/console_formatter.rb, line 52
def write(obj)
  case obj
  when SSHKit::Command
    log_command_start(obj)
    log_and_clear_command_output(obj, :stderr)
    log_and_clear_command_output(obj, :stdout)
    log_command_exit(obj) if obj.finished?
  when SSHKit::LogMessage
    write_log_message(obj)
  end
end
Also aliased as: <<
write_banner() click to toggle source
# File lib/airbrussh/console_formatter.rb, line 25
def write_banner
  print_line(config.banner_message) if config.banner_message
end

Private Instance Methods

clock() click to toggle source
# File lib/airbrussh/console_formatter.rb, line 106
def clock
  @start_at ||= Time.now
  duration = Time.now - @start_at

  minutes = (duration / 60).to_i
  seconds = (duration - minutes * 60).to_i

  format("%02d:%02d", minutes, seconds)
end
debug?(obj) click to toggle source
# File lib/airbrussh/console_formatter.rb, line 116
def debug?(obj)
  obj.verbosity <= SSHKit::Logger::DEBUG
end
decorate(command) click to toggle source
# File lib/airbrussh/console_formatter.rb, line 120
def decorate(command)
  Airbrussh::CommandFormatter.new(command, @context.position(command))
end
format_log_message(log_message) click to toggle source
# File lib/airbrussh/console_formatter.rb, line 75
def format_log_message(log_message)
  case log_message.verbosity
  when SSHKit::Logger::WARN
    "#{yellow('WARN')}  #{log_message}"
  when SSHKit::Logger::ERROR
    "#{red('ERROR')} #{log_message}"
  when SSHKit::Logger::FATAL
    "#{red('FATAL')} #{log_message}"
  else
    log_message.to_s
  end
end
log_and_clear_command_output(command, stream) click to toggle source

For SSHKit versions up to and including 1.7.1, the stdout and stderr output was available as attributes on the Command. Print the data for the specified command and stream if enabled and clear the stream. (see Airbrussh::Configuration#command_output).

# File lib/airbrussh/console_formatter.rb, line 92
def log_and_clear_command_output(command, stream)
  output = command.public_send(stream)
  log_command_data(command, stream, output)
  command.public_send("#{stream}=", "")
end
print_indented_line(string, offset=0) click to toggle source
print_line(string) click to toggle source
print_task_if_changed() click to toggle source
write_log_message(log_message) click to toggle source
# File lib/airbrussh/console_formatter.rb, line 69
def write_log_message(log_message)
  return if debug?(log_message)
  print_task_if_changed
  print_indented_line(format_log_message(log_message))
end