codility_log 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/codility_log.rb +166 -0
  3. metadata +57 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 61d5284039dc25f6513467b0b7fec29536b420ad
4
+ data.tar.gz: 77d66d0eceaa22bea3eccb99dced0b460f87855e
5
+ SHA512:
6
+ metadata.gz: 7d06090dc18e8a60d082bb7b47a2ebb0699f0eb5d6135de70a21cfbee0ee1b3a5f02e2ec6771b322f5f1a5e91c975fd3e21513ca823a1eeec7a93237d44dc80c
7
+ data.tar.gz: 734d12478edd832a2886ecc14be60e96096088521832e52a7c67b55b785ed5e0ff56d04c3d528649cf2f15a2eaa5965da380c931a8738583124037e5d4f4ef27
@@ -0,0 +1,166 @@
1
+ # frozen-string-literal: true
2
+
3
+ require 'highline'
4
+
5
+ module CodilityLog
6
+
7
+ def self.logger
8
+ CodilityLog::Logger.new
9
+ end
10
+
11
+ class Logger
12
+ QUESTION = HighLine.color('¿ ', :white, :bold).freeze
13
+ YES_OR_NO = [
14
+ ' (',
15
+ HighLine.color('yes', :underscore),
16
+ '/',
17
+ HighLine.color('no', :underscore),
18
+ ') ',
19
+ ].join.freeze
20
+
21
+ FORMATTER = {
22
+ cmd: { header: '$ ', color: :blue },
23
+ error: { header: 'ERR: ', color: :red },
24
+ warning: { header: 'WARN: ', color: :yellow },
25
+ info: { header: 'INFO: ' },
26
+ ok: { color: :green },
27
+ default: { header: '' },
28
+ }.freeze
29
+
30
+ class Aborted < RuntimeError; end
31
+
32
+ attr_reader :terminal
33
+
34
+ def initialize
35
+ @terminal = HighLine.new
36
+ end
37
+
38
+ def color(*args)
39
+ @terminal.color(*args)
40
+ end
41
+
42
+ # Ask user a nicely formatted yes/no question and return appropriate
43
+ # boolean answer.
44
+ #--
45
+ # FIXME: this should be named #agree?
46
+ def agree(yes_no_question)
47
+ terminal.agree("#{QUESTION}#{yes_no_question}#{YES_OR_NO}")
48
+ end
49
+
50
+ # Ask user a yes/no question (via #agree) and raise an exception if user
51
+ # says no.
52
+ def raise_unless_agreed(question = nil)
53
+ question ||= 'Proceed [yes] or Abort [no]?'
54
+ raise Aborted, 'Aborting!' unless agree question
55
+ end
56
+
57
+ # Ask user to confirm putting uppercased answer (or `cancel` to abort).
58
+ def confirm_with(answer)
59
+ ask 'Please confirm', answer
60
+ end
61
+
62
+ # Simple print to STDOUT
63
+ def say(msg)
64
+ terminal.say(msg)
65
+ end
66
+
67
+ def log(msg, fmt = :default)
68
+ case fmt
69
+ when Symbol
70
+ formatter = FORMATTER[fmt].dup
71
+ when Hash
72
+ formatter = FORMATTER[:default].dup
73
+ formatter.merge!(fmt)
74
+ else
75
+ raise ArgumentError, "Invalid formatter: #{fmt}"
76
+ end
77
+
78
+ say format_message(msg, formatter)
79
+ end
80
+
81
+ # Logs message using #log, coloring it green (except the timestamp)
82
+ def ok(msg)
83
+ log msg, :ok
84
+ end
85
+
86
+ # Logs informational message using #log, adding INFO: as a header
87
+ def info(msg)
88
+ log msg, :info
89
+ end
90
+
91
+ # Logs warning message using #log, adding WARN: as a header and coloring
92
+ # the message yellow
93
+ def warning(msg)
94
+ log msg, :warning
95
+ end
96
+
97
+ # Logs error message using #log, adding ERR: as a header and coloring
98
+ # the message red. The header is aligned with #warning and #info headers by
99
+ # adding additional spacing.
100
+ def error(msg)
101
+ log msg, :error
102
+ end
103
+
104
+ # Logs message using #log, adding `$ ` header and coloring the message blue.
105
+ # This method is only responsible for custom formatting, `msg` can be any
106
+ # string, that does not necessarily correspond to an actual command.
107
+ def log_cmd(msg)
108
+ log msg, :cmd
109
+ end
110
+
111
+ # Works like normal #say method but adds an empty line before and after the
112
+ # message.
113
+ def say_with_space(msg)
114
+ say "\n"
115
+ say msg
116
+ say "\n"
117
+ end
118
+
119
+ # Works like normal #log method but adds an empty line before and after the
120
+ # message.
121
+ def log_with_space(msg, fmt = :default)
122
+ say "\n"
123
+ log msg, fmt
124
+ say "\n"
125
+ end
126
+
127
+ # Works like #log_cmd but also adds the current working dir in []
128
+ def log_cmd_with_path(msg, path: Dir.pwd)
129
+ log msg, header: "[#{path}] $", color: FORMATTER[:cmd][:color]
130
+ end
131
+
132
+ private
133
+
134
+ # Formats message using the given formatter.
135
+ #
136
+ # Add a whitespace to the header if there is none (and the header isn't
137
+ # empty) to ensure the message is readable. Header should be applied only
138
+ # to the first line, later lines should be aligned with the first one, but
139
+ # have whitespace instead of the header.
140
+
141
+ def format_message(msg, formatter)
142
+ ts = timestamp
143
+ header = formatter[:header] || ''
144
+ header << ' ' unless header[-1] == ' ' || header.length == 0
145
+
146
+ ts_length = ts.length
147
+ header_length = header.length
148
+
149
+ formatted_msg = msg.split("\n").map.with_index do |line, index|
150
+ ts = ' ' * ts_length unless index.zero?
151
+ header = ' ' * header_length unless index.zero?
152
+ header = color(header, formatter[:color])
153
+ line = color(line, formatter[:color])
154
+
155
+ "#{ts} #{header}#{line}\n"
156
+ end
157
+
158
+ formatted_msg.join
159
+ end
160
+
161
+ # Returns current UTC time in format: HH:MM:SS
162
+ def timestamp
163
+ Time.now.utc.strftime '%T UTC'
164
+ end
165
+ end
166
+ end
metadata ADDED
@@ -0,0 +1,57 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: codility_log
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Kordian Cichowski
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-12-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: highline
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ description: Helper class for logging to the terminal
28
+ email: sysadm@codility.com
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - lib/codility_log.rb
34
+ homepage:
35
+ licenses: []
36
+ metadata: {}
37
+ post_install_message:
38
+ rdoc_options: []
39
+ require_paths:
40
+ - lib
41
+ required_ruby_version: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ required_rubygems_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: '0'
51
+ requirements: []
52
+ rubyforge_project:
53
+ rubygems_version: 2.5.2
54
+ signing_key:
55
+ specification_version: 4
56
+ summary: Codility logging helper
57
+ test_files: []