riel 1.1.15 → 1.1.16

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -25,20 +25,44 @@ arguments.
25
25
 
26
26
  ### ANSIHighlighter
27
27
 
28
- This class processes strings, wrapping them in ANSI colors. For example:
28
+ This class wraps strings in ANSI colors. For example:
29
29
 
30
30
  "foo".blue
31
31
  "bar".bold.blue.on_white
32
32
 
33
+ It also handles RGB values (on a 0-5 scale, not 0-255):
34
+
35
+ "foo".rgb(3, 1, 5)
36
+ "bar".on_rgb(1, 0, 4)
37
+
38
+ Colors and attributes can be chained:
39
+
40
+ "hey!".rgb(0, 3, 5).on_rgb(5, 2, 1).bold.underline
41
+
42
+ Aliases (names) for colors can be defined:
43
+
44
+ Text::ANSIHighlighter.instance.add_alias :teal, 1, 4, 4
45
+ puts "hello, word".teal
46
+
47
+ Background aliases are supported:
48
+
49
+ Text::ANSIHighlighter.instance.add_alias :on_slate, 0, 1, 1
50
+ puts "hello, word".teal
51
+
33
52
  ### SetDiff
34
53
 
35
54
  This class compares enumerables as sets, A fully including B, B fully including
36
55
  A, or A and B having common elements.
37
56
 
38
- ### AnsiPalette
57
+ ### ANSIPalette
39
58
 
40
59
  This class prints the set of ANSI colors as foregrounds and backgrounds.
41
60
 
61
+ ### RGBPalette
62
+
63
+ This class prints the set of RGB (extended ANSI) colors as foregrounds,
64
+ backgrounds and combinations.
65
+
42
66
  ### ANSIITable
43
67
 
44
68
  This class prints a spreadsheet-like table of data.
data/lib/riel.rb CHANGED
@@ -4,14 +4,5 @@ $:.unshift(riellibdir) unless
4
4
  $:.include?(riellibdir) || $:.include?(File.expand_path(riellibdir))
5
5
 
6
6
  module RIEL
7
- VERSION = '1.1.15'
8
- end
9
-
10
- rbfiles = Dir[riellibdir + "/riel/**/*.rb"]
11
-
12
- rieldir = riellibdir + '/riel'
13
-
14
- rbfiles.sort.each do |rbfile|
15
- rootname = rbfile.sub(Regexp.new('^' + rieldir + '/([\/\w]+)\.rb'), '\1')
16
- require "riel/" + rootname
7
+ VERSION = '1.1.16'
17
8
  end
@@ -2,6 +2,7 @@
2
2
  # -*- ruby -*-
3
3
 
4
4
  require 'riel/log'
5
+ require 'riel/enumerable'
5
6
 
6
7
  module RIEL
7
8
  module ASCIITable
data/lib/riel/optproc.rb CHANGED
@@ -231,10 +231,10 @@ module OptProc
231
231
  end
232
232
 
233
233
  COMBINED_OPTS_RES = [
234
- # -number non-num, then anything
235
- Regexp.new('^ ( - \d+ ) ( \D+.* ) $ ', Regexp::EXTENDED),
236
- # -letter anything
237
- Regexp.new('^ ( - [a-z] ) ( .+ ) $ ', Regexp::EXTENDED)
234
+ # -number non-num
235
+ Regexp.new('^ ( - \d+ ) ( \D+.* ) $ ', Regexp::EXTENDED),
236
+ # -letter anything
237
+ Regexp.new('^ ( - [a-zA-Z] ) ( .+ ) $ ', Regexp::EXTENDED)
238
238
  ]
239
239
 
240
240
  def process_option args
@@ -9,46 +9,34 @@ require 'riel/text/ansi/foregrounds'
9
9
  require 'riel/text/ansi/backgrounds'
10
10
  require 'riel/text/ansi/grey'
11
11
  require 'riel/text/ansi/rgb_color'
12
+ require 'riel/text/ansi/rgb_highlighter'
12
13
  require 'singleton'
13
14
 
14
15
  module Text
15
16
  # Highlights using ANSI escape sequences.
16
17
  class ANSIHighlighter < Highlighter
17
- include Singleton
18
-
19
- DEFAULT_COLORS = [
20
- "black on yellow",
21
- "black on green",
22
- "black on magenta",
23
- "yellow on black",
24
- "magenta on black",
25
- "green on black",
26
- "cyan on black",
27
- "blue on yellow",
28
- "blue on magenta",
29
- "blue on green",
30
- "blue on cyan",
31
- "yellow on blue",
32
- "magenta on blue",
33
- "green on blue",
34
- "cyan on blue",
35
- ]
18
+ include Singleton, RGBHighlighter
36
19
 
37
20
  ATTRIBUTES = Hash.new
38
21
  [ Attributes, Foregrounds, Backgrounds ].each { |cls| ATTRIBUTES.merge! cls.new.colors }
39
22
 
23
+ RGB_RE = Regexp.new '(on_?)?(\d)(\d)(\d)'
24
+
40
25
  def initialize
41
- @aliases = Hash.new
26
+ super
42
27
  @default_codes = nil
43
28
  end
44
29
 
45
30
  def default_codes limit = -1
46
- @default_codes ||= Text::Highlighter::DEFAULT_COLORS.collect { |color| to_codes color }
31
+ @default_codes ||= DEFAULT_COLORS.collect { |color| to_codes color }
47
32
  @default_codes[0 .. limit]
48
33
  end
49
34
 
50
35
  def to_codes str
51
36
  names = parse_colors str
37
+ if names.empty?
38
+ return to_rgb_codes str
39
+ end
52
40
  names_to_code names
53
41
  end
54
42
 
@@ -61,20 +49,7 @@ module Text
61
49
  names = [ names ] unless names.kind_of? Array
62
50
  names.collect { |name| ATTRIBUTES[name].to_s }.join ''
63
51
  end
64
-
65
- def to_rgb str, red, green, blue, meth
66
- color = RGBColor.new red, green, blue
67
- color.send(meth) + str + color.reset
68
- end
69
-
70
- def rgb str, red, green, blue
71
- to_rgb str, red, green, blue, :fg
72
- end
73
-
74
- def on_rgb str, red, green, blue
75
- to_rgb str, red, green, blue, :bg
76
- end
77
-
52
+
78
53
  def to_grey str, value, meth
79
54
  color = Grey.new 232 + value
80
55
  color.send(meth) + str + color.reset
@@ -90,33 +65,5 @@ module Text
90
65
 
91
66
  alias_method :gray, :grey
92
67
  alias_method :on_gray, :on_grey
93
-
94
- def add_alias name, red, green, blue
95
- type = name.to_s[0 .. 2] == 'on_' ? :bg : :fg
96
- color = RGBColor.new red, green, blue, type
97
- @aliases[name] = color
98
- end
99
-
100
- def has_alias? name
101
- @aliases.include? name
102
- end
103
-
104
- def respond_to? meth
105
- has_alias? meth
106
- end
107
-
108
- def method_missing(meth, *args, &blk)
109
- if has_alias? meth
110
- methdecl = Array.new
111
- methdecl << "def #{meth}(str, &blk);"
112
- methdecl << " color = @aliases[:#{meth}];"
113
- methdecl << " color.to_s + str + color.reset;"
114
- methdecl << "end"
115
- self.class.class_eval methdecl.join("\n")
116
- send meth, *args, &blk
117
- else
118
- super
119
- end
120
- end
121
68
  end
122
69
  end
@@ -6,7 +6,7 @@ require 'riel/text/ansi/palette'
6
6
  require 'singleton'
7
7
 
8
8
  module Text
9
- class AnsiPalette < Palette
9
+ class ANSIPalette < Palette
10
10
  include Singleton
11
11
 
12
12
  ANSI_RG = (0 .. 7)
@@ -0,0 +1,73 @@
1
+ #!/usr/bin/ruby -w
2
+ # -*- ruby -*-
3
+
4
+ require 'riel/text/ansi/rgb_color'
5
+ require 'singleton'
6
+
7
+ module Text
8
+ # Highlights using extended (RGB) ANSI escape sequences.
9
+ module RGBHighlighter
10
+ RGB_RE = Regexp.new '(on_?)?(\d)(\d)(\d)'
11
+
12
+ def initialize
13
+ @aliases = Hash.new
14
+ end
15
+
16
+ def to_rgb_codes str
17
+ codes = str.scan(RGB_RE).collect do |bg, r, g, b|
18
+ to_rgb_code r.to_i, g.to_i, b.to_i, bg ? :bg : :fg
19
+ end
20
+ codes.join ''
21
+ end
22
+
23
+ def to_rgb_code red, green, blue, fgbg = :fg
24
+ color = RGBColor.new red, green, blue
25
+ color.send fgbg
26
+ end
27
+
28
+ def to_rgb str, red, green, blue, meth
29
+ color = RGBColor.new red, green, blue
30
+ color.send(meth) + str + color.reset
31
+ end
32
+
33
+ def rgb str, red, green, blue
34
+ to_rgb str, red, green, blue, :fg
35
+ end
36
+
37
+ def on_rgb str, red, green, blue
38
+ to_rgb str, red, green, blue, :bg
39
+ end
40
+
41
+ def rrggbb red, green, blue
42
+ [ red, green, blue ].collect { |x| (x * 6 / 255.0).to_i }
43
+ end
44
+
45
+ def add_alias name, red, green, blue
46
+ type = name.to_s[0 .. 2] == 'on_' ? :bg : :fg
47
+ color = RGBColor.new red, green, blue, type
48
+ @aliases[name] = color
49
+ end
50
+
51
+ def has_alias? name
52
+ @aliases.include? name
53
+ end
54
+
55
+ def respond_to? meth
56
+ has_alias?(meth) || super
57
+ end
58
+
59
+ def method_missing(meth, *args, &blk)
60
+ if has_alias? meth
61
+ methdecl = Array.new
62
+ methdecl << "def #{meth}(str, &blk);"
63
+ methdecl << " color = @aliases[:#{meth}];"
64
+ methdecl << " color.to_s + str + color.reset;"
65
+ methdecl << "end"
66
+ self.class.class_eval methdecl.join("\n")
67
+ send meth, *args, &blk
68
+ else
69
+ super
70
+ end
71
+ end
72
+ end
73
+ end
@@ -22,7 +22,7 @@ module Text
22
22
 
23
23
  BACKGROUND_COLORS = COLORS.collect { |color| "on_#{color}" }
24
24
  FOREGROUND_COLORS = COLORS
25
-
25
+
26
26
  COLORS_RE = Regexp.new('(?: ' +
27
27
  # background will be in capture 0
28
28
  'on(?:\s+|_) ( ' + COLORS.join(' | ') + ' ) | ' +
@@ -21,7 +21,7 @@ module Text
21
21
  methdecl << "end"
22
22
  self.class.class_eval methdecl.join("\n")
23
23
  send meth, *args, &blk
24
- elsif ANSIHighlighter.instance.has_alias? meth
24
+ elsif @@highlighter.has_alias? meth
25
25
  methdecl = Array.new
26
26
  methdecl << "def #{meth}(&blk);"
27
27
  methdecl << " @@highlighter.#{meth}(self, &blk);"
@@ -2,7 +2,6 @@
2
2
  # -*- ruby -*-
3
3
 
4
4
  require 'rubygems'
5
- require 'riel'
6
5
  require 'test/unit'
7
6
  require 'riel/asciitable/table'
8
7
  require 'stringio'
@@ -2,6 +2,7 @@
2
2
  # -*- ruby -*-
3
3
 
4
4
  require 'test/unit'
5
+ require 'riel/text/highlightable'
5
6
  require 'riel/text/ansi/ansi_highlight'
6
7
  require 'riel/text/string'
7
8
 
@@ -62,16 +63,6 @@ class AnsiHighlightTestCase < Test::Unit::TestCase
62
63
  assert_equal "...\e[34m\e[42mthis\e[0m... is blue", hl.gsub("...this... is blue", %r{this}, "blue on green")
63
64
  end
64
65
 
65
- def test_rgb
66
- str = "123".rgb(1, 2, 3)
67
- assert_equal "\x1b[38;5;67m123\e[0m", str
68
- end
69
-
70
- def test_on_rgb
71
- str = "123".on_rgb(1, 2, 3)
72
- assert_equal "\x1b[48;5;67m123\e[0m", str
73
- end
74
-
75
66
  def test_grey
76
67
  str = "123".grey(14)
77
68
  assert_equal "\x1b[38;5;246m123\e[0m", str
@@ -105,39 +96,21 @@ class AnsiHighlightTestCase < Test::Unit::TestCase
105
96
  assert_equal "\e[34mABC\e[0m\e[31m123\e[0m", str + int
106
97
  end
107
98
 
108
- def test_rgb_fg_alias
109
- hl = Text::ANSIHighlighter.instance
110
- hl.add_alias :teal, 1, 4, 4
111
-
112
- str = "ABC".teal
113
- # puts str
114
- assert_equal "\x1b[38;5;80mABC\e[0m", str
115
- end
116
-
117
- def test_rgb_bg_alias
99
+ def assert_to_codes expected, rgbstr
118
100
  hl = Text::ANSIHighlighter.instance
119
- hl.add_alias :on_maroon, 1, 0, 2
120
-
121
- str = "ABC".on_maroon
122
- # puts str
123
- assert_equal "\x1b[48;5;54mABC\e[0m", str
101
+ codes = hl.to_codes rgbstr
102
+ assert_equal expected, codes
124
103
  end
125
104
 
126
105
  def test_to_codes_one_color
127
- hl = Text::ANSIHighlighter.instance
128
- code = hl.to_codes 'blue'
129
- assert_equal "\e[34m", code
106
+ assert_to_codes "\e[34m", 'blue'
130
107
  end
131
108
 
132
109
  def test_to_codes_decoration_color
133
- hl = Text::ANSIHighlighter.instance
134
- code = hl.to_codes 'bold blue'
135
- assert_equal "\e[1m\e[34m", code
110
+ assert_to_codes "\e[1m\e[34m", 'bold blue'
136
111
  end
137
112
 
138
113
  def test_to_codes_background_color
139
- hl = Text::ANSIHighlighter.instance
140
- code = hl.to_codes 'on_blue'
141
- assert_equal "\e[44m", code
114
+ assert_to_codes "\e[44m", 'on_blue'
142
115
  end
143
116
  end
@@ -5,9 +5,9 @@ require 'riel/text/ansi/ansi_palette'
5
5
  require 'riel/text/ansi/palette_tc'
6
6
 
7
7
  module Text
8
- class AnsiPaletteTest < PaletteTest
8
+ class ANSIPaletteTest < PaletteTest
9
9
  def cls
10
- AnsiPalette
10
+ ANSIPalette
11
11
  end
12
12
 
13
13
  def test_ansi_foregrounds
@@ -0,0 +1,99 @@
1
+ #!/usr/bin/ruby -w
2
+ # -*- ruby -*-
3
+
4
+ require 'test/unit'
5
+ require 'riel/text/highlightable'
6
+ require 'riel/text/ansi/rgb_highlighter'
7
+ require 'riel/text/string'
8
+
9
+ class RGBHighlighterTest < Test::Unit::TestCase
10
+ def setup
11
+ String.highlight
12
+ @hl = Text::ANSIHighlighter.instance
13
+ end
14
+
15
+ def test_rgb
16
+ str = "123".rgb(1, 2, 3)
17
+ assert_equal "\x1b[38;5;67m123\e[0m", str
18
+ end
19
+
20
+ def test_on_rgb
21
+ str = "123".on_rgb(1, 2, 3)
22
+ assert_equal "\x1b[48;5;67m123\e[0m", str
23
+ end
24
+
25
+ def test_rgb_chained
26
+ str = "gaudy".rgb(4, 1, 2).on_rgb(1, 5, 3)
27
+ puts "str: #{str}"
28
+ assert_equal "\x1b[48;5;85m\x1b[38;5;168mgaudy\e[0m\e[0m", str
29
+ end
30
+
31
+ def test_rgb_bold_underline
32
+ str = "hey!".rgb(0, 3, 5).on_rgb(5, 2, 1).bold.underline
33
+ puts "str: #{str}"
34
+ assert_equal "\e[4m\e[1m\e[48;5;209m\x1b[38;5;39mhey!\e[0m\e[0m\e[0m\e[0m", str
35
+ end
36
+
37
+ def assert_to_codes expected, rgbstr
38
+ codes = @hl.to_codes rgbstr
39
+ assert_equal expected, codes
40
+ end
41
+
42
+ def test_to_rgb_code
43
+ code = @hl.to_rgb_code 1, 3, 5
44
+ assert_equal "\e[38;5;75m", code
45
+ end
46
+
47
+ def test_to_rgb_code_background
48
+ code = @hl.to_rgb_code 1, 3, 5, :bg
49
+ assert_equal "\e[48;5;75m", code
50
+ end
51
+
52
+ def test_to_codes_rgb_foreground
53
+ assert_to_codes "\e[38;5;75m", '135'
54
+ end
55
+
56
+ def test_to_codes_rgb_onbackground
57
+ assert_to_codes "\e[48;5;75m", 'on135'
58
+ end
59
+
60
+ def test_to_codes_rgb_on_background
61
+ assert_to_codes "\e[48;5;75m", 'on_135'
62
+ end
63
+
64
+ def test_rgb_fg_alias
65
+ @hl.add_alias :teal, 1, 4, 4
66
+
67
+ str = "ABC".teal
68
+ assert_equal "\x1b[38;5;80mABC\e[0m", str
69
+ end
70
+
71
+ def test_rgb_bg_alias
72
+ @hl.add_alias :on_maroon, 1, 0, 2
73
+
74
+ str = "ABC".on_maroon
75
+ assert_equal "\x1b[48;5;54mABC\e[0m", str
76
+ end
77
+
78
+ def add_rrggbb name, red, green, blue
79
+ rr, gg, bb = @hl.rrggbb(red, green, blue)
80
+ @hl.add_alias name, rr, gg, bb
81
+ end
82
+
83
+ def test_rgb_multiple_aliases
84
+ add_rrggbb :on_dahlia_mauve, 168, 83, 135
85
+ add_rrggbb :mauve_chalk, 228, 211, 210
86
+
87
+ str = "mauve chalk on dahlia".mauve_chalk.on_dahlia_mauve
88
+ puts str
89
+ assert_equal "\x1b[48;5;133m\x1b[38;5;224mmauve chalk on dahlia\e[0m\e[0m", str
90
+ end
91
+
92
+ def test_rrggbb
93
+ rr, gg, bb = @hl.rrggbb(122, 212, 67)
94
+ puts "rr, gg, bb: #{rr}, #{gg}, #{bb}"
95
+
96
+ rr, gg, bb = @hl.rrggbb(121, 211, 62)
97
+ puts "rr, gg, bb: #{rr}, #{gg}, #{bb}"
98
+ end
99
+ end
@@ -2,6 +2,7 @@
2
2
  # -*- ruby -*-
3
3
 
4
4
  require 'test/unit'
5
+ require 'riel/text/highlightable'
5
6
  require 'riel/text/ansi/ansi_highlight'
6
7
  require 'riel/text/string'
7
8
 
@@ -11,10 +12,6 @@ class HighlightableTestCase < Test::Unit::TestCase
11
12
  assert_equal "\e[34mfoo\e[0m", "foo".blue
12
13
  end
13
14
 
14
- class String
15
- include Text::Highlightable
16
- end
17
-
18
15
  def test_string_include
19
16
  assert_equal "\e[34mfoo\e[0m", "foo".blue
20
17
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: riel
3
3
  version: !ruby/object:Gem::Version
4
- hash: 13
4
+ hash: 51
5
5
  prerelease:
6
6
  segments:
7
7
  - 1
8
8
  - 1
9
- - 15
10
- version: 1.1.15
9
+ - 16
10
+ version: 1.1.16
11
11
  platform: ruby
12
12
  authors:
13
13
  - Jeff Pace
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2012-12-11 00:00:00 -05:00
18
+ date: 2012-12-15 00:00:00 -05:00
19
19
  default_executable:
20
20
  dependencies: []
21
21
 
@@ -57,6 +57,7 @@ files:
57
57
  - lib/riel/text/ansi/ansi_color.rb
58
58
  - lib/riel/text/ansi/rgb_color.rb
59
59
  - lib/riel/text/ansi/attributes.rb
60
+ - lib/riel/text/ansi/rgb_highlighter.rb
60
61
  - lib/riel/text/ansi/ansi_highlight.rb
61
62
  - lib/riel/text/ansi/grey_palette.rb
62
63
  - lib/riel/text/ansi/ansi_palette.rb
@@ -102,6 +103,7 @@ files:
102
103
  - test/riel/text/ansi/rgb_palette_test.rb
103
104
  - test/riel/text/ansi/grey_palette_test.rb
104
105
  - test/riel/text/ansi/ansi_highlight_test.rb
106
+ - test/riel/text/ansi/rgb_highlighter_test.rb
105
107
  - test/riel/text/string_test.rb
106
108
  - test/riel/text/highlightable_test.rb
107
109
  - test/riel/integer_test.rb
@@ -169,6 +171,7 @@ test_files:
169
171
  - test/riel/text/ansi/rgb_palette_test.rb
170
172
  - test/riel/text/ansi/grey_palette_test.rb
171
173
  - test/riel/text/ansi/ansi_highlight_test.rb
174
+ - test/riel/text/ansi/rgb_highlighter_test.rb
172
175
  - test/riel/text/string_test.rb
173
176
  - test/riel/text/highlightable_test.rb
174
177
  - test/riel/integer_test.rb