Class: CodilityLog::Logger

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initializeLogger

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

#terminalObject (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 format_message(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.

Raises:



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