class Crew::Logger

Public Class Methods

new(mute = false) click to toggle source
# File lib/crew/logger.rb, line 3
def initialize(mute = false)
  @indent = 0
  @mute = mute
  writer.sync = true
end

Public Instance Methods

cd(dir) click to toggle source
# File lib/crew/logger.rb, line 27
def cd(dir)
  log "cd #{dir}".color("#aaaaaa")
end
context(name) { || ... } click to toggle source
# File lib/crew/logger.rb, line 97
def context(name)
  log "--> [#{name.color(:cyan)}] starting"
  start_time = Time.new.to_f
  indent do
    yield
  end
  end_time = Time.new.to_f
  log "<-- [#{name.color(:cyan)}] ended in %0.2f seconds" % [end_time - start_time]
end
fail_test(name) click to toggle source
# File lib/crew/logger.rb, line 89
def fail_test(name)
  log "#{'✘'.color(:red)} Failing test #{name.inverse}"
end
indent() { || ... } click to toggle source
# File lib/crew/logger.rb, line 116
def indent
  @indent += 1
  yield
ensure
  @indent -= 1
end
info(text) click to toggle source
# File lib/crew/logger.rb, line 19
def info(text)
  log text
end
log(line) click to toggle source
# File lib/crew/logger.rb, line 107
def log(line)
  writer.write "  " * @indent
  writer.puts line
end
muted() { || ... } click to toggle source
# File lib/crew/logger.rb, line 9
def muted
  original_mute = @mute
  @mute = true
  begin
    yield
  ensure
    @mute = original_mute
  end
end
no_test(name) click to toggle source
# File lib/crew/logger.rb, line 93
def no_test(name)
  log "#{'≈'.color(:blue)} No tests for #{name}"
end
pass_test(name) click to toggle source
# File lib/crew/logger.rb, line 81
def pass_test(name)
  log "#{'✓'.color(:green)} Passed test #{name.inverse}"
end
set_logging(enabled) click to toggle source
# File lib/crew/logger.rb, line 112
def set_logging(enabled)
  @logging = enabled
end
sh(command) click to toggle source
# File lib/crew/logger.rb, line 23
def sh(command)
  log "#{'$'.color(:blue)} #{command}"
end
skip_test(name) click to toggle source
# File lib/crew/logger.rb, line 85
def skip_test(name)
  log "#{'/'.color(:yellow)} Skipping test #{name.inverse}"
end
spinner(label) click to toggle source
# File lib/crew/logger.rb, line 31
def spinner(label)
  muted do
    $stderr.write "  " * @indent
    index = 0
    states = %w(◴ ◷ ◶ ◵)
    spin = proc do |val|
      if index > 0
        $stderr.write "\b" * (label.size + 2)
      end
      if val
        $stderr.write "* ".color(:green)
        $stderr.puts label
      else
        $stderr.write states[index % states.size].color(:yellow)
        $stderr.write " #{label}"
        index += 1
      end
    end
    spin[false]
    spin
  end
end
task(command, args) { || ... } click to toggle source
# File lib/crew/logger.rb, line 54
def task(command, args)
  log "#{command} #{args.to_s}".color("#aaaaaa")
  out = nil
  begin
    indent do
      out = yield
    end
    pass_line = "#{'✓'.color(:green)} #{command}"
    pass_line << " #{'# =>'.color(:magenta)} #{out.inspect}"
    log pass_line
    out
  rescue AssertionError
    log "#{'✘'.color(:red)} #{command}"
    raise
  rescue => e
    log "#{'!!!'.color(:blue)} #{command} ([#{e.class}] #{e.message})"
    raise
  end
end
test(name) { || ... } click to toggle source
# File lib/crew/logger.rb, line 74
def test(name)
  log "Testing #{name.inverse}"
  indent do
    yield
  end
end
writer() click to toggle source
# File lib/crew/logger.rb, line 123
def writer
  @mute ? StringIO.new : $stderr
end