Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #3 from sharat87/master

Allow for groups in the pattern, use the first group's match to highlight
  • Loading branch information...
commit 4c65575786a1266bccf0b658bded8c40677c5840 2 parents 0ba8aae + 9523eab
@cespare authored
Showing with 23 additions and 4 deletions.
  1. +2 −1  README.md
  2. +21 −3 lib/discoball.rb
View
3  README.md
@@ -3,7 +3,8 @@ discoball
`discoball` is a tool to filter streams and colorize patterns. It functions somewhat like `egrep --color`,
except that it can highlight multiple patterns (in different colors). Patterns are arbitrary ruby regexes that
-are matched against the entire line.
+are matched against the entire line. The regex may contain groups, in which case, only the first group's match
+text is highlighted.
Usage
-----
View
24 lib/discoball.rb
@@ -35,20 +35,38 @@ def filter(line)
case @color_mode
when :one_color
matches = @patterns.reduce([]) { |memo, pattern| # No Array#flat_map in Ruby 1.8 :\
- m = line.scan(pattern)
+ m = line.scan(pattern).map do |mstr|
+ if mstr.class == Array # Has group(s)
+ mstr[0]
+ else
+ mstr
+ end
+ end
match_found[pattern] = true unless m.empty?
memo += m
}.uniq
matches.each { |match| highlight!(line, match, SINGLE_COLOR) }
when :group_colors
@patterns.each do |pattern|
- matches = line.scan(pattern).uniq
+ matches = line.scan(pattern).map do |mstr|
+ if mstr.class == Array
+ mstr[0]
+ else
+ mstr
+ end
+ end.uniq
match_found[pattern] = true unless matches.empty?
matches.each { |match| highlight!(line, match, @color_assignments[pattern]) }
end
when :individual
matches = @patterns.reduce([]) { |memo, pattern|
- m = line.scan(pattern)
+ m = line.scan(pattern).map do |mstr|
+ if mstr.class == Array
+ mstr[0]
+ else
+ mstr
+ end
+ end
match_found[pattern] = true unless m.empty?
memo += m
}.uniq
Please sign in to comment.
Something went wrong with that request. Please try again.