colorit 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,15 @@
1
+
2
+
3
+ ## 0.3.0
4
+
5
+ * added support for regular expressions for color'd strings
6
+ * gem-ified and prepared for testability
7
+
8
+ ## 0.2.0
9
+
10
+ * added black as color (black on white)
11
+
12
+ ## 0.1.0
13
+
14
+ * refactored to be able to pipe to colorit
15
+ * removed hard dependency to Perl and `tail` (d'oh!)
@@ -0,0 +1,50 @@
1
+ # colorit
2
+
3
+ Colorit colors your console outputs with the use of regular expressions and marks words for you in fancy colors.
4
+
5
+ ## Example
6
+
7
+ This simple example will `tail -f` the systems auth.log file and mark all occurences of *root* in red and of *CRON* in green.
8
+
9
+ ```bash
10
+ tail -f /var/log/auth.log | colorit --red root --green CRON
11
+ ```
12
+
13
+ The next is another easy example that will mark the file names in blue, when more than one log file is tail'd:
14
+
15
+ ```bash
16
+ tail -f /var/log/*.log | colorit --blue "==> (.*) <=="
17
+ ```
18
+
19
+ ## Installation
20
+
21
+ * `[sudo] gem install colorit`
22
+
23
+ ## Author
24
+
25
+ Original author: Dominik Liebler <liebler.dominik@googlemail.com>
26
+
27
+ ## License
28
+
29
+ (The MIT License)
30
+
31
+ Copyright (c) 2012 Dominik Liebler
32
+
33
+ Permission is hereby granted, free of charge, to any person obtaining
34
+ a copy of this software and associated documentation files (the
35
+ 'Software'), to deal in the Software without restriction, including
36
+ without limitation the rights to use, copy, modify, merge, publish,
37
+ distribute, sublicense, and/or sell copies of the Software, and to
38
+ permit persons to whom the Software is furnished to do so, subject to
39
+ the following conditions:
40
+
41
+ The above copyright notice and this permission notice shall be
42
+ included in all copies or substantial portions of the Software.
43
+
44
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
45
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
46
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
47
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
48
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
49
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
50
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ lib = ::File.expand_path('../../lib', __FILE__)
4
+ require "#{lib}/colorit"
5
+
6
+ colorit_command = Colorit::Command.new(Colorit::Config.new)
7
+ colorit_command.parse_options
8
+ colorit_command.run(ARGF)
@@ -0,0 +1,14 @@
1
+ lib = File.expand_path('../../lib', __FILE__)
2
+ $:.unshift lib unless $:.include?(lib)
3
+
4
+ module Colorit
5
+ def self.load_modules
6
+ require 'colorit/colors'
7
+ require 'colorit/string'
8
+ require 'colorit/version'
9
+ require 'colorit/command'
10
+ require 'colorit/config'
11
+ end
12
+ end
13
+
14
+ Colorit.load_modules
@@ -0,0 +1,34 @@
1
+ module Colorit
2
+ module Colors
3
+ include Enumerable
4
+
5
+ COLORS = {
6
+ :reset => "\e[0m",
7
+ :red => "\e[1;37;41m",
8
+ :black => "\e[1;30;47m",
9
+ :green => "\e[1;32m",
10
+ :yellow => "\e[1;33m",
11
+ :blue => "\e[1;34m",
12
+ :magenta => "\e[1;35m",
13
+ :cyan => "\e[1;36m",
14
+ }
15
+
16
+ # to implement Enumerable
17
+ #
18
+ def self.each(&block)
19
+ self.all().each(&block)
20
+ end
21
+
22
+ # get reset preset
23
+ #
24
+ def self.reset
25
+ Colorit::Colors::COLORS[:reset]
26
+ end
27
+
28
+ # get all "real" colors
29
+ #
30
+ def self.all
31
+ Colorit::Colors::COLORS.reject {|k,v| k == :reset }
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,74 @@
1
+ require 'optparse'
2
+
3
+ module Colorit
4
+ class Command
5
+ #
6
+ # @param Colorit::Config config
7
+ #
8
+ def initialize(config)
9
+ @config = config
10
+ end
11
+
12
+ def parse_options
13
+ OptionParser.new do |opts|
14
+ opts.banner = "Usage: colorit [options]"
15
+
16
+ # add a CLI option to every color
17
+ Colorit::Colors.each do |key,color|
18
+ opts.on("-#{key.to_s[0]}", "--#{key} WORD", "highlight WORD in #{color}#{key}#{Colorit::Colors.reset()}") do |word|
19
+ @config.send(key, word)
20
+ end
21
+ end
22
+
23
+ opts.on('--debug', 'show debug output') do
24
+ @config.debug = true
25
+ end
26
+
27
+ opts.on_tail("--version", "show version") do
28
+ puts Colorit::VERSION.join('.')
29
+ exit
30
+ end
31
+ end.parse!
32
+ end
33
+
34
+ # run the command line tool on the stream_source (ARGF in most cases)
35
+ #
36
+ def run(stream_source)
37
+ colors = prepare_patterns
38
+
39
+ begin
40
+ stream_source.each do |line|
41
+ colors.each do |color, pattern|
42
+ line = line.colorize(pattern, color)
43
+ end
44
+
45
+ puts line
46
+ end
47
+ rescue SystemExit, Interrupt
48
+ # do nothing but leaving the loop to exit ...
49
+ end
50
+ end
51
+
52
+ private
53
+
54
+ # prepare all regexp patterns for all colors
55
+ #
56
+ # @return Hash
57
+ #
58
+ def prepare_patterns
59
+ colors = {}
60
+ @config.colors.each do |color, words|
61
+ if words.length > 1
62
+ word_pattern = (words.map { |h| "(#{h})" }).join('|')
63
+ word_pattern = Regexp.new(word_pattern)
64
+ else
65
+ word_pattern = Regexp.new(words[0])
66
+ end
67
+
68
+ colors[color] = word_pattern
69
+ end
70
+
71
+ colors
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,26 @@
1
+ module Colorit
2
+ class Config
3
+ attr_reader :colors, :debug
4
+
5
+ # add setters for every color
6
+ #
7
+ Colorit::Colors.each do |key,value|
8
+ if key != :reset
9
+ define_method(key) do |word|
10
+ @colors[key] = [] unless @colors.include?(key)
11
+ @colors[key] << word
12
+ end
13
+ end
14
+ end
15
+
16
+ def initialize
17
+ @debug = false
18
+ @colors = {}
19
+ end
20
+
21
+ def debug=(debug)
22
+ # todo: ensure boolean here!
23
+ @debug = debug
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,12 @@
1
+ # monkey-patch String to color strings and patterns in strings
2
+ #
3
+ class String
4
+
5
+ # colorize an arbitrary string
6
+ #
7
+ def colorize(pattern, color)
8
+ gsub(pattern) do
9
+ Colorit::Colors.all[color] + $& + Colorit::Colors.reset()
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,3 @@
1
+ module Colorit
2
+ VERSION = %w(0 3 0) unless defined?(::Colorit::VERSION)
3
+ end
metadata ADDED
@@ -0,0 +1,56 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: colorit
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.3.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Dominik Liebler
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-12-12 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: colorit colors your console outputs using regular expressions to emphasize
15
+ special words in e.g. logfiles
16
+ email:
17
+ - liebler.dominik@googlemail.com
18
+ executables:
19
+ - colorit
20
+ extensions: []
21
+ extra_rdoc_files: []
22
+ files:
23
+ - bin/colorit
24
+ - lib/colorit/colors.rb
25
+ - lib/colorit/command.rb
26
+ - lib/colorit/config.rb
27
+ - lib/colorit/string.rb
28
+ - lib/colorit/version.rb
29
+ - lib/colorit.rb
30
+ - README.md
31
+ - CHANGELOG.md
32
+ homepage: https://github.com/domnikl/colorit
33
+ licenses: []
34
+ post_install_message:
35
+ rdoc_options: []
36
+ require_paths:
37
+ - lib
38
+ required_ruby_version: !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ required_rubygems_version: !ruby/object:Gem::Requirement
45
+ none: false
46
+ requirements:
47
+ - - ! '>='
48
+ - !ruby/object:Gem::Version
49
+ version: 1.3.6
50
+ requirements: []
51
+ rubyforge_project:
52
+ rubygems_version: 1.8.16
53
+ signing_key:
54
+ specification_version: 3
55
+ summary: colors your console outputs
56
+ test_files: []