riel 1.1.12 → 1.1.13

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.
Files changed (44) hide show
  1. data/README.md +41 -0
  2. data/lib/riel/array.rb +0 -2
  3. data/lib/riel/command.rb +0 -3
  4. data/lib/riel/date.rb +0 -4
  5. data/lib/riel/file.rb +0 -1
  6. data/lib/riel/pathname.rb +0 -2
  7. data/lib/riel/setdiff.rb +5 -3
  8. data/lib/riel/string.rb +4 -20
  9. data/lib/riel/text/ansi/ansi_color.rb +31 -0
  10. data/lib/riel/text/ansi/ansi_colors.rb +16 -0
  11. data/lib/riel/text/ansi/ansi_highlight.rb +111 -0
  12. data/lib/riel/text/ansi/ansi_list.rb +17 -0
  13. data/lib/riel/text/ansi/ansi_palette.rb +48 -0
  14. data/lib/riel/text/ansi/attributes.rb +21 -0
  15. data/lib/riel/text/ansi/backgrounds.rb +13 -0
  16. data/lib/riel/text/ansi/color.rb +62 -0
  17. data/lib/riel/text/ansi/foregrounds.rb +12 -0
  18. data/lib/riel/text/ansi/grey.rb +30 -0
  19. data/lib/riel/text/ansi/grey_palette.rb +36 -0
  20. data/lib/riel/text/ansi/palette.rb +18 -0
  21. data/lib/riel/text/ansi/rgb_color.rb +28 -0
  22. data/lib/riel/text/ansi/rgb_palette.rb +49 -0
  23. data/lib/riel/text/highlight.rb +130 -0
  24. data/lib/riel/text/highlightable.rb +85 -0
  25. data/lib/riel/text/html_highlight.rb +98 -0
  26. data/lib/riel/text/non_highlight.rb +17 -0
  27. data/lib/riel/text/string.rb +10 -0
  28. data/lib/riel/text.rb +1 -412
  29. data/lib/riel.rb +1 -1
  30. data/test/riel/command_test.rb +11 -10
  31. data/test/riel/date_test.rb +23 -6
  32. data/test/riel/file_test.rb +15 -4
  33. data/test/riel/io_test.rb +2 -2
  34. data/test/riel/setdiff_test.rb +31 -10
  35. data/test/riel/string_test.rb +0 -5
  36. data/test/riel/text/ansi/ansi_highlight_test.rb +125 -0
  37. data/test/riel/text/ansi/ansi_palette_test.rb +65 -0
  38. data/test/riel/text/ansi/grey_palette_test.rb +39 -0
  39. data/test/riel/text/ansi/rgb_palette_test.rb +122 -0
  40. data/test/riel/text/highlightable_test.rb +27 -0
  41. data/test/riel/text/string_test.rb +48 -0
  42. data/test/riel/text_test.rb +1 -1
  43. metadata +37 -6
  44. data/README +0 -0
data/README.md ADDED
@@ -0,0 +1,41 @@
1
+ RIEL - Extensions to core Ruby libraries
2
+ ============================================================
3
+
4
+ ## DESCRIPTION
5
+
6
+ RIEL is a library that extends core Ruby libraries. It includes a logging
7
+ framework, an option processor, highlighting of strings.
8
+
9
+ ## CLASSES
10
+
11
+ ### Array
12
+
13
+ The core Array class is extended for better output of `to_s`, to output strings
14
+ in the format: [ "this", "is", "a", "test" ], instead of the default,
15
+ "thisisatest".
16
+
17
+ The `rand` method, equivalent to Array#sample, returns a random element from an
18
+ array.
19
+
20
+ ### Command
21
+
22
+ The method `run` runs a command, returning the lines of output, and optionally
23
+ calling a block with each line, and the line number, if the block takes two
24
+ arguments.
25
+
26
+ ### ANSIHighlighter
27
+
28
+ This class processes strings, wrapping them in ANSI colors. For example:
29
+
30
+ "foo".blue
31
+ "bar".bold.blue.on_white
32
+
33
+ ### SetDiff
34
+
35
+ This class compares enumerables as sets, A fully including B, B fully including
36
+ A, or A and B having common elements.
37
+
38
+ ### AnsiPalette
39
+
40
+ This class prints the set of ANSI colors as foregrounds and backgrounds.
41
+
data/lib/riel/array.rb CHANGED
@@ -2,7 +2,6 @@
2
2
  # -*- ruby -*-
3
3
 
4
4
  class Array
5
-
6
5
  $-w = false
7
6
 
8
7
  # Wraps the array with brackets, and inserts a comma and a space between
@@ -17,5 +16,4 @@ class Array
17
16
  def rand
18
17
  return self[Kernel.rand length]
19
18
  end
20
-
21
19
  end
data/lib/riel/command.rb CHANGED
@@ -3,11 +3,9 @@
3
3
  #!ruby -w
4
4
 
5
5
  class Command
6
-
7
6
  # Runs the given command and arguments, returning the lines of output. If a
8
7
  # block is provided, then it will be called with each line of output. If the
9
8
  # block takes two arguments, then the line number is also passed to the block.
10
-
11
9
  def self.run(cmd, *args, &blk)
12
10
  lines = Array.new
13
11
 
@@ -26,5 +24,4 @@ class Command
26
24
 
27
25
  lines
28
26
  end
29
-
30
27
  end
data/lib/riel/date.rb CHANGED
@@ -4,13 +4,9 @@
4
4
 
5
5
  require 'date'
6
6
 
7
-
8
7
  class Date
9
-
10
8
  # Returns the number of days in the given month.
11
-
12
9
  def self.days_in_month year, month
13
10
  (Date.new(year, 12, 31) << (12 - month)).day
14
11
  end
15
-
16
12
  end
data/lib/riel/file.rb CHANGED
@@ -206,5 +206,4 @@ class File
206
206
  end
207
207
  tgt
208
208
  end
209
-
210
209
  end
data/lib/riel/pathname.rb CHANGED
@@ -6,7 +6,6 @@ require 'riel/string'
6
6
  require 'fileutils'
7
7
 
8
8
  class Pathname
9
-
10
9
  # a compliment to the +dirname+, +basename+, and +extname+ family, this returns
11
10
  # the basename without the extension, e.g. "foo" from "/usr/share/lib/foo.bar".
12
11
  def rootname
@@ -24,5 +23,4 @@ class Pathname
24
23
  def mkdirs
25
24
  File.makedirs to_s
26
25
  end
27
-
28
26
  end
data/lib/riel/setdiff.rb CHANGED
@@ -5,10 +5,12 @@
5
5
  # identical, A contains B, B contains A, or A and B contain common elements.
6
6
 
7
7
  class SetDiff
8
- def SetDiff.new a, b
8
+ def self.new a, b
9
9
  allitems = a | b
10
10
 
11
- a_and_b = Array.new
11
+ diff_type = :identical
12
+
13
+ a_and_b = Array.new
12
14
  a_not_in_b = Array.new
13
15
  b_not_in_a = Array.new
14
16
 
@@ -30,7 +32,7 @@ class SetDiff
30
32
  attr_reader :a_and_b, :a_not_in_b, :b_not_in_a
31
33
 
32
34
  def initialize a_and_b, a_not_in_b, b_not_in_a
33
- @a_and_b = a_and_b
35
+ @a_and_b = a_and_b
34
36
  @a_not_in_b = a_not_in_b
35
37
  @b_not_in_a = b_not_in_a
36
38
  end
data/lib/riel/string.rb CHANGED
@@ -13,7 +13,6 @@ require 'riel/text'
13
13
  #
14
14
 
15
15
  class String
16
-
17
16
  #
18
17
  # Returns whether the string ends with the given substring.
19
18
  #
@@ -74,17 +73,17 @@ class String
74
73
  # "1-".to_ranges :min => 0, :max => 8 # [ 1 .. 8 ]
75
74
  #
76
75
  def to_ranges args = Hash.new
77
- min = args[:min] || -Infinity
78
- max = args[:max] || Infinity
76
+ min = args[:min] || -Infinity
77
+ max = args[:max] || Infinity
79
78
  collapse = args[:collapse]
80
79
 
81
80
  ranges = Array.new
82
81
  self.split(%r{\s*,\s*}).each do |section|
83
- md = section.match(RANGE_REGEXP)
82
+ md = section.match(RANGE_REGEXP)
84
83
  next unless md
85
84
 
86
85
  from = String._matchdata_to_number md, 1, min
87
- to = String._has_matchdata?(md, 2) ? String._matchdata_to_number(md, 3, max) : from
86
+ to = String._has_matchdata?(md, 2) ? String._matchdata_to_number(md, 3, max) : from
88
87
 
89
88
  prevrange = ranges[-1]
90
89
 
@@ -98,20 +97,6 @@ class String
98
97
  ranges
99
98
  end
100
99
 
101
- # :stopdoc:
102
- HIGHLIGHTER = ::Text::ANSIHighlighter.new
103
- # :startdoc:
104
-
105
- #
106
- # Returns a highlighted (colored) version of the string, applying the regular
107
- # expressions in the array, which are paired with the desired color.
108
- #
109
- def highlight re, color
110
- gsub(re) do |match|
111
- HIGHLIGHTER.color color, match
112
- end
113
- end
114
-
115
100
  # :stopdoc:
116
101
  def self._has_matchdata? md, idx
117
102
  md && md[idx] && !md[idx].empty?
@@ -121,5 +106,4 @@ class String
121
106
  _has_matchdata?(md, idx) ? md[idx].to_i : default
122
107
  end
123
108
  # :startdoc:
124
-
125
109
  end
@@ -0,0 +1,31 @@
1
+ #!/usr/bin/ruby -w
2
+ # -*- ruby -*-
3
+
4
+ require 'riel/text/ansi/color'
5
+
6
+ module Text
7
+ # An ANSI color uses only the basic 8 colors.
8
+ class AnsiColor < Color
9
+ def initialize value
10
+ super value, nil
11
+ end
12
+
13
+ def to_s
14
+ "\e[#{value}m"
15
+ end
16
+
17
+ def print_fg
18
+ print fg
19
+ print "#{@value}/."
20
+ print reset
21
+ print ' '
22
+ end
23
+
24
+ def print_bg
25
+ print bg
26
+ print "./#{@value}"
27
+ print reset
28
+ print ' '
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/ruby -w
2
+ # -*- ruby -*-
3
+
4
+ require 'riel/text/ansi/ansi_list'
5
+
6
+ module Text
7
+ class Colors < ANSIList
8
+ COLORS = [ :black, :red, :green, :yellow, :blue, :magenta, :cyan, :white ]
9
+
10
+ def initialize colors, start
11
+ color_to_code = Hash.new
12
+ colors.each_with_index { |color, idx| color_to_code[color.to_s] = start + idx }
13
+ super color_to_code
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,111 @@
1
+ #!/usr/bin/ruby -w
2
+ # -*- ruby -*-
3
+
4
+ require 'riel/text/highlight'
5
+ require 'riel/text/ansi/ansi_color'
6
+ require 'riel/text/ansi/ansi_colors'
7
+ require 'riel/text/ansi/attributes'
8
+ require 'riel/text/ansi/foregrounds'
9
+ require 'riel/text/ansi/backgrounds'
10
+ require 'riel/text/ansi/grey'
11
+ require 'riel/text/ansi/rgb_color'
12
+ require 'singleton'
13
+
14
+ module Text
15
+ # Highlights using ANSI escape sequences.
16
+ 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
+ ]
36
+
37
+ ATTRIBUTES = Hash.new
38
+ [ Attributes, Foregrounds, Backgrounds ].each { |cls| ATTRIBUTES.merge! cls.new.colors }
39
+
40
+ def initialize
41
+ @aliases = Hash.new
42
+ end
43
+
44
+ def codes names
45
+ names.collect { |name| ATTRIBUTES[name] }.compact
46
+ end
47
+
48
+ # Returns the escape sequence for the given names.
49
+ def names_to_code names
50
+ names = [ names ] unless names.kind_of? Array
51
+ names.collect { |name| ATTRIBUTES[name].to_s }.join ''
52
+ end
53
+
54
+ def to_rgb str, red, green, blue, meth
55
+ color = RGBColor.new red, green, blue
56
+ color.send(meth) + str + color.reset
57
+ end
58
+
59
+ def rgb str, red, green, blue
60
+ to_rgb str, red, green, blue, :fg
61
+ end
62
+
63
+ def on_rgb str, red, green, blue
64
+ to_rgb str, red, green, blue, :bg
65
+ end
66
+
67
+ def to_grey str, value, meth
68
+ color = Grey.new 232 + value
69
+ color.send(meth) + str + color.reset
70
+ end
71
+
72
+ def grey str, value
73
+ to_grey str, value, :fg
74
+ end
75
+
76
+ def on_grey str, value
77
+ to_grey str, value, :bg
78
+ end
79
+
80
+ alias_method :gray, :grey
81
+ alias_method :on_gray, :on_grey
82
+
83
+ def add_alias name, red, green, blue
84
+ type = name.to_s[0 .. 2] == 'on_' ? :bg : :fg
85
+ color = RGBColor.new red, green, blue, type
86
+ @aliases[name] = color
87
+ end
88
+
89
+ def has_alias? name
90
+ @aliases.include? name
91
+ end
92
+
93
+ def respond_to? meth
94
+ has_alias? meth
95
+ end
96
+
97
+ def method_missing(meth, *args, &blk)
98
+ if has_alias? meth
99
+ methdecl = Array.new
100
+ methdecl << "def #{meth}(str, &blk);"
101
+ methdecl << " color = @aliases[:#{meth}];"
102
+ methdecl << " color.to_s + str + color.reset;"
103
+ methdecl << "end"
104
+ self.class.class_eval methdecl.join("\n")
105
+ send meth, *args, &blk
106
+ else
107
+ super
108
+ end
109
+ end
110
+ end
111
+ end
@@ -0,0 +1,17 @@
1
+ #!/usr/bin/ruby -w
2
+ # -*- ruby -*-
3
+
4
+ require 'riel/text/ansi/ansi_color'
5
+
6
+ module Text
7
+ class ANSIList
8
+ attr_reader :colors
9
+
10
+ def initialize colors
11
+ @colors = Hash.new
12
+ colors.each do |name, code|
13
+ @colors[name] = AnsiColor.new code
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,48 @@
1
+ #!/usr/bin/ruby -w
2
+ # -*- ruby -*-
3
+
4
+ require 'riel/text/ansi/ansi_color'
5
+ require 'riel/text/ansi/palette'
6
+ require 'singleton'
7
+
8
+ module Text
9
+ class AnsiPalette < Palette
10
+ include Singleton
11
+
12
+ ANSI_RG = (0 .. 7)
13
+
14
+ def each &blk
15
+ ANSI_RG.each do |bg|
16
+ color = AnsiColor.new bg
17
+ blk.call color
18
+ end
19
+ end
20
+
21
+ def print_foregrounds
22
+ puts "ansi foregrounds"
23
+ write_foregrounds
24
+ puts
25
+ puts
26
+ end
27
+
28
+ def print_backgrounds
29
+ puts "ansi backgrounds"
30
+ write_backgrounds
31
+ puts
32
+ puts
33
+ end
34
+
35
+ def print_combinations
36
+ puts "ansi combinations"
37
+ each do |bgcolor|
38
+ puts "bg: #{bgcolor.to_str}"
39
+ each do |fgcolor|
40
+ print bgcolor.bg
41
+ fgcolor.print_fg
42
+ end
43
+ puts
44
+ puts
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,21 @@
1
+ #!/usr/bin/ruby -w
2
+ # -*- ruby -*-
3
+
4
+ require 'riel/text/ansi/ansi_list'
5
+
6
+ module Text
7
+ class Attributes < ANSIList
8
+ def initialize
9
+ super Hash[
10
+ 'none' => '0',
11
+ 'reset' => '0',
12
+ 'bold' => '1',
13
+ 'underscore' => '4',
14
+ 'underline' => '4',
15
+ 'blink' => '5',
16
+ 'negative' => '7',
17
+ 'concealed' => '8',
18
+ ]
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,13 @@
1
+ #!/usr/bin/ruby -w
2
+ # -*- ruby -*-
3
+
4
+ require 'riel/text/ansi/ansi_colors'
5
+
6
+ module Text
7
+ class Backgrounds < Colors
8
+ def initialize
9
+ on_colors = COLORS.collect { |color| "on_#{color}" }
10
+ super on_colors, 40
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,62 @@
1
+ #!/usr/bin/ruby -w
2
+ # -*- ruby -*-
3
+
4
+ module Text
5
+ class Color
6
+ BOLD = "\e[1m"
7
+ RESET = "\x1b[0m"
8
+
9
+ # \e1 == \x1b
10
+
11
+ attr_reader :value
12
+
13
+ def initialize value, type
14
+ @value = value
15
+ @type = type
16
+ end
17
+
18
+ def to_s
19
+ @type == :fg ? fg : bg
20
+ end
21
+
22
+ def fg
23
+ str 38
24
+ end
25
+
26
+ def bg
27
+ str 48
28
+ end
29
+
30
+ def str num
31
+ "\x1b[#{num};5;#{value}m"
32
+ end
33
+
34
+ def bold
35
+ BOLD
36
+ end
37
+
38
+ def reset
39
+ RESET
40
+ end
41
+
42
+ def print_fg
43
+ write fg
44
+ end
45
+
46
+ def print_bg
47
+ write bg
48
+ end
49
+
50
+ def to_str
51
+ sprintf "%03d", @value
52
+ end
53
+
54
+ private
55
+ def write fgbg
56
+ print fgbg
57
+ print to_str
58
+ print reset
59
+ print ' '
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,12 @@
1
+ #!/usr/bin/ruby -w
2
+ # -*- ruby -*-
3
+
4
+ require 'riel/text/ansi/ansi_colors'
5
+
6
+ module Text
7
+ class Foregrounds < Colors
8
+ def initialize
9
+ super COLORS, 30
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,30 @@
1
+ #!/usr/bin/ruby -w
2
+ # -*- ruby -*-
3
+
4
+ require 'riel/text/ansi/color'
5
+
6
+ module Text
7
+ class Grey < Color
8
+ def initialize value
9
+ super value, nil
10
+ end
11
+
12
+ def to_s
13
+ "\e[#{value}m"
14
+ end
15
+
16
+ def print_fg
17
+ print fg
18
+ print to_str
19
+ print reset
20
+ print ' '
21
+ end
22
+
23
+ def print_bg
24
+ print bg
25
+ print to_str
26
+ print reset
27
+ print ' '
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,36 @@
1
+ #!/usr/bin/ruby -w
2
+ # -*- ruby -*-
3
+
4
+ require 'riel/text/ansi/grey'
5
+ require 'riel/text/ansi/palette'
6
+ require 'singleton'
7
+
8
+ module Text
9
+ class GreyPalette < Palette
10
+ include Singleton
11
+
12
+ GREY_RG = (0 .. 23)
13
+
14
+ def each &blk
15
+ count = 0
16
+ GREY_RG.each do |grey|
17
+ code = 232 + grey
18
+ color = Grey.new code
19
+ blk.call color
20
+ count += 1
21
+ puts if count > 0 && (count % 6) == 0
22
+ end
23
+ puts
24
+ end
25
+
26
+ def print_foregrounds
27
+ puts "grey foreground colors"
28
+ write_foregrounds
29
+ end
30
+
31
+ def print_backgrounds
32
+ puts "grey background colors"
33
+ write_backgrounds
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,18 @@
1
+ #!/usr/bin/ruby -w
2
+ # -*- ruby -*-
3
+
4
+ module Text
5
+ class Palette
6
+ def write_foregrounds
7
+ each do |color|
8
+ color.print_fg
9
+ end
10
+ end
11
+
12
+ def write_backgrounds
13
+ each do |color|
14
+ color.print_bg
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,28 @@
1
+ #!/usr/bin/ruby -w
2
+ # -*- ruby -*-
3
+
4
+ require 'riel/text/ansi/color'
5
+
6
+ module Text
7
+ class RGBColor < Color
8
+ attr_reader :red
9
+ attr_reader :green
10
+ attr_reader :blue
11
+
12
+ def initialize red, green, blue, type = :fg
13
+ super((red * 36) + (green * 6) + blue + 16, type)
14
+
15
+ @red = red
16
+ @green = green
17
+ @blue = blue
18
+ end
19
+
20
+ def to_str
21
+ sprintf "%d%d%d", @red, @green, @blue
22
+ end
23
+
24
+ def value
25
+ (@red * 36) + (@green * 6) + @blue + 16
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,49 @@
1
+ #!/usr/bin/ruby -w
2
+ # -*- ruby -*-
3
+
4
+ require 'riel/text/ansi/rgb_color'
5
+ require 'riel/text/ansi/palette'
6
+ require 'singleton'
7
+
8
+ module Text
9
+ class RGBPalette < Palette
10
+ include Singleton
11
+
12
+ def each &blk
13
+ rgbrg = (0 .. 5)
14
+
15
+ count = 0
16
+ rgbrg.each do |red|
17
+ rgbrg.each do |green|
18
+ rgbrg.each do |blue|
19
+ blk.call RGBColor.new(red, green, blue)
20
+ count += 1
21
+ puts if count > 0 && (count % 6) == 0
22
+ end
23
+ end
24
+ puts
25
+ end
26
+ end
27
+
28
+ def print_foregrounds
29
+ puts "rgb foreground colors"
30
+ write_foregrounds
31
+ end
32
+
33
+ def print_backgrounds
34
+ puts "rgb background colors"
35
+ write_backgrounds
36
+ end
37
+
38
+ def print_combinations
39
+ puts "all combinations"
40
+ each do |rgbbg|
41
+ puts "bg: #{rgbbg.to_str}"
42
+ each do |rgbfg|
43
+ print rgbbg.bg
44
+ rgbfg.print_fg
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end