class CodilityLog::Logger

Constants

FORMATTER
QUESTION
YES_OR_NO

Attributes

terminal[R]

Public Class Methods

new() click to toggle source
# File lib/codility_log.rb, line 34
def initialize
  @terminal = HighLine.new
end

Public Instance Methods

agree(yes_no_question) click to toggle source

Ask user a nicely formatted yes/no question and return appropriate boolean answer.

# File lib/codility_log.rb, line 46
def agree(yes_no_question)
  terminal.agree("#{QUESTION}#{yes_no_question}#{YES_OR_NO}")
end
color(*args) click to toggle source
# File lib/codility_log.rb, line 38
def color(*args)
  @terminal.color(*args)
end
confirm_with(answer) click to toggle source

Ask user to confirm putting uppercased answer (or `cancel` to abort).

# File lib/codility_log.rb, line 58
def confirm_with(answer)
  ask 'Please confirm', answer
end
error(msg) click to toggle source

Logs error message using log, adding ERR: as a header and coloring the message red. The header is aligned with warning and info headers by adding additional spacing.

# File lib/codility_log.rb, line 100
def error(msg)
  log msg, :error
end
info(msg) click to toggle source

Logs informational message using log, adding INFO: as a header

# File lib/codility_log.rb, line 87
def info(msg)
  log msg, :info
end
log(msg, fmt = :default) click to toggle source
# File lib/codility_log.rb, line 67
def log(msg, fmt = :default)
  case fmt
  when Symbol
    formatter = FORMATTER[fmt].dup
  when Hash
    formatter = FORMATTER[:default].dup
    formatter.merge!(fmt)
  else
    raise ArgumentError, "Invalid formatter: #{fmt}"
  end

  say format_message(msg, formatter)
end
log_cmd(msg) click to toggle source

Logs message using log, adding `$ ` header and coloring the message blue. This method is only responsible for custom formatting, `msg` can be any string, that does not necessarily correspond to an actual command.

# File lib/codility_log.rb, line 107
def log_cmd(msg)
  log msg, :cmd
end
log_cmd_with_path(msg, path: Dir.pwd) click to toggle source

Works like log_cmd but also adds the current working dir in []

# File lib/codility_log.rb, line 128
def log_cmd_with_path(msg, path: Dir.pwd)
  log msg, header: "[#{path}] $", color: FORMATTER[:cmd][:color]
end
log_with_space(msg, fmt = :default) click to toggle source

Works like normal log method but adds an empty line before and after the message.

# File lib/codility_log.rb, line 121
def log_with_space(msg, fmt = :default)
  say "\n"
  log msg, fmt
  say "\n"
end
ok(msg) click to toggle source

Logs message using log, coloring it green (except the timestamp)

# File lib/codility_log.rb, line 82
def ok(msg)
  log msg, :ok
end
raise_unless_agreed(question = nil) click to toggle source

Ask user a yes/no question (via agree) and raise an exception if user says no.

# File lib/codility_log.rb, line 52
def raise_unless_agreed(question = nil)
  question ||= 'Proceed [yes] or Abort [no]?'
  raise Aborted, 'Aborting!' unless agree question
end
say(msg) click to toggle source

Simple print to STDOUT

# File lib/codility_log.rb, line 63
def say(msg)
  terminal.say(msg)
end
say_with_space(msg) click to toggle source

Works like normal say method but adds an empty line before and after the message.

# File lib/codility_log.rb, line 113
def say_with_space(msg)
  say "\n"
  say msg
  say "\n"
end
warning(msg) click to toggle source

Logs warning message using log, adding WARN: as a header and coloring the message yellow

# File lib/codility_log.rb, line 93
def warning(msg)
  log msg, :warning
end

Private Instance Methods

format_message(msg, formatter) click to toggle source

Formats message using the given formatter.

Add a whitespace to the header if there is none (and the header isn't empty) to ensure the message is readable. Header should be applied only to the first line, later lines should be aligned with the first one, but have whitespace instead of the header.

# File lib/codility_log.rb, line 141
def format_message(msg, formatter)
  ts = timestamp
  header = formatter[:header] || ''
  header << ' ' unless header[-1] == ' ' || header.length == 0

  ts_length = ts.length
  header_length = header.length

  formatted_msg = msg.split("\n").map.with_index do |line, index|
    ts     = ' ' * ts_length unless index.zero?
    header = ' ' * header_length unless index.zero?
    header = color(header, formatter[:color])
    line   = color(line, formatter[:color])

    "#{ts} #{header}#{line}\n"
  end

  formatted_msg.join
end
timestamp() click to toggle source

Returns current UTC time in format: HH:MM:SS

# File lib/codility_log.rb, line 162
def timestamp
  Time.now.utc.strftime '%T UTC'
end