rbcat 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +28 -80
- data/lib/rbcat.rb +6 -1
- data/lib/rbcat/cli.rb +7 -9
- data/lib/rbcat/colorizer.rb +7 -11
- data/lib/rbcat/colors.rb +9 -0
- data/lib/rbcat/configuration.rb +2 -0
- data/lib/rbcat/rules.rb +9 -4
- data/lib/rbcat/version.rb +1 -1
- metadata +3 -4
- data/lib/rbcat/configuration_error.rb +0 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: dcd92d02b1549d5f321baa2199365b15fbbfc2e5cd62261c27ba41d53cffbbf7
|
4
|
+
data.tar.gz: 75ea1ff2ba8f5ef6b0a2dc2705a361576ef9d6461991bb6e13d77addd6458e83
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 47d472e9f8c5c92643c3a7d24eaec225fa78c00ace3997610a008ce98ff07c618867167a9fe947d45812cb0c9366a39cc64db6446a90b1598ee3266413206d35
|
7
|
+
data.tar.gz: 01d8aab7888306963d77999f18fc4003bacdc7e92a2013fa5dd3dc951fcf756c55835166a117803de7b036f5a0c33093917a30f253b5f55feee3e2caf7a0bd8f
|
data/README.md
CHANGED
@@ -37,17 +37,13 @@ rules = {
|
|
37
37
|
}
|
38
38
|
|
39
39
|
require "rbcat"
|
40
|
-
|
41
|
-
puts colorizer.colorize(description)
|
42
|
-
|
40
|
+
puts Rbcat.colorize(description, rules: rules)
|
43
41
|
```
|
44
42
|
|
45
43
|
![](https://hsto.org/webt/qp/pp/nb/qpppnbvennx7yp5nxpye5qrgt_c.png)
|
46
44
|
|
47
45
|
|
48
|
-
|
49
46
|
**Same using CLI:**
|
50
|
-
|
51
47
|
```yaml
|
52
48
|
# rbcat_config.yaml
|
53
49
|
---
|
@@ -70,25 +66,24 @@ colorizes content by set of regex rules from a config file, and then writes it
|
|
70
66
|
to standard output.
|
71
67
|
You can use rbcat in your ruby/ROR projects or as a standalone CLI tool (similar to grcat)." > description.txt
|
72
68
|
|
73
|
-
$ cat description.txt | rbcat --rules=rbcat_config.yaml
|
69
|
+
$ cat description.txt | rbcat --rules=rbcat_config.yaml
|
70
|
+
# or
|
74
71
|
$ rbcat --rules=rbcat_config.yaml < description.txt
|
75
72
|
```
|
76
73
|
|
77
74
|
![](https://hsto.org/webt/_u/5o/vu/_u5ovumrklgtx-akeqd_lbdpkys.png)
|
78
75
|
|
79
76
|
|
80
|
-
|
81
77
|
### Configuration
|
82
|
-
|
83
78
|
##### Configure
|
84
79
|
|
85
80
|
You can configure Rbcat this way:
|
86
81
|
|
87
82
|
```ruby
|
88
|
-
require
|
83
|
+
require 'rbcat'
|
84
|
+
require 'yaml'
|
89
85
|
|
90
86
|
Rbcat.configure do |config|
|
91
|
-
require "yaml"
|
92
87
|
config.rules = YAML.load_file(File.expand_path("rbcat_config.yaml"))
|
93
88
|
config.predefined = [:logger]
|
94
89
|
end
|
@@ -97,14 +92,10 @@ end
|
|
97
92
|
And then everywhere in the ruby code just:
|
98
93
|
|
99
94
|
```ruby
|
100
|
-
|
101
|
-
puts colorizer.colorize("String to colorize")
|
95
|
+
puts Rbcat.colorize("String to colorize")
|
102
96
|
```
|
103
97
|
|
104
|
-
|
105
|
-
|
106
98
|
##### Regex rules and colors
|
107
|
-
|
108
99
|
Config contains rules. Each rule has options. Example:
|
109
100
|
|
110
101
|
```ruby
|
@@ -117,9 +108,7 @@ config = {
|
|
117
108
|
}
|
118
109
|
```
|
119
110
|
|
120
|
-
|
121
|
-
|
122
|
-
##### Predefined color sets
|
111
|
+
##### Predefined sets
|
123
112
|
|
124
113
|
There are predefined sets of rules: **jsonhash** (colorizes strings that contain _json_ or _ruby hash_) and **logger** (colorizes _DEBUG_, _INFO_, _WARN_ and _ERROR_).
|
125
114
|
|
@@ -135,16 +124,10 @@ Let's see:
|
|
135
124
|
|
136
125
|
You can use both custom and predefined rules in the same time.
|
137
126
|
|
138
|
-
|
139
|
-
|
140
127
|
##### Colors
|
141
|
-
|
142
128
|
To print all available colors: `$ rbcat --print_colors`
|
143
129
|
|
144
|
-
|
145
|
-
|
146
130
|
##### Yaml config
|
147
|
-
|
148
131
|
Correct yaml config should be convertible to the Ruby hash. Here is an example of Rbcat config in both Ruby hash and yaml:
|
149
132
|
|
150
133
|
```yaml
|
@@ -190,27 +173,20 @@ Correct yaml config should be convertible to the Ruby hash. Here is an example o
|
|
190
173
|
}
|
191
174
|
```
|
192
175
|
|
193
|
-
|
194
|
-
|
195
176
|
### Using inside ruby project
|
196
|
-
|
197
177
|
It's a good idea to use rbcat with logger, so you can configure logger with colorizer once and then use it to print info to the console everywhere in the ruby code.
|
198
178
|
|
199
179
|
What we need to do is to [define formatter](http://ruby-doc.org/stdlib-2.5.0/libdoc/logger/rdoc/Logger.html#class-Logger-label-Format) for the logger, while creating one.
|
200
180
|
|
201
|
-
|
202
|
-
|
203
181
|
##### Using with a default ruby logger
|
204
|
-
|
205
|
-
Here is the simple example:
|
206
|
-
|
182
|
+
Here is a simple example:
|
207
183
|
```ruby
|
208
|
-
require
|
209
|
-
require
|
184
|
+
require 'logger'
|
185
|
+
require 'rbcat'
|
186
|
+
require 'yaml'
|
210
187
|
|
211
188
|
# configure rbcat first
|
212
189
|
Rbcat.configure do |config|
|
213
|
-
require "yaml"
|
214
190
|
config.rules = YAML.load_file(File.expand_path("rbcat_config.yaml"))
|
215
191
|
end
|
216
192
|
|
@@ -218,22 +194,17 @@ end
|
|
218
194
|
formatter = proc do |severity, datetime, progname, msg|
|
219
195
|
# default ruby logger layout:
|
220
196
|
output = "%s, [%s#%d] %5s -- %s: %s\n".freeze % [severity[0..0], datetime, $$, severity, progname, msg]
|
221
|
-
|
222
|
-
colorizer.colorize(output)
|
197
|
+
Rbcat.colorize(output)
|
223
198
|
end
|
224
199
|
|
225
200
|
# logger instance
|
226
201
|
logger = ::Logger.new(STDOUT, formatter: formatter)
|
227
|
-
|
228
202
|
logger.info "Message to colorize"
|
229
203
|
```
|
230
204
|
|
231
|
-
|
232
|
-
|
233
|
-
One of possible solutions is to use logger module and then include it to every class where we need it:
|
234
|
-
|
205
|
+
Usually, there are many classes and we need somehow to have access to colorized logger instance from everywhere. For this we can define logger module and then include it to every class where we need it:
|
235
206
|
```ruby
|
236
|
-
require
|
207
|
+
require 'logger'
|
237
208
|
|
238
209
|
module Log
|
239
210
|
def logger
|
@@ -241,8 +212,7 @@ module Log
|
|
241
212
|
::Logger.new(STDOUT, formatter: proc { |severity, datetime, progname, msg|
|
242
213
|
# default ruby logger layout
|
243
214
|
output = "%s, [%s#%d] %5s -- %s: %s\n".freeze % [severity[0..0], datetime, $$, severity, progname, msg]
|
244
|
-
|
245
|
-
colorizer.colorize(output)
|
215
|
+
Rbcat.colorize(output)
|
246
216
|
})
|
247
217
|
end
|
248
218
|
end
|
@@ -259,13 +229,11 @@ end
|
|
259
229
|
SomeClass.new.print_message("Colorized message")
|
260
230
|
```
|
261
231
|
|
262
|
-
|
263
|
-
|
264
|
-
In the example above, we still need every time include Log module for every class. **There is another more convenient way**, using Log class with class logger methods:
|
232
|
+
In the example above, we still need every time to include Log module for every class. **There is another more convenient way**, using Log class with logger class methods:
|
265
233
|
|
266
234
|
```ruby
|
267
|
-
require
|
268
|
-
require
|
235
|
+
require 'logger'
|
236
|
+
require 'forwardable'
|
269
237
|
|
270
238
|
class Log
|
271
239
|
class << self
|
@@ -277,8 +245,7 @@ class Log
|
|
277
245
|
::Logger.new(STDOUT, formatter: proc { |severity, datetime, progname, msg|
|
278
246
|
# default ruby logger layout
|
279
247
|
output = "%s, [%s#%d] %5s -- %s: %s\n".freeze % [severity[0..0], datetime, $$, severity, progname, msg]
|
280
|
-
|
281
|
-
colorizer.colorize(output)
|
248
|
+
Rbcat.colorize(output, predefined: [:logger])
|
282
249
|
})
|
283
250
|
end
|
284
251
|
end
|
@@ -291,20 +258,17 @@ end
|
|
291
258
|
With this approach, you can use colorized logger everywhere in the code.
|
292
259
|
|
293
260
|
|
294
|
-
|
295
261
|
##### Using with other logger libraries
|
296
|
-
|
297
262
|
[Logstash:](https://github.com/dwbutler/logstash-logger)
|
298
263
|
|
299
264
|
```ruby
|
300
|
-
require
|
301
|
-
require
|
265
|
+
require 'logstash-logger'
|
266
|
+
require 'rbcat'
|
302
267
|
|
303
268
|
logger = begin
|
304
269
|
formatter = proc { |severity, datetime, progname, msg|
|
305
270
|
output = "%s, [%s#%d] %5s -- %s: %s\n".freeze % [severity[0..0], datetime, $$, severity, progname, msg]
|
306
|
-
|
307
|
-
colorizer.colorize(output)
|
271
|
+
Rbcat.colorize(output)
|
308
272
|
}
|
309
273
|
|
310
274
|
LogStashLogger.new(type: :stdout, formatter: formatter)
|
@@ -314,20 +278,17 @@ logger.info "Info message to colorize"
|
|
314
278
|
```
|
315
279
|
|
316
280
|
|
317
|
-
|
318
281
|
##### Write clear log to the file and print colorized output to the console at the same time
|
319
|
-
|
320
282
|
Suddenly, default ruby logger can't output info to the several sources at the same time. But it can do [Logstash](https://github.com/dwbutler/logstash-logger) for example:
|
321
283
|
|
322
284
|
```ruby
|
323
|
-
require
|
324
|
-
require
|
285
|
+
require 'logstash-logger'
|
286
|
+
require 'rbcat'
|
325
287
|
|
326
288
|
logger = begin
|
327
289
|
formatter = proc { |severity, datetime, progname, msg|
|
328
290
|
output = "%s, [%s#%d] %5s -- %s: %s\n".freeze % [severity[0..0], datetime, $$, severity, progname, msg]
|
329
|
-
|
330
|
-
colorizer.colorize(output)
|
291
|
+
Rbcat.colorize(output)
|
331
292
|
}
|
332
293
|
|
333
294
|
outputs = [
|
@@ -335,16 +296,12 @@ logger = begin
|
|
335
296
|
{ type: :file, formatter: ::Logger::Formatter }
|
336
297
|
]
|
337
298
|
|
338
|
-
LogStashLogger.new(type: :multi_logger,
|
299
|
+
LogStashLogger.new(type: :multi_logger, outputs: outputs)
|
339
300
|
end
|
340
301
|
```
|
341
302
|
|
342
|
-
|
343
|
-
|
344
303
|
### Q&A
|
345
|
-
|
346
304
|
##### I have a problem with a printing delay to the console using rbcat CLI
|
347
|
-
|
348
305
|
The same problem has [grcat](https://github.com/garabik/grc).
|
349
306
|
|
350
307
|
Example:
|
@@ -353,34 +310,25 @@ Example:
|
|
353
310
|
$ ruby -e "loop { puts 'INFO: This is info message'; sleep 0.1 }" | rbcat --predefined=logger
|
354
311
|
```
|
355
312
|
|
356
|
-
This code should print _"INFO: This is info message"_ to the console every 0.1 seconds. But its
|
313
|
+
This code should print _"INFO: This is info message"_ to the console every 0.1 seconds. But its doesn't, because of nature of STDOUT buffering. [Here is a great article](https://eklitzke.org/stdout-buffering) about it.
|
357
314
|
|
358
315
|
One of possible solutions is to use **[unbuffer](https://linux.die.net/man/1/unbuffer)** tool (`sudo apt install expect` for ubuntu):
|
359
316
|
|
360
317
|
```bash
|
361
318
|
$ unbuffer ruby -e "loop { puts 'INFO: This is info message'; sleep 0.1 }" | rbcat --predefined=logger
|
362
319
|
```
|
363
|
-
|
364
320
|
Now message prints to the console every 0.1 seconds without any delay.
|
365
321
|
|
366
|
-
|
367
|
-
|
368
322
|
##### I don't like colors which colorizer prints
|
369
|
-
|
370
|
-
Colorizer uses [standard ANSI escape color codes](https://en.wikipedia.org/wiki/ANSI_escape_code#Colors), but each terminal can have an individual color palette. You can install additional color schemes, [here](https://github.com/Mayccoll/Gogh) for example [themes](https://github.com/denysdovhan/one-gnome-terminal) for Gnome terminal.
|
371
|
-
|
323
|
+
Colorizer uses [standard ANSI escape color codes](https://en.wikipedia.org/wiki/ANSI_escape_code#Colors), but each terminal can have an individual color palette. You can install additional color schemes, [here](https://github.com/Mayccoll/Gogh) for example [themes](https://github.com/denysdovhan/one-gnome-terminal) for a Gnome terminal.
|
372
324
|
|
373
325
|
|
374
326
|
##### I want to temporary disable colorizer
|
375
|
-
|
376
327
|
Define environment variable `RBCAT_COLORIZER` with value `false`. Or use `RBCAT_COLORIZER=false` as the first parameter of command:
|
377
328
|
|
378
329
|
```bash
|
379
330
|
$ RBCAT_COLORIZER=false ruby grcat_example.rb
|
380
331
|
```
|
381
332
|
|
382
|
-
|
383
|
-
|
384
333
|
### License
|
385
|
-
|
386
334
|
MIT
|
data/lib/rbcat.rb
CHANGED
@@ -1,7 +1,12 @@
|
|
1
1
|
require "rbcat/version"
|
2
|
-
require "rbcat/configuration_error"
|
3
2
|
require "rbcat/configuration"
|
4
3
|
require "rbcat/colors"
|
5
4
|
require "rbcat/rules"
|
6
5
|
require "rbcat/colorizer"
|
7
6
|
require "rbcat/cli"
|
7
|
+
|
8
|
+
module Rbcat
|
9
|
+
def self.colorize(string, options = {})
|
10
|
+
Colorizer.colorize(string, options)
|
11
|
+
end
|
12
|
+
end
|
data/lib/rbcat/cli.rb
CHANGED
@@ -1,17 +1,18 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require
|
3
|
+
require 'optparse'
|
4
|
+
require 'yaml'
|
4
5
|
|
5
6
|
module Rbcat
|
6
7
|
class CLI
|
7
8
|
def self.start(args)
|
8
9
|
options = parse_options(args)
|
9
|
-
|
10
|
+
colorizer_options = create_colorizer_options(options)
|
10
11
|
|
11
12
|
while input = STDIN.gets
|
12
13
|
input.each_line do |line|
|
13
14
|
begin
|
14
|
-
puts
|
15
|
+
puts Rbcat.colorize(line, colorizer_options)
|
15
16
|
rescue Errno::EPIPE
|
16
17
|
exit(74)
|
17
18
|
end
|
@@ -19,16 +20,13 @@ module Rbcat
|
|
19
20
|
end
|
20
21
|
end
|
21
22
|
|
22
|
-
private_class_method
|
23
|
-
|
24
|
-
def self.create_colorizer(options)
|
23
|
+
private_class_method def self.create_colorizer_options(options)
|
25
24
|
rules =
|
26
25
|
if options[:rules]
|
27
26
|
file_path = File.expand_path(options[:rules])
|
28
27
|
unless File.exist? file_path
|
29
28
|
raise ConfigurationError, "Config file not found: #{file_path}."
|
30
29
|
else
|
31
|
-
require "yaml"
|
32
30
|
YAML.load_file(file_path)
|
33
31
|
end
|
34
32
|
end
|
@@ -36,10 +34,10 @@ module Rbcat
|
|
36
34
|
predefined = options[:predefined]
|
37
35
|
order = options[:order]
|
38
36
|
|
39
|
-
|
37
|
+
{ predefined: predefined, rules: rules, order: order }
|
40
38
|
end
|
41
39
|
|
42
|
-
def self.parse_options(args)
|
40
|
+
private_class_method def self.parse_options(args)
|
43
41
|
options = {}
|
44
42
|
|
45
43
|
args.push("-h") if args.empty?
|
data/lib/rbcat/colorizer.rb
CHANGED
@@ -6,15 +6,13 @@ module Rbcat
|
|
6
6
|
end
|
7
7
|
end
|
8
8
|
|
9
|
-
def
|
10
|
-
@config = create_config(predefined, rules, order)
|
11
|
-
end
|
12
|
-
|
13
|
-
def colorize(string)
|
9
|
+
def self.colorize(string, predefined: nil, rules: nil, order: nil)
|
14
10
|
return string if ENV["RBCAT_COLORIZER"] == "false"
|
15
11
|
|
16
12
|
colors = Rbcat::Colors::DEFAULT
|
17
|
-
|
13
|
+
config = create_config(predefined, rules, order)
|
14
|
+
|
15
|
+
config.each_value do |settings|
|
18
16
|
if settings[:once]
|
19
17
|
string.sub!(settings[:regexp]) do |match|
|
20
18
|
colors[settings[:color]] + match + colors[:default]
|
@@ -38,14 +36,12 @@ module Rbcat
|
|
38
36
|
string
|
39
37
|
end
|
40
38
|
|
41
|
-
def uncolorize(string)
|
39
|
+
def self.uncolorize(string)
|
42
40
|
pattern = /\033\[([0-9]+);([0-9]+)m|\033\[([0-9]+)m/m
|
43
41
|
string.gsub(pattern, "")
|
44
42
|
end
|
45
43
|
|
46
|
-
|
47
|
-
|
48
|
-
def create_config(predefined, rules, order)
|
44
|
+
private_class_method def self.create_config(predefined, rules, order)
|
49
45
|
predefined_rules = predefined ? predefined : Rbcat.configuration&.predefined
|
50
46
|
rules ||= Rbcat.configuration&.rules
|
51
47
|
|
@@ -79,7 +75,7 @@ module Rbcat
|
|
79
75
|
end
|
80
76
|
end
|
81
77
|
|
82
|
-
def deep_merge(hash, other_hash)
|
78
|
+
private_class_method def self.deep_merge(hash, other_hash)
|
83
79
|
other_hash.each_pair do |current_key, other_value|
|
84
80
|
this_value = hash[current_key]
|
85
81
|
hash[current_key] =
|
data/lib/rbcat/colors.rb
CHANGED
@@ -19,6 +19,15 @@ module Rbcat
|
|
19
19
|
cyan: "\033[36m",
|
20
20
|
white: "\033[37m",
|
21
21
|
|
22
|
+
bold_black: "\033[1;30m",
|
23
|
+
bold_red: "\033[1;31m",
|
24
|
+
bold_green: "\033[1;32m",
|
25
|
+
bold_yellow: "\033[1;33m",
|
26
|
+
bold_blue: "\033[1;34m",
|
27
|
+
bold_magenta: "\033[1;35m",
|
28
|
+
bold_cyan: "\033[1;36m",
|
29
|
+
bold_white: "\033[1;37m",
|
30
|
+
|
22
31
|
on_black: "\033[40m",
|
23
32
|
on_red: "\033[41m",
|
24
33
|
on_green: "\033[42m",
|
data/lib/rbcat/configuration.rb
CHANGED
data/lib/rbcat/rules.rb
CHANGED
@@ -29,22 +29,27 @@ module Rbcat
|
|
29
29
|
|
30
30
|
LOGGER = {
|
31
31
|
info_logger: {
|
32
|
-
regexp: /INFO(\s--\s
|
32
|
+
regexp: /INFO(\s--\s.*?\:|)/m,
|
33
33
|
color: :cyan,
|
34
34
|
once: true
|
35
35
|
},
|
36
36
|
error_logger: {
|
37
|
-
regexp: /ERROR(\s--\s
|
37
|
+
regexp: /ERROR(\s--\s.*?\:|)/m,
|
38
38
|
color: :red,
|
39
39
|
once: true
|
40
40
|
},
|
41
|
+
fatal_logger: {
|
42
|
+
regexp: /FATAL(\s--\s.*?\:|)/m,
|
43
|
+
color: :bold_red,
|
44
|
+
once: true
|
45
|
+
},
|
41
46
|
warn_logger: {
|
42
|
-
regexp: /WARN(\s--\s
|
47
|
+
regexp: /WARN(\s--\s.*?\:|)/m,
|
43
48
|
color: :yellow,
|
44
49
|
once: true
|
45
50
|
},
|
46
51
|
debug_logger: {
|
47
|
-
regexp: /DEBUG(\s--\s
|
52
|
+
regexp: /DEBUG(\s--\s.*?\:|)/m,
|
48
53
|
color: :green,
|
49
54
|
once: true
|
50
55
|
}
|
data/lib/rbcat/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rbcat
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Victor Afanasev
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-
|
11
|
+
date: 2018-08-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -60,7 +60,6 @@ files:
|
|
60
60
|
- lib/rbcat/colorizer.rb
|
61
61
|
- lib/rbcat/colors.rb
|
62
62
|
- lib/rbcat/configuration.rb
|
63
|
-
- lib/rbcat/configuration_error.rb
|
64
63
|
- lib/rbcat/rules.rb
|
65
64
|
- lib/rbcat/version.rb
|
66
65
|
- rbcat.gemspec
|
@@ -84,7 +83,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
84
83
|
version: '0'
|
85
84
|
requirements: []
|
86
85
|
rubyforge_project:
|
87
|
-
rubygems_version: 2.7.
|
86
|
+
rubygems_version: 2.7.6
|
88
87
|
signing_key:
|
89
88
|
specification_version: 4
|
90
89
|
summary: Colorize output by defined set of regex rules
|