Class: CodilityLog::Logger
- Inherits:
-
Object
- Object
- CodilityLog::Logger
- Defined in:
- lib/codility_log.rb
Defined Under Namespace
Classes: Aborted
Constant Summary collapse
- QUESTION =
HighLine.color('¿ ', :white, :bold).freeze
- YES_OR_NO =
[ ' (', HighLine.color('yes', :underscore), '/', HighLine.color('no', :underscore), ') ', ].join.freeze
- FORMATTER =
{ cmd: { header: '$ ', color: :blue }, error: { header: 'ERR: ', color: :red }, warning: { header: 'WARN: ', color: :yellow }, info: { header: 'INFO: ' }, ok: { color: :green }, default: { header: '' }, }.freeze
Instance Attribute Summary collapse
-
#terminal ⇒ Object
readonly
Returns the value of attribute terminal.
Instance Method Summary collapse
-
#agree(yes_no_question) ⇒ Object
Ask user a nicely formatted yes/no question and return appropriate boolean answer.
- #color(*args) ⇒ Object
-
#confirm_with(answer) ⇒ Object
Ask user to confirm putting uppercased answer (or ‘cancel` to abort).
-
#error(msg) ⇒ Object
Logs error message using #log, adding ERR: as a header and coloring the message red.
-
#info(msg) ⇒ Object
Logs informational message using #log, adding INFO: as a header.
-
#initialize ⇒ Logger
constructor
A new instance of Logger.
- #log(msg, fmt = :default) ⇒ Object
-
#log_cmd(msg) ⇒ Object
Logs message using #log, adding ‘$ ` header and coloring the message blue.
-
#log_cmd_with_path(msg, path: Dir.pwd) ⇒ Object
Works like #log_cmd but also adds the current working dir in [].
-
#log_with_space(msg, fmt = :default) ⇒ Object
Works like normal #log method but adds an empty line before and after the message.
-
#ok(msg) ⇒ Object
Logs message using #log, coloring it green (except the timestamp).
-
#raise_unless_agreed(question = nil) ⇒ Object
Ask user a yes/no question (via #agree) and raise an exception if user says no.
-
#say(msg) ⇒ Object
Simple print to STDOUT.
-
#say_with_space(msg) ⇒ Object
Works like normal #say method but adds an empty line before and after the message.
-
#warning(msg) ⇒ Object
Logs warning message using #log, adding WARN: as a header and coloring the message yellow.
Constructor Details
#initialize ⇒ Logger
Returns a new instance of Logger.
34 35 36 |
# File 'lib/codility_log.rb', line 34 def initialize @terminal = HighLine.new end |
Instance Attribute Details
#terminal ⇒ Object (readonly)
Returns the value of attribute terminal.
32 33 34 |
# File 'lib/codility_log.rb', line 32 def terminal @terminal end |
Instance Method Details
#agree(yes_no_question) ⇒ Object
Ask user a nicely formatted yes/no question and return appropriate boolean answer. – FIXME: this should be named #agree?
46 47 48 |
# File 'lib/codility_log.rb', line 46 def agree(yes_no_question) terminal.agree("#{QUESTION}#{yes_no_question}#{YES_OR_NO}") end |
#color(*args) ⇒ Object
38 39 40 |
# File 'lib/codility_log.rb', line 38 def color(*args) @terminal.color(*args) end |
#confirm_with(answer) ⇒ Object
Ask user to confirm putting uppercased answer (or ‘cancel` to abort).
58 59 60 |
# File 'lib/codility_log.rb', line 58 def confirm_with(answer) ask 'Please confirm', answer end |
#error(msg) ⇒ Object
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.
100 101 102 |
# File 'lib/codility_log.rb', line 100 def error(msg) log msg, :error end |
#info(msg) ⇒ Object
Logs informational message using #log, adding INFO: as a header
87 88 89 |
# File 'lib/codility_log.rb', line 87 def info(msg) log msg, :info end |
#log(msg, fmt = :default) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 |
# 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 (msg, formatter) end |
#log_cmd(msg) ⇒ Object
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.
107 108 109 |
# File 'lib/codility_log.rb', line 107 def log_cmd(msg) log msg, :cmd end |
#log_cmd_with_path(msg, path: Dir.pwd) ⇒ Object
Works like #log_cmd but also adds the current working dir in []
128 129 130 |
# 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) ⇒ Object
Works like normal #log method but adds an empty line before and after the message.
121 122 123 124 125 |
# File 'lib/codility_log.rb', line 121 def log_with_space(msg, fmt = :default) say "\n" log msg, fmt say "\n" end |
#ok(msg) ⇒ Object
Logs message using #log, coloring it green (except the timestamp)
82 83 84 |
# File 'lib/codility_log.rb', line 82 def ok(msg) log msg, :ok end |
#raise_unless_agreed(question = nil) ⇒ Object
Ask user a yes/no question (via #agree) and raise an exception if user says no.
52 53 54 55 |
# 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) ⇒ Object
Simple print to STDOUT
63 64 65 |
# File 'lib/codility_log.rb', line 63 def say(msg) terminal.say(msg) end |
#say_with_space(msg) ⇒ Object
Works like normal #say method but adds an empty line before and after the message.
113 114 115 116 117 |
# File 'lib/codility_log.rb', line 113 def say_with_space(msg) say "\n" say msg say "\n" end |
#warning(msg) ⇒ Object
Logs warning message using #log, adding WARN: as a header and coloring the message yellow
93 94 95 |
# File 'lib/codility_log.rb', line 93 def warning(msg) log msg, :warning end |