discoball 0.0.2 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md CHANGED
@@ -10,8 +10,16 @@ Usage
10
10
 
11
11
  $ discoball [options] <pattern1 pattern2 ...>
12
12
 
13
+ where options are:
14
+
15
+ * `--group-colors` or `-g`: Color all matches of the same pattern with the same color
16
+ * `--one-color` or `-o`: Highlight all matches with a single color
17
+ * `--match-any` or `-m`: Only print lines matching an input pattern
18
+ * `--match-all` or `-a`: Only print lines matching all input patterns
19
+ * `--help` or `-h`: Print the help message
20
+
13
21
  Examples
14
- --------
22
+ ------------------
15
23
 
16
24
  * Highlight instances of "foo" and "bar" in the text of `myfile.txt`:
17
25
 
@@ -19,7 +27,34 @@ Examples
19
27
 
20
28
  * Highlight paths of processes running out of `/usr/sbin/`:
21
29
 
22
- $ ps -ef | discoball '/usr/sbin/.*$'
30
+ $ ps -ef | discoball --one_color --match '/usr/sbin/.*$'
31
+
32
+ * I wrote discoball for use with [Steve Losh's todo-list tool, t](https://github.com/sjl/t). I put tags on
33
+ my tasks annotated with `+` (inspired by [Todo.txt](http://todotxt.com/)):
34
+
35
+ $ t Make an appointment with the dentist +health
36
+
37
+ When I list my tasks (using `t`), I use discoball to highlight the tags with different colors:
38
+
39
+ $ t | discoball '\+\S+'
40
+
41
+ I can even do some fancier stuff to list particular labels. I have the following function defined in my
42
+ `.bashrc`:
43
+
44
+ ``` bash
45
+ function tl() {
46
+ if [ -z "$1" ]; then
47
+ t | discoball '\+\S+'
48
+ else
49
+ t | discoball -a "${@/#/\+}"
50
+ fi
51
+ }
52
+ ```
53
+
54
+ I can use this as follows:
55
+
56
+ $ tl # ~> Show the list of tasks, with tags highlighted
57
+ $ tl health urgent # ~> Show only tasks tagged with 'health' and 'urgent'
23
58
 
24
59
  Installation
25
60
  ------------
data/bin/discoball CHANGED
@@ -13,22 +13,32 @@ where options are:
13
13
  EOS
14
14
  opt :group_colors, "Color all matches of the same pattern with the same color", :default => false
15
15
  opt :one_color, "Highlight all matches with a single color", :default => false
16
- opt :match, "Only print lines with matches", :default => false
16
+ opt :match_any, "Only print lines matching an input pattern", :default => false
17
+ opt :match_all, "Only print lines matching all input patterns", :default => false
17
18
  end
18
19
 
19
20
  color_mode_array = [:group_colors, :one_color].select { |option| options[option] }
20
21
  if color_mode_array.size > 1
21
22
  Trollop::die "Only one of --group-colors or --one-color may be set"
22
23
  end
23
-
24
24
  if color_mode_array.empty?
25
25
  color_mode = :individual
26
26
  else
27
27
  color_mode = color_mode_array[0]
28
28
  end
29
29
 
30
+ match_mode_array = [:match_any, :match_all].select { |option| options[option] }
31
+ if match_mode_array.size > 1
32
+ Trollop::die "Only one of --match-any or --match-all may be set"
33
+ end
34
+ if match_mode_array.empty?
35
+ match_mode = :all
36
+ else
37
+ match_mode = match_mode_array[0]
38
+ end
39
+
30
40
  patterns = ARGV.map { |pattern| Regexp.new(pattern) }
31
- highlighter = Discoball::Highlighter.new(patterns, color_mode, options[:match])
41
+ highlighter = Discoball::Highlighter.new(patterns, color_mode, match_mode)
32
42
  STDIN.each do |line|
33
43
  filtered = highlighter.filter(line)
34
44
  puts filtered unless filtered.nil?
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.2"
3
+ s.version = "0.0.3"
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=
data/lib/discoball.rb CHANGED
@@ -7,16 +7,19 @@ module Discoball
7
7
  SINGLE_COLOR = :blue
8
8
 
9
9
  class Highlighter
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
+ # Patterns is an array of patterns to match. There are two options that can be changed:
11
+ # * color_mode
12
+ # -:individual - each unique string matching one of the patterns will be a different color.
13
+ # -:group_colors - the matches corresponding to each pattern will be the same color.
14
+ # -:one_color - all matches will be a single color.
15
+ # * match_mode
16
+ # -:all - all lines are returned
17
+ # -:match_any - lines matching any pattern are returned
18
+ # -:match_all - only lines matching every pattern are returned
19
+ def initialize(patterns, color_mode = :individual, match_mode = :all)
17
20
  @patterns = patterns
18
21
  @color_mode = color_mode
19
- @match_only = match_only
22
+ @match_mode = match_mode
20
23
  @color_stack = String.colors.reject { |color| UNUSABLE_COLORS.any? { |unusable| color =~ unusable } }
21
24
  @color_assignments = {}
22
25
  if color_mode == :group_colors
@@ -25,21 +28,29 @@ module Discoball
25
28
  end
26
29
 
27
30
  def filter(line)
28
- match_found = false
31
+ match_found = {}
32
+ @patterns.each { |pattern| match_found[pattern] = false }
33
+
29
34
  case @color_mode
30
35
  when :one_color
31
- matches = @patterns.flat_map { |pattern| line.scan(pattern) }.uniq
32
- match_found ||= !matches.empty?
36
+ matches = @patterns.flat_map { |pattern|
37
+ m = line.scan(pattern)
38
+ match_found[pattern] = true unless m.empty?
39
+ m
40
+ }.uniq
33
41
  matches.each { |match| highlight!(line, match, SINGLE_COLOR) }
34
42
  when :group_colors
35
43
  @patterns.each do |pattern|
36
44
  matches = line.scan(pattern).uniq
37
- match_found ||= !matches.empty?
45
+ match_found[pattern] = true unless matches.empty?
38
46
  matches.each { |match| highlight!(line, match, @color_assignments[pattern]) }
39
47
  end
40
48
  when :individual
41
- matches = @patterns.flat_map { |pattern| line.scan(pattern) }.uniq
42
- match_found ||= !matches.empty?
49
+ matches = @patterns.flat_map { |pattern|
50
+ m = line.scan(pattern)
51
+ match_found[pattern] = true unless m.empty?
52
+ m
53
+ }.uniq
43
54
  matches.each do |match|
44
55
  unless @color_assignments.include? match
45
56
  @color_assignments[match] = pop_rotate
@@ -47,7 +58,15 @@ module Discoball
47
58
  matches.each { |match| highlight!(line, match, @color_assignments[match]) }
48
59
  end
49
60
  end
50
- (@match_only && !match_found) ? nil : line
61
+
62
+ case @match_mode
63
+ when :match_any
64
+ match_found.any? { |pattern, found| found } ? line : nil
65
+ when :match_all
66
+ match_found.all? { |pattern, found| found } ? line : nil
67
+ else
68
+ line
69
+ end
51
70
  end
52
71
 
53
72
  private
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: discoball
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.0.2
5
+ version: 0.0.3
6
6
  platform: ruby
7
7
  authors:
8
8
  - Caleb Spare
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-05-10 00:00:00 -07:00
13
+ date: 2011-05-11 00:00:00 -07:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency