discoball 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.md ADDED
@@ -0,0 +1,20 @@
1
+ discoball
2
+ =========
3
+
4
+ `discoball` is a tool to filter streams and colorize patterns. It functions somewhat like `egrep --color`,
5
+ except that it can highlight multiple patterns (in different colors). Patterns are arbitrary ruby regexes that
6
+ are matched against the entire line.
7
+
8
+ Usage:
9
+
10
+ $ discoball [options] <pattern1 pattern2 ...>
11
+
12
+ Examples:
13
+
14
+ * Highlight instances of "foo" and "bar" in the text of `myfile.txt`:
15
+
16
+ $ cat myfile.txt | discoball foo bar
17
+
18
+ * Highlight paths of processes running out of `/usr/sbin/`:
19
+
20
+ $ ps -ef | discoball '/usr/sbin/.*$'
data/bin/discoball ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "discoball"
4
+
5
+ highlighter = Discoball::Highlighter.new(ARGV.map { |pattern| Regexp.new(pattern) })
6
+ STDIN.each { |line| puts highlighter.filter(line) }
data/discoball.gemspec ADDED
@@ -0,0 +1,24 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = "discoball"
3
+ s.version = "0.0.1"
4
+
5
+ s.required_rubygems_version = Gem::Requirement.new(">=0") if s.respond_to? :required_rubygems_version=
6
+ s.specification_version = 2 if s.respond_to? :specification_version=
7
+
8
+ s.author = "Caleb Spare"
9
+ s.email = "cespare@gmail.com"
10
+
11
+ s.description = "A simple stream filter to highlight patterns"
12
+ s.summary = "A simple stream filter to highlight patterns"
13
+ s.homepage = "http://github.com/cespare/discoball"
14
+ s.rubyforge_project = "discoball"
15
+
16
+ s.executables = %w(discoball)
17
+ s.files = %w(
18
+ README.md
19
+ discoball.gemspec
20
+ lib/discoball.rb
21
+ bin/discoball
22
+ )
23
+ s.add_dependency("colorize", ">=0.5.8")
24
+ end
data/lib/discoball.rb ADDED
@@ -0,0 +1,32 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "colorize"
4
+
5
+ module Discoball
6
+ UNUSABLE_COLORS = [/black$/, /^default$/, /white$/, /^light/]
7
+
8
+ class Highlighter
9
+ def initialize(patterns)
10
+ @patterns = patterns
11
+ @color_stack = String.colors.reject { |color| UNUSABLE_COLORS.any? { |unusable| color =~ unusable } }
12
+ @color_assignments = {}
13
+ end
14
+
15
+ def filter(line)
16
+ matches = @patterns.flat_map { |pattern| line.scan(pattern) }.uniq
17
+ matches.each { |match| line.gsub!(match, highlight(match)) }
18
+ line
19
+ end
20
+
21
+ private
22
+
23
+ def highlight(match)
24
+ unless @color_assignments.include? match
25
+ color = @color_stack.pop
26
+ @color_stack.insert(0, color)
27
+ @color_assignments[match] = color
28
+ end
29
+ match.colorize(@color_assignments[match]).underline
30
+ end
31
+ end
32
+ end
metadata ADDED
@@ -0,0 +1,81 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: discoball
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 1
9
+ version: 0.0.1
10
+ platform: ruby
11
+ authors:
12
+ - Caleb Spare
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2011-05-10 00:00:00 -07:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: colorize
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ segments:
29
+ - 0
30
+ - 5
31
+ - 8
32
+ version: 0.5.8
33
+ type: :runtime
34
+ version_requirements: *id001
35
+ description: A simple stream filter to highlight patterns
36
+ email: cespare@gmail.com
37
+ executables:
38
+ - discoball
39
+ extensions: []
40
+
41
+ extra_rdoc_files: []
42
+
43
+ files:
44
+ - README.md
45
+ - discoball.gemspec
46
+ - lib/discoball.rb
47
+ - bin/discoball
48
+ has_rdoc: true
49
+ homepage: http://github.com/cespare/discoball
50
+ licenses: []
51
+
52
+ post_install_message:
53
+ rdoc_options: []
54
+
55
+ require_paths:
56
+ - lib
57
+ required_ruby_version: !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ segments:
63
+ - 0
64
+ version: "0"
65
+ required_rubygems_version: !ruby/object:Gem::Requirement
66
+ none: false
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ segments:
71
+ - 0
72
+ version: "0"
73
+ requirements: []
74
+
75
+ rubyforge_project: discoball
76
+ rubygems_version: 1.3.7
77
+ signing_key:
78
+ specification_version: 2
79
+ summary: A simple stream filter to highlight patterns
80
+ test_files: []
81
+