rbcat 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +8 -0
- data/.rubocop.yml +2 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +21 -0
- data/README.md +386 -0
- data/Rakefile +2 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/exe/rbcat +4 -0
- data/lib/rbcat.rb +7 -0
- data/lib/rbcat/cli.rb +87 -0
- data/lib/rbcat/colorizer.rb +96 -0
- data/lib/rbcat/colors.rb +50 -0
- data/lib/rbcat/configuration.rb +20 -0
- data/lib/rbcat/configuration_error.rb +3 -0
- data/lib/rbcat/rules.rb +53 -0
- data/lib/rbcat/version.rb +3 -0
- data/rbcat.gemspec +29 -0
- metadata +91 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 7cf26b222d1b203aca1a1a0fe57e6888d4c9443c89ed2e9357f094adeb5114f7
|
4
|
+
data.tar.gz: f32f88be4960c79ca91eebf320ff9b010ed47bd4e7bebb939d2e1bf78a4c44f0
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: aecdbfac200a6845ce384a7c4e1386bf328427c00332914ee54a74e4050b11dc96b77a6bb5bcf1fa1fc2e044af7bdab7af122ad15f9259cb5ea9d2a5a469b11f
|
7
|
+
data.tar.gz: ecc9501e91a5ad25885c140edb90fd1784719135924920b96728b9e0998836780483b59d06c7f9447b200987088dfa22cd71066d5e780aab0507bd906ad24fd5
|
data/.gitignore
ADDED
data/.rubocop.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2018 Victor Afanasev
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,386 @@
|
|
1
|
+
# Rbcat
|
2
|
+
|
3
|
+
### Introduction
|
4
|
+
|
5
|
+
Rbcat it's a CLI tool written in ruby which reads from standard input (STDIN), colorizes content by set of regex rules from a config file, and then writes it to standard output. Inspired by [grcat](https://github.com/garabik/grc).
|
6
|
+
You can use rbcat in your ruby/ROR projects or as a standalone CLI tool (similar to grcat).
|
7
|
+
|
8
|
+
**Install rbcat first:** `gem install rbcat`
|
9
|
+
|
10
|
+
```ruby
|
11
|
+
# rbcat_example.rb
|
12
|
+
|
13
|
+
description = <<~HEREDOC
|
14
|
+
Rbcat it's a CLI tool written in ruby which reads from standard input (stdin),
|
15
|
+
colorizes content by set of regex rules from a config file, and then writes it
|
16
|
+
to standard output.
|
17
|
+
You can use rbcat in your ruby/ROR projects or as a standalone CLI tool (similar to grcat).
|
18
|
+
HEREDOC
|
19
|
+
|
20
|
+
rules = {
|
21
|
+
ruby_word: {
|
22
|
+
regexp: /ruby/m,
|
23
|
+
color: :red
|
24
|
+
},
|
25
|
+
upcase_words: {
|
26
|
+
regexp: /[A-Z]{2,}/m,
|
27
|
+
color: :bold
|
28
|
+
},
|
29
|
+
inside_round_brackets: {
|
30
|
+
regexp: /\(.*?\)/m,
|
31
|
+
color: :cyan
|
32
|
+
},
|
33
|
+
gem_name: {
|
34
|
+
regexp: /rbcat/mi,
|
35
|
+
color: :green
|
36
|
+
}
|
37
|
+
}
|
38
|
+
|
39
|
+
require "rbcat"
|
40
|
+
colorizer = Rbcat::Colorizer.new(rules: rules)
|
41
|
+
puts colorizer.colorize(description)
|
42
|
+
|
43
|
+
```
|
44
|
+
|
45
|
+
![](https://hsto.org/webt/qp/pp/nb/qpppnbvennx7yp5nxpye5qrgt_c.png)
|
46
|
+
|
47
|
+
|
48
|
+
|
49
|
+
**Same using CLI:**
|
50
|
+
|
51
|
+
```yaml
|
52
|
+
# rbcat_config.yaml
|
53
|
+
---
|
54
|
+
:ruby_word:
|
55
|
+
:regexp: !ruby/regexp /ruby/m
|
56
|
+
:color: :red
|
57
|
+
:upcase_words:
|
58
|
+
:regexp: !ruby/regexp /[A-Z]{2,}/m
|
59
|
+
:color: :bold
|
60
|
+
:inside_round_brackets:
|
61
|
+
:regexp: !ruby/regexp /\(.*?\)/m
|
62
|
+
:color: :cyan
|
63
|
+
:gem_name:
|
64
|
+
:regexp: !ruby/regexp /rbcat/mi
|
65
|
+
:color: :green
|
66
|
+
```
|
67
|
+
```bash
|
68
|
+
$ echo "Rbcat it's a CLI tool written in ruby which reads from standard input (stdin),
|
69
|
+
colorizes content by set of regex rules from a config file, and then writes it
|
70
|
+
to standard output.
|
71
|
+
You can use rbcat in your ruby/ROR projects or as a standalone CLI tool (similar to grcat)." > description.txt
|
72
|
+
|
73
|
+
$ cat description.txt | rbcat --rules=rbcat_config.yaml # or
|
74
|
+
$ rbcat --rules=rbcat_config.yaml < description.txt
|
75
|
+
```
|
76
|
+
|
77
|
+
![](https://hsto.org/webt/_u/5o/vu/_u5ovumrklgtx-akeqd_lbdpkys.png)
|
78
|
+
|
79
|
+
|
80
|
+
|
81
|
+
### Configuration
|
82
|
+
|
83
|
+
##### Configure
|
84
|
+
|
85
|
+
You can configure Rbcat this way:
|
86
|
+
|
87
|
+
```ruby
|
88
|
+
require "rbcat"
|
89
|
+
|
90
|
+
Rbcat.configure do |config|
|
91
|
+
require "yaml"
|
92
|
+
config.rules = YAML.load_file(File.expand_path("rbcat_config.yaml"))
|
93
|
+
config.predefined = [:logger]
|
94
|
+
end
|
95
|
+
```
|
96
|
+
|
97
|
+
And then everywhere in the ruby code just:
|
98
|
+
|
99
|
+
```ruby
|
100
|
+
colorizer = Rbcat::Colorizer.new
|
101
|
+
puts colorizer.colorize("String to colorize")
|
102
|
+
```
|
103
|
+
|
104
|
+
|
105
|
+
|
106
|
+
##### Regex rules and colors
|
107
|
+
|
108
|
+
Config contains rules. Each rule has options. Example:
|
109
|
+
|
110
|
+
```ruby
|
111
|
+
config = {
|
112
|
+
ruby_word: {
|
113
|
+
regexp: /ruby/m, # Regex mask (required)
|
114
|
+
color: :red, # Color (required)
|
115
|
+
once: true # Colorize only first occurrence, then skip others (optional, defalut value is false)
|
116
|
+
}
|
117
|
+
}
|
118
|
+
```
|
119
|
+
|
120
|
+
|
121
|
+
|
122
|
+
##### Predefined color sets
|
123
|
+
|
124
|
+
There are predefined sets of rules: **jsonhash** (colorizes strings that contain _json_ or _ruby hash_) and **logger** (colorizes _DEBUG_, _INFO_, _WARN_ and _ERROR_).
|
125
|
+
|
126
|
+
Usage: `--predefined=jsonhash,logger` (CLI), or `Rbcat::Colorizer.new(predefined: [:jsonhash, logger])` (ruby)
|
127
|
+
|
128
|
+
Let's see:
|
129
|
+
|
130
|
+
![](https://hsto.org/webt/ko/ui/dw/kouidw7sm_wcsitt-nfz88ydpf4.png)
|
131
|
+
|
132
|
+
![](https://hsto.org/webt/18/6g/wj/186gwj7o9rvsqipo2rspwbe958q.png)
|
133
|
+
|
134
|
+
![](https://hsto.org/webt/yv/vg/jv/yvvgjvjpobcyjziwcv1vyxln3u4.png)
|
135
|
+
|
136
|
+
You can use both custom and predefined rules in the same time.
|
137
|
+
|
138
|
+
|
139
|
+
|
140
|
+
##### Colors
|
141
|
+
|
142
|
+
To print all available colors: `$ rbcat --print_colors`
|
143
|
+
|
144
|
+
|
145
|
+
|
146
|
+
##### Yaml config
|
147
|
+
|
148
|
+
Correct yaml config should be convertible to the Ruby hash. Here is an example of Rbcat config in both Ruby hash and yaml:
|
149
|
+
|
150
|
+
```yaml
|
151
|
+
# rbcat_config.yaml
|
152
|
+
|
153
|
+
---
|
154
|
+
:ruby_word:
|
155
|
+
:regexp: !ruby/regexp /ruby/m
|
156
|
+
:color: :red
|
157
|
+
:upcase_words:
|
158
|
+
:regexp: !ruby/regexp /[A-Z]{2,}/m
|
159
|
+
:color: :bold
|
160
|
+
:inside_round_brackets:
|
161
|
+
:regexp: !ruby/regexp /\(.*?\)/m
|
162
|
+
:color: :cyan
|
163
|
+
:gem_name:
|
164
|
+
:regexp: !ruby/regexp /rbcat/mi
|
165
|
+
:color: :green
|
166
|
+
:once: true
|
167
|
+
```
|
168
|
+
|
169
|
+
```ruby
|
170
|
+
# ruby hash
|
171
|
+
|
172
|
+
{
|
173
|
+
ruby_word: {
|
174
|
+
regexp: /ruby/m,
|
175
|
+
color: :red
|
176
|
+
},
|
177
|
+
upcase_words: {
|
178
|
+
regexp: /[A-Z]{2,}/m,
|
179
|
+
color: :bold
|
180
|
+
},
|
181
|
+
inside_round_brackets: {
|
182
|
+
regexp: /\(.*?\)/m,
|
183
|
+
color: :cyan
|
184
|
+
},
|
185
|
+
gem_name: {
|
186
|
+
regexp: /rbcat/mi,
|
187
|
+
color: :green
|
188
|
+
once: true
|
189
|
+
}
|
190
|
+
}
|
191
|
+
```
|
192
|
+
|
193
|
+
|
194
|
+
|
195
|
+
### Using inside ruby project
|
196
|
+
|
197
|
+
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
|
+
|
199
|
+
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
|
+
|
201
|
+
|
202
|
+
|
203
|
+
##### Using with a default ruby logger
|
204
|
+
|
205
|
+
Here is the simple example:
|
206
|
+
|
207
|
+
```ruby
|
208
|
+
require "logger"
|
209
|
+
require "rbcat"
|
210
|
+
|
211
|
+
# configure rbcat first
|
212
|
+
Rbcat.configure do |config|
|
213
|
+
require "yaml"
|
214
|
+
config.rules = YAML.load_file(File.expand_path("rbcat_config.yaml"))
|
215
|
+
end
|
216
|
+
|
217
|
+
# define formatter
|
218
|
+
formatter = proc do |severity, datetime, progname, msg|
|
219
|
+
# default ruby logger layout:
|
220
|
+
output = "%s, [%s#%d] %5s -- %s: %s\n".freeze % [severity[0..0], datetime, $$, severity, progname, msg]
|
221
|
+
colorizer = Rbcat::Colorizer.new
|
222
|
+
colorizer.colorize(output)
|
223
|
+
end
|
224
|
+
|
225
|
+
# logger instance
|
226
|
+
logger = ::Logger.new(STDOUT, formatter: formatter)
|
227
|
+
|
228
|
+
logger.info "Message to colorize"
|
229
|
+
```
|
230
|
+
|
231
|
+
This is a nice example but almost isn't usable. In the normal ruby project, there are many classes and we need somehow have access to ours colorized logger instance from everywhere.
|
232
|
+
|
233
|
+
One of possible solutions is to use logger module and then include it to every class where we need it:
|
234
|
+
|
235
|
+
```ruby
|
236
|
+
require "logger"
|
237
|
+
|
238
|
+
module Log
|
239
|
+
def logger
|
240
|
+
@logger ||= begin
|
241
|
+
::Logger.new(STDOUT, formatter: proc { |severity, datetime, progname, msg|
|
242
|
+
# default ruby logger layout
|
243
|
+
output = "%s, [%s#%d] %5s -- %s: %s\n".freeze % [severity[0..0], datetime, $$, severity, progname, msg]
|
244
|
+
colorizer = Rbcat::Colorizer.new
|
245
|
+
colorizer.colorize(output)
|
246
|
+
})
|
247
|
+
end
|
248
|
+
end
|
249
|
+
end
|
250
|
+
|
251
|
+
class SomeClass
|
252
|
+
include Log
|
253
|
+
|
254
|
+
def print_message(msg)
|
255
|
+
logger.info msg
|
256
|
+
end
|
257
|
+
end
|
258
|
+
|
259
|
+
SomeClass.new.print_message("Colorized message")
|
260
|
+
```
|
261
|
+
|
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:
|
265
|
+
|
266
|
+
```ruby
|
267
|
+
require "logger"
|
268
|
+
require "forwardable"
|
269
|
+
|
270
|
+
class Log
|
271
|
+
class << self
|
272
|
+
extend Forwardable
|
273
|
+
delegate [:debug, :info, :warn, :error, :fatal] => :logger
|
274
|
+
|
275
|
+
def logger
|
276
|
+
@logger ||= begin
|
277
|
+
::Logger.new(STDOUT, formatter: proc { |severity, datetime, progname, msg|
|
278
|
+
# default ruby logger layout
|
279
|
+
output = "%s, [%s#%d] %5s -- %s: %s\n".freeze % [severity[0..0], datetime, $$, severity, progname, msg]
|
280
|
+
colorizer = Rbcat::Colorizer.new(predefined: [:logger])
|
281
|
+
colorizer.colorize(output)
|
282
|
+
})
|
283
|
+
end
|
284
|
+
end
|
285
|
+
end
|
286
|
+
end
|
287
|
+
```
|
288
|
+
|
289
|
+
![](https://hsto.org/webt/1f/vr/bc/1fvrbc5mlez-kmgromeidhky700.png)
|
290
|
+
|
291
|
+
With this approach, you can use colorized logger everywhere in the code.
|
292
|
+
|
293
|
+
|
294
|
+
|
295
|
+
##### Using with other logger libraries
|
296
|
+
|
297
|
+
[Logstash:](https://github.com/dwbutler/logstash-logger)
|
298
|
+
|
299
|
+
```ruby
|
300
|
+
require "logstash-logger"
|
301
|
+
require "rbcat"
|
302
|
+
|
303
|
+
logger = begin
|
304
|
+
formatter = proc { |severity, datetime, progname, msg|
|
305
|
+
output = "%s, [%s#%d] %5s -- %s: %s\n".freeze % [severity[0..0], datetime, $$, severity, progname, msg]
|
306
|
+
colorizer = Colorizer.new
|
307
|
+
colorizer.colorize(output)
|
308
|
+
}
|
309
|
+
|
310
|
+
LogStashLogger.new(type: :stdout, formatter: formatter)
|
311
|
+
end
|
312
|
+
|
313
|
+
logger.info "Info message to colorize"
|
314
|
+
```
|
315
|
+
|
316
|
+
|
317
|
+
|
318
|
+
##### Write clear log to the file and print colorized output to the console at the same time
|
319
|
+
|
320
|
+
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
|
+
|
322
|
+
```ruby
|
323
|
+
require "logstash-logger"
|
324
|
+
require "rbcat"
|
325
|
+
|
326
|
+
logger = begin
|
327
|
+
formatter = proc { |severity, datetime, progname, msg|
|
328
|
+
output = "%s, [%s#%d] %5s -- %s: %s\n".freeze % [severity[0..0], datetime, $$, severity, progname, msg]
|
329
|
+
colorizer = Colorizer.new
|
330
|
+
colorizer.colorize(output)
|
331
|
+
}
|
332
|
+
|
333
|
+
outputs = [
|
334
|
+
{ type: :stdout, formatter: formatter },
|
335
|
+
{ type: :file, formatter: ::Logger::Formatter }
|
336
|
+
]
|
337
|
+
|
338
|
+
LogStashLogger.new(type: :multi_logger, formatter: formatter, outputs: outputs)
|
339
|
+
end
|
340
|
+
```
|
341
|
+
|
342
|
+
|
343
|
+
|
344
|
+
### Q&A
|
345
|
+
|
346
|
+
##### I have a problem with a printing delay to the console using rbcat CLI
|
347
|
+
|
348
|
+
The same problem has [grcat](https://github.com/garabik/grc).
|
349
|
+
|
350
|
+
Example:
|
351
|
+
|
352
|
+
```bash
|
353
|
+
$ ruby -e "loop { puts 'INFO: This is info message'; sleep 0.1 }" | rbcat --predefined=logger
|
354
|
+
```
|
355
|
+
|
356
|
+
This code should print _"INFO: This is info message"_ to the console every 0.1 seconds. But its don't, because of nature of STDOUT buffering. [Here is a great article](https://eklitzke.org/stdout-buffering) about it.
|
357
|
+
|
358
|
+
One of possible solutions is to use **[unbuffer](https://linux.die.net/man/1/unbuffer)** tool (`sudo apt install expect` for ubuntu):
|
359
|
+
|
360
|
+
```bash
|
361
|
+
$ unbuffer ruby -e "loop { puts 'INFO: This is info message'; sleep 0.1 }" | rbcat --predefined=logger
|
362
|
+
```
|
363
|
+
|
364
|
+
Now message prints to the console every 0.1 seconds without any delay.
|
365
|
+
|
366
|
+
|
367
|
+
|
368
|
+
##### 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
|
+
|
372
|
+
|
373
|
+
|
374
|
+
##### I want to temporary disable colorizer
|
375
|
+
|
376
|
+
Define environment variable `RBCAT_COLORIZER` with value `false`. Or use `RBCAT_COLORIZER=false` as the first parameter of command:
|
377
|
+
|
378
|
+
```bash
|
379
|
+
$ RBCAT_COLORIZER=false ruby grcat_example.rb
|
380
|
+
```
|
381
|
+
|
382
|
+
|
383
|
+
|
384
|
+
### License
|
385
|
+
|
386
|
+
MIT
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "rbcat"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/exe/rbcat
ADDED
data/lib/rbcat.rb
ADDED
data/lib/rbcat/cli.rb
ADDED
@@ -0,0 +1,87 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "optparse"
|
4
|
+
|
5
|
+
module Rbcat
|
6
|
+
class CLI
|
7
|
+
def self.start(args)
|
8
|
+
options = parse_options(args)
|
9
|
+
colorizer = create_colorizer(options)
|
10
|
+
|
11
|
+
while input = STDIN.gets
|
12
|
+
input.each_line do |line|
|
13
|
+
begin
|
14
|
+
puts colorizer.colorize(line)
|
15
|
+
rescue Errno::EPIPE
|
16
|
+
exit(74)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
private_class_method
|
23
|
+
|
24
|
+
def self.create_colorizer(options)
|
25
|
+
rules =
|
26
|
+
if options[:rules]
|
27
|
+
file_path = File.expand_path(options[:rules])
|
28
|
+
unless File.exist? file_path
|
29
|
+
raise ConfigurationError, "Config file not found: #{file_path}."
|
30
|
+
else
|
31
|
+
require "yaml"
|
32
|
+
YAML.load_file(file_path)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
predefined = options[:predefined]
|
37
|
+
order = options[:order]
|
38
|
+
|
39
|
+
Colorizer.new(predefined: predefined, rules: rules, order: order)
|
40
|
+
end
|
41
|
+
|
42
|
+
def self.parse_options(args)
|
43
|
+
options = {}
|
44
|
+
|
45
|
+
args.push("-h") if args.empty?
|
46
|
+
|
47
|
+
OptionParser.new do |opts|
|
48
|
+
opts.banner = <<~HEREDOC
|
49
|
+
Rbcat it's a CLI tool which reads from standard input (STDIN),
|
50
|
+
colorizes content by set of regex rules from a config file,
|
51
|
+
and then writes it to standard output.
|
52
|
+
HEREDOC
|
53
|
+
opts.separator ""
|
54
|
+
|
55
|
+
predefined_desc = "Colorize input by set of predefined rules. " \
|
56
|
+
"Currently there are json/hash 'jsonhash' and ruby's logger 'logger'. " \
|
57
|
+
"Example: --predefined=jsonhash,logger"
|
58
|
+
opts.on("-p", "--predefined GROUPS", Array, predefined_desc) do |arg|
|
59
|
+
options[:predefined] = arg
|
60
|
+
end
|
61
|
+
|
62
|
+
rules_desc = "Path to the custom yaml config for rbcat."
|
63
|
+
opts.on("-r", "--rules PATH", rules_desc) do |arg|
|
64
|
+
options[:rules] = arg
|
65
|
+
end
|
66
|
+
|
67
|
+
order_desc = "Specify order for predefined and custom rules." \
|
68
|
+
"Avaiable options: 'predefined_first' (default) or 'predefined_last'."
|
69
|
+
opts.on("-o", "--order ORDER", order_desc) do |arg|
|
70
|
+
options[:order] = arg
|
71
|
+
end
|
72
|
+
|
73
|
+
opts.on("-c", "--print_colors", "Print all avaiable colors.") do
|
74
|
+
Rbcat::Colorizer.print_colors
|
75
|
+
exit
|
76
|
+
end
|
77
|
+
|
78
|
+
opts.on_tail("-v", "--version", "Show version") do
|
79
|
+
puts Rbcat::VERSION
|
80
|
+
exit
|
81
|
+
end
|
82
|
+
end.parse!(args)
|
83
|
+
|
84
|
+
options
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
@@ -0,0 +1,96 @@
|
|
1
|
+
module Rbcat
|
2
|
+
class Colorizer
|
3
|
+
def self.print_colors
|
4
|
+
Rbcat::Colors::DEFAULT.each do |key, value|
|
5
|
+
puts "#{value}#{key}\e[0m"
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
def initialize(predefined: nil, rules: nil, order: nil)
|
10
|
+
@config = create_config(predefined, rules, order)
|
11
|
+
end
|
12
|
+
|
13
|
+
def colorize(string)
|
14
|
+
return string if ENV["RBCAT_COLORIZER"] == "false"
|
15
|
+
|
16
|
+
colors = Rbcat::Colors::DEFAULT
|
17
|
+
@config.each_value do |settings|
|
18
|
+
if settings[:once]
|
19
|
+
string.sub!(settings[:regexp]) do |match|
|
20
|
+
colors[settings[:color]] + match + colors[:default]
|
21
|
+
end
|
22
|
+
elsif settings[:colors]
|
23
|
+
string.gsub!(settings[:regexp]) do
|
24
|
+
str = ""
|
25
|
+
Regexp.last_match.captures.each_with_index do |match, i|
|
26
|
+
str << colors[settings[:colors][i]] + match + colors[:default]
|
27
|
+
end
|
28
|
+
|
29
|
+
str
|
30
|
+
end
|
31
|
+
else
|
32
|
+
string.gsub!(settings[:regexp]) do |match|
|
33
|
+
colors[settings[:color]] + match + colors[:default]
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
string
|
39
|
+
end
|
40
|
+
|
41
|
+
def uncolorize(string)
|
42
|
+
pattern = /\033\[([0-9]+);([0-9]+)m|\033\[([0-9]+)m/m
|
43
|
+
string.gsub(pattern, "")
|
44
|
+
end
|
45
|
+
|
46
|
+
private
|
47
|
+
|
48
|
+
def create_config(predefined, rules, order)
|
49
|
+
predefined_rules = predefined ? predefined : Rbcat.configuration&.predefined
|
50
|
+
rules ||= Rbcat.configuration&.rules
|
51
|
+
|
52
|
+
raise ConfigurationError, "No config defined." unless predefined_rules || rules
|
53
|
+
if rules && rules.class != Hash
|
54
|
+
raise ConfigurationError, "Incorrect configuration. " \
|
55
|
+
"There is error while converting yaml config into Ruby's hash of rules."
|
56
|
+
end
|
57
|
+
|
58
|
+
config = {}
|
59
|
+
order ||= Rbcat.configuration&.order || :predefined_first
|
60
|
+
|
61
|
+
case order
|
62
|
+
when :predefined_first
|
63
|
+
predefined_rules&.each do |group|
|
64
|
+
config.merge!(Object.const_get "Rbcat::Rules::#{group.to_s.upcase}")
|
65
|
+
end
|
66
|
+
|
67
|
+
rules ? deep_merge(config, rules) : config
|
68
|
+
when :predefined_last
|
69
|
+
config.merge!(rules) if rules
|
70
|
+
|
71
|
+
predefined_rules&.each do |group|
|
72
|
+
config.merge!(Object.const_get "Rbcat::Rules::#{group.to_s.upcase}")
|
73
|
+
end
|
74
|
+
|
75
|
+
config
|
76
|
+
else
|
77
|
+
raise ConfigurationError, "Wrong type of order: #{order}. " \
|
78
|
+
"Define :predefined_first (default) or :predefined_last"
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
def deep_merge(hash, other_hash)
|
83
|
+
other_hash.each_pair do |current_key, other_value|
|
84
|
+
this_value = hash[current_key]
|
85
|
+
hash[current_key] =
|
86
|
+
if this_value.is_a?(Hash) && other_value.is_a?(Hash)
|
87
|
+
deep_merge(this_value, other_value)
|
88
|
+
else
|
89
|
+
other_value
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
hash
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
data/lib/rbcat/colors.rb
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Rbcat
|
4
|
+
module Colors
|
5
|
+
DEFAULT = {
|
6
|
+
default: "\033[0m",
|
7
|
+
bold: "\033[1m",
|
8
|
+
underline: "\033[4m",
|
9
|
+
blink: "\033[5m",
|
10
|
+
reverse: "\033[7m",
|
11
|
+
concealed: "\033[8m",
|
12
|
+
|
13
|
+
black: "\033[30m",
|
14
|
+
red: "\033[31m",
|
15
|
+
green: "\033[32m",
|
16
|
+
yellow: "\033[33m",
|
17
|
+
blue: "\033[34m",
|
18
|
+
magenta: "\033[35m",
|
19
|
+
cyan: "\033[36m",
|
20
|
+
white: "\033[37m",
|
21
|
+
|
22
|
+
on_black: "\033[40m",
|
23
|
+
on_red: "\033[41m",
|
24
|
+
on_green: "\033[42m",
|
25
|
+
on_yellow: "\033[43m",
|
26
|
+
on_blue: "\033[44m",
|
27
|
+
on_magenta: "\033[45m",
|
28
|
+
on_cyan: "\033[46m",
|
29
|
+
on_white: "\033[47m",
|
30
|
+
|
31
|
+
bright_black: "\033[30;90m",
|
32
|
+
bright_red: "\033[31;91m",
|
33
|
+
bright_green: "\033[32;92m",
|
34
|
+
bright_yellow: "\033[33;93m",
|
35
|
+
bright_blue: "\033[34;94m",
|
36
|
+
bright_magenta: "\033[35;95m",
|
37
|
+
bright_cyan: "\033[36;96m",
|
38
|
+
bright_white: "\033[37;97m",
|
39
|
+
|
40
|
+
on_bright_black: "\033[40;100m",
|
41
|
+
on_bright_red: "\033[41;101m",
|
42
|
+
on_bright_green: "\033[42;102m",
|
43
|
+
on_bright_yellow: "\033[43;103m",
|
44
|
+
on_bright_blue: "\033[44;104m",
|
45
|
+
on_bright_magenta: "\033[45;105m",
|
46
|
+
on_bright_cyan: "\033[46;106m",
|
47
|
+
on_bright_white: "\033[47;107m"
|
48
|
+
}.freeze
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module Rbcat
|
2
|
+
class << self
|
3
|
+
attr_accessor :configuration
|
4
|
+
end
|
5
|
+
|
6
|
+
def self.configure
|
7
|
+
self.configuration ||= Configuration.new
|
8
|
+
yield(configuration)
|
9
|
+
end
|
10
|
+
|
11
|
+
class Configuration
|
12
|
+
attr_accessor :predefined, :rules, :order
|
13
|
+
|
14
|
+
def initialize
|
15
|
+
@predefined = nil
|
16
|
+
@rules = nil
|
17
|
+
@order = nil
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
data/lib/rbcat/rules.rb
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
module Rbcat
|
2
|
+
module Rules
|
3
|
+
JSONHASH = {
|
4
|
+
value_integer: {
|
5
|
+
regexp: /(?<=\"\:)\d+|(?<=\=\>)\d+/m,
|
6
|
+
color: :cyan
|
7
|
+
},
|
8
|
+
key_string: {
|
9
|
+
regexp: /\"[^\"]*\"(?=\:)/m,
|
10
|
+
color: :green
|
11
|
+
},
|
12
|
+
key_symbol: {
|
13
|
+
regexp: /\:[\p{L}\_\d]*(?=\=\>)|\:\"[^\"]*\"(?=\=\>)/m,
|
14
|
+
color: :magenta
|
15
|
+
},
|
16
|
+
value_string: {
|
17
|
+
regexp: /\"(?:[^"\\]|\\.)*\"(?=[\,\n\}\]])/m,
|
18
|
+
color: :yellow
|
19
|
+
},
|
20
|
+
value_null_nil: {
|
21
|
+
regexp: /(?<=\:)null|(?<=\=\>)nil/m,
|
22
|
+
color: :magenta
|
23
|
+
},
|
24
|
+
value_true_false: {
|
25
|
+
regexp: /(?<=\:)(false|true)|(?<=\=\>)(false|true)/m,
|
26
|
+
color: :magenta
|
27
|
+
}
|
28
|
+
}.freeze
|
29
|
+
|
30
|
+
LOGGER = {
|
31
|
+
info_logger: {
|
32
|
+
regexp: /INFO(\s--\s:|)/m,
|
33
|
+
color: :cyan,
|
34
|
+
once: true
|
35
|
+
},
|
36
|
+
error_logger: {
|
37
|
+
regexp: /ERROR(\s--\s:|)/m,
|
38
|
+
color: :red,
|
39
|
+
once: true
|
40
|
+
},
|
41
|
+
warn_logger: {
|
42
|
+
regexp: /WARN(\s--\s:|)/m,
|
43
|
+
color: :yellow,
|
44
|
+
once: true
|
45
|
+
},
|
46
|
+
debug_logger: {
|
47
|
+
regexp: /DEBUG(\s--\s:|)/m,
|
48
|
+
color: :green,
|
49
|
+
once: true
|
50
|
+
}
|
51
|
+
}.freeze
|
52
|
+
end
|
53
|
+
end
|
data/rbcat.gemspec
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
lib = File.expand_path("../lib", __FILE__)
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
5
|
+
require "rbcat/version"
|
6
|
+
|
7
|
+
Gem::Specification.new do |spec|
|
8
|
+
spec.name = "rbcat"
|
9
|
+
spec.version = Rbcat::VERSION
|
10
|
+
|
11
|
+
spec.authors = ["Victor Afanasev"]
|
12
|
+
spec.email = ["vicfreefly@gmail.com"]
|
13
|
+
|
14
|
+
spec.summary = "Colorize output by defined set of regex rules"
|
15
|
+
spec.description = "Colorize output by defined set of regex rules"
|
16
|
+
spec.homepage = "https://github.com/vfreefly/rbcat"
|
17
|
+
spec.license = "MIT"
|
18
|
+
|
19
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
20
|
+
f.match(%r{^(test|spec|features)/})
|
21
|
+
end
|
22
|
+
spec.bindir = "exe"
|
23
|
+
spec.executables = "rbcat"
|
24
|
+
spec.require_paths = ["lib"]
|
25
|
+
spec.required_ruby_version = ">= 2.3.0"
|
26
|
+
|
27
|
+
spec.add_development_dependency "bundler", "~> 1.16"
|
28
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
29
|
+
end
|
metadata
ADDED
@@ -0,0 +1,91 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rbcat
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Victor Afanasev
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-03-13 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.16'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.16'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
description: Colorize output by defined set of regex rules
|
42
|
+
email:
|
43
|
+
- vicfreefly@gmail.com
|
44
|
+
executables:
|
45
|
+
- rbcat
|
46
|
+
extensions: []
|
47
|
+
extra_rdoc_files: []
|
48
|
+
files:
|
49
|
+
- ".gitignore"
|
50
|
+
- ".rubocop.yml"
|
51
|
+
- Gemfile
|
52
|
+
- LICENSE.txt
|
53
|
+
- README.md
|
54
|
+
- Rakefile
|
55
|
+
- bin/console
|
56
|
+
- bin/setup
|
57
|
+
- exe/rbcat
|
58
|
+
- lib/rbcat.rb
|
59
|
+
- lib/rbcat/cli.rb
|
60
|
+
- lib/rbcat/colorizer.rb
|
61
|
+
- lib/rbcat/colors.rb
|
62
|
+
- lib/rbcat/configuration.rb
|
63
|
+
- lib/rbcat/configuration_error.rb
|
64
|
+
- lib/rbcat/rules.rb
|
65
|
+
- lib/rbcat/version.rb
|
66
|
+
- rbcat.gemspec
|
67
|
+
homepage: https://github.com/vfreefly/rbcat
|
68
|
+
licenses:
|
69
|
+
- MIT
|
70
|
+
metadata: {}
|
71
|
+
post_install_message:
|
72
|
+
rdoc_options: []
|
73
|
+
require_paths:
|
74
|
+
- lib
|
75
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
76
|
+
requirements:
|
77
|
+
- - ">="
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
version: 2.3.0
|
80
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
81
|
+
requirements:
|
82
|
+
- - ">="
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
version: '0'
|
85
|
+
requirements: []
|
86
|
+
rubyforge_project:
|
87
|
+
rubygems_version: 2.7.3
|
88
|
+
signing_key:
|
89
|
+
specification_version: 4
|
90
|
+
summary: Colorize output by defined set of regex rules
|
91
|
+
test_files: []
|