discoball 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +12 -4
- data/bin/discoball +31 -2
- data/discoball.gemspec +2 -1
- data/lib/discoball.rb +45 -11
- metadata +14 -15
data/README.md
CHANGED
@@ -5,16 +5,24 @@ discoball
|
|
5
5
|
except that it can highlight multiple patterns (in different colors). Patterns are arbitrary ruby regexes that
|
6
6
|
are matched against the entire line.
|
7
7
|
|
8
|
-
Usage
|
8
|
+
Usage
|
9
|
+
-----
|
9
10
|
|
10
11
|
$ discoball [options] <pattern1 pattern2 ...>
|
11
12
|
|
12
|
-
Examples
|
13
|
+
Examples
|
14
|
+
--------
|
13
15
|
|
14
16
|
* Highlight instances of "foo" and "bar" in the text of `myfile.txt`:
|
15
17
|
|
16
|
-
|
18
|
+
$ cat myfile.txt | discoball foo bar
|
17
19
|
|
18
20
|
* Highlight paths of processes running out of `/usr/sbin/`:
|
19
21
|
|
20
|
-
|
22
|
+
$ ps -ef | discoball '/usr/sbin/.*$'
|
23
|
+
|
24
|
+
Installation
|
25
|
+
------------
|
26
|
+
|
27
|
+
The easiest way to get `discoball` is by using RubyGems: `$ gem install discoball`. You can also clone the git
|
28
|
+
repository at `git://github.com/cespare/discoball.git` if you want the latest code.
|
data/bin/discoball
CHANGED
@@ -1,6 +1,35 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
2
|
|
3
3
|
require "discoball"
|
4
|
+
require "trollop"
|
4
5
|
|
5
|
-
|
6
|
-
|
6
|
+
options = Trollop::options do
|
7
|
+
banner <<-EOS
|
8
|
+
discoball is a tool for highlighting patterns in a text stream.
|
9
|
+
|
10
|
+
Usage:
|
11
|
+
$ discoball [options] <pattern1 pattern2 ...>
|
12
|
+
where options are:
|
13
|
+
EOS
|
14
|
+
opt :group_colors, "Color all matches of the same pattern with the same color", :default => false
|
15
|
+
opt :one_color, "Highlight all matches with a single color", :default => false
|
16
|
+
opt :match, "Only print lines with matches", :default => false
|
17
|
+
end
|
18
|
+
|
19
|
+
color_mode_array = [:group_colors, :one_color].select { |option| options[option] }
|
20
|
+
if color_mode_array.size > 1
|
21
|
+
Trollop::die "Only one of --group-colors or --one-color may be set"
|
22
|
+
end
|
23
|
+
|
24
|
+
if color_mode_array.empty?
|
25
|
+
color_mode = :individual
|
26
|
+
else
|
27
|
+
color_mode = color_mode_array[0]
|
28
|
+
end
|
29
|
+
|
30
|
+
patterns = ARGV.map { |pattern| Regexp.new(pattern) }
|
31
|
+
highlighter = Discoball::Highlighter.new(patterns, color_mode, options[:match])
|
32
|
+
STDIN.each do |line|
|
33
|
+
filtered = highlighter.filter(line)
|
34
|
+
puts filtered unless filtered.nil?
|
35
|
+
end
|
data/discoball.gemspec
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
Gem::Specification.new do |s|
|
2
2
|
s.name = "discoball"
|
3
|
-
s.version = "0.0.
|
3
|
+
s.version = "0.0.2"
|
4
4
|
|
5
5
|
s.required_rubygems_version = Gem::Requirement.new(">=0") if s.respond_to? :required_rubygems_version=
|
6
6
|
s.specification_version = 2 if s.respond_to? :specification_version=
|
@@ -21,4 +21,5 @@ Gem::Specification.new do |s|
|
|
21
21
|
bin/discoball
|
22
22
|
)
|
23
23
|
s.add_dependency("colorize", ">=0.5.8")
|
24
|
+
s.add_dependency("trollop", ">=1.16.2")
|
24
25
|
end
|
data/lib/discoball.rb
CHANGED
@@ -4,29 +4,63 @@ require "colorize"
|
|
4
4
|
|
5
5
|
module Discoball
|
6
6
|
UNUSABLE_COLORS = [/black$/, /^default$/, /white$/, /^light/]
|
7
|
+
SINGLE_COLOR = :blue
|
7
8
|
|
8
9
|
class Highlighter
|
9
|
-
|
10
|
+
# Patterns is an array of patterns to match. There are three options that can be changed:
|
11
|
+
# * If color_mode is :individual, then each unique string matching one of the patterns will be a
|
12
|
+
# different color.
|
13
|
+
# * If color_mode is :group_colors, then the matches corresponding to each pattern will be the same color.
|
14
|
+
# * If color_mode is :one_color, then all matches will be a single color.
|
15
|
+
# * If match_only is set, then only matching lines will be returned.
|
16
|
+
def initialize(patterns, color_mode = :individual, match_only = false)
|
10
17
|
@patterns = patterns
|
18
|
+
@color_mode = color_mode
|
19
|
+
@match_only = match_only
|
11
20
|
@color_stack = String.colors.reject { |color| UNUSABLE_COLORS.any? { |unusable| color =~ unusable } }
|
12
21
|
@color_assignments = {}
|
22
|
+
if color_mode == :group_colors
|
23
|
+
@patterns.each { |pattern| @color_assignments[pattern] = pop_rotate }
|
24
|
+
end
|
13
25
|
end
|
14
26
|
|
15
27
|
def filter(line)
|
16
|
-
|
17
|
-
|
18
|
-
|
28
|
+
match_found = false
|
29
|
+
case @color_mode
|
30
|
+
when :one_color
|
31
|
+
matches = @patterns.flat_map { |pattern| line.scan(pattern) }.uniq
|
32
|
+
match_found ||= !matches.empty?
|
33
|
+
matches.each { |match| highlight!(line, match, SINGLE_COLOR) }
|
34
|
+
when :group_colors
|
35
|
+
@patterns.each do |pattern|
|
36
|
+
matches = line.scan(pattern).uniq
|
37
|
+
match_found ||= !matches.empty?
|
38
|
+
matches.each { |match| highlight!(line, match, @color_assignments[pattern]) }
|
39
|
+
end
|
40
|
+
when :individual
|
41
|
+
matches = @patterns.flat_map { |pattern| line.scan(pattern) }.uniq
|
42
|
+
match_found ||= !matches.empty?
|
43
|
+
matches.each do |match|
|
44
|
+
unless @color_assignments.include? match
|
45
|
+
@color_assignments[match] = pop_rotate
|
46
|
+
end
|
47
|
+
matches.each { |match| highlight!(line, match, @color_assignments[match]) }
|
48
|
+
end
|
49
|
+
end
|
50
|
+
(@match_only && !match_found) ? nil : line
|
19
51
|
end
|
20
52
|
|
21
53
|
private
|
22
54
|
|
23
|
-
def highlight(match)
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
55
|
+
def highlight!(line, match, color)
|
56
|
+
line.gsub!(match, match.colorize(color).underline)
|
57
|
+
end
|
58
|
+
|
59
|
+
# Get the next color and put it at the back
|
60
|
+
def pop_rotate
|
61
|
+
color = @color_stack.pop
|
62
|
+
@color_stack.insert(0, color)
|
63
|
+
color
|
30
64
|
end
|
31
65
|
end
|
32
66
|
end
|
metadata
CHANGED
@@ -1,12 +1,8 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: discoball
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
prerelease:
|
5
|
-
|
6
|
-
- 0
|
7
|
-
- 0
|
8
|
-
- 1
|
9
|
-
version: 0.0.1
|
4
|
+
prerelease:
|
5
|
+
version: 0.0.2
|
10
6
|
platform: ruby
|
11
7
|
authors:
|
12
8
|
- Caleb Spare
|
@@ -25,13 +21,20 @@ dependencies:
|
|
25
21
|
requirements:
|
26
22
|
- - ">="
|
27
23
|
- !ruby/object:Gem::Version
|
28
|
-
segments:
|
29
|
-
- 0
|
30
|
-
- 5
|
31
|
-
- 8
|
32
24
|
version: 0.5.8
|
33
25
|
type: :runtime
|
34
26
|
version_requirements: *id001
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: trollop
|
29
|
+
prerelease: false
|
30
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
31
|
+
none: false
|
32
|
+
requirements:
|
33
|
+
- - ">="
|
34
|
+
- !ruby/object:Gem::Version
|
35
|
+
version: 1.16.2
|
36
|
+
type: :runtime
|
37
|
+
version_requirements: *id002
|
35
38
|
description: A simple stream filter to highlight patterns
|
36
39
|
email: cespare@gmail.com
|
37
40
|
executables:
|
@@ -59,21 +62,17 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
59
62
|
requirements:
|
60
63
|
- - ">="
|
61
64
|
- !ruby/object:Gem::Version
|
62
|
-
segments:
|
63
|
-
- 0
|
64
65
|
version: "0"
|
65
66
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
66
67
|
none: false
|
67
68
|
requirements:
|
68
69
|
- - ">="
|
69
70
|
- !ruby/object:Gem::Version
|
70
|
-
segments:
|
71
|
-
- 0
|
72
71
|
version: "0"
|
73
72
|
requirements: []
|
74
73
|
|
75
74
|
rubyforge_project: discoball
|
76
|
-
rubygems_version: 1.
|
75
|
+
rubygems_version: 1.6.2
|
77
76
|
signing_key:
|
78
77
|
specification_version: 2
|
79
78
|
summary: A simple stream filter to highlight patterns
|