class CodilityLog::Logger
Constants
- FORMATTER
- QUESTION
- YES_OR_NO
Attributes
Public Class Methods
# File lib/codility_log.rb, line 34 def initialize @terminal = HighLine.new end
Public Instance Methods
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
# File lib/codility_log.rb, line 38 def color(*args) @terminal.color(*args) end
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
Logs informational message using log
, adding INFO: as a header
# File lib/codility_log.rb, line 87 def info(msg) log msg, :info end
# 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
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
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
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
Logs message using log
, coloring it green (except the timestamp)
# File lib/codility_log.rb, line 82 def ok(msg) log msg, :ok end
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
Simple print to STDOUT
# File lib/codility_log.rb, line 63 def say(msg) terminal.say(msg) end
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
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
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
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