riel 1.1.12 → 1.1.13

Sign up to get free protection for your applications and to get access to all the features.
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/lib/riel/text.rb CHANGED
@@ -1,415 +1,4 @@
1
1
  #!/usr/bin/ruby -w
2
2
  # -*- ruby -*-
3
3
 
4
- module Text
5
-
6
- # Highlights text using either ANSI terminal codes, or HTML.
7
-
8
- # Note that the foreground and background sections can have modifiers
9
- # (attributes).
10
- #
11
- # Examples:
12
- # black
13
- # blue on white
14
- # bold green on yellow
15
- # underscore bold magenta on cyan
16
- # underscore red on cyan
17
-
18
- class Highlighter
19
-
20
- VERSION = "1.0.4"
21
-
22
- ATTRIBUTES = %w{
23
- none
24
- reset
25
- bold
26
- underscore
27
- underline
28
- blink
29
- negative
30
- concealed
31
- black
32
- red
33
- green
34
- yellow
35
- blue
36
- magenta
37
- cyan
38
- white
39
- on_black
40
- on_red
41
- on_green
42
- on_yellow
43
- on_blue
44
- on_magenta
45
- on_cyan
46
- on_white
47
- }
48
-
49
- NONE = Object.new
50
- HTML = Object.new
51
- ANSI = Object.new
52
-
53
- COLORS = %w{ black red green yellow blue magenta cyan white }
54
- DECORATIONS = %w{ none reset bold underscore underline blink negative concealed }
55
-
56
- BACKGROUND_COLORS = COLORS.collect { |color| "on_#{color}" }
57
- FOREGROUND_COLORS = COLORS
58
-
59
- COLORS_RE = Regexp.new('(?: ' +
60
- # background will be in capture 0
61
- 'on(?:\s+|_) ( ' + COLORS.join(' | ') + ' ) | ' +
62
- # foreground will be in capture 1
63
- '( ' + (COLORS + DECORATIONS).join(' | ') + ' ) ' +
64
- ')', Regexp::EXTENDED);
65
-
66
- DEFAULT_COLORS = [
67
- "black on yellow",
68
- "black on green",
69
- "black on magenta",
70
- "yellow on black",
71
- "magenta on black",
72
- "green on black",
73
- "cyan on black",
74
- "blue on yellow",
75
- "blue on magenta",
76
- "blue on green",
77
- "blue on cyan",
78
- "yellow on blue",
79
- "magenta on blue",
80
- "green on blue",
81
- "cyan on blue",
82
- ]
83
-
84
- attr_reader :colors
85
-
86
- def self.parse_colors str
87
- str.scan(Regexp.new(COLORS_RE)).collect do |color|
88
- color[0] ? "on_" + color[0] : color[1]
89
- end
90
- end
91
-
92
- # returns a list of all color combinations.
93
- def self.all_colors
94
- all_colors = Array.new
95
- ([ nil ] + DECORATIONS).each do |dec|
96
- ([ nil ] + FOREGROUND_COLORS).each do |fg|
97
- ([ nil ] + BACKGROUND_COLORS).each do |bg|
98
- name = [ dec, fg, bg ].compact.join("_")
99
- all_colors << name if name && name.length > 0
100
- end
101
- end
102
- end
103
- all_colors
104
- end
105
-
106
- # todo: change this to use method_missing:
107
- if false
108
- all_colors.each do |name|
109
- meth = Array.new
110
- meth << "def #{name}(&blk)"
111
- meth << " color(\"#{name}\", &blk)"
112
- meth << "end"
113
-
114
- self.class_eval meth.join("\n")
115
- end
116
- end
117
-
118
- def initialize colors
119
- @colors = colors
120
- end
121
-
122
- def highlight str
123
- # implemented by subclasses
124
- end
125
-
126
- def to_s
127
- (@colors || '').join(' ')
128
- end
129
-
130
- def == other
131
- return @colors.sort == other.colors.sort
132
- end
133
-
134
- # Colorizes the given object. If a block is passed, its return value is used
135
- # and the stream is reset. If a String is provided as the object, it is
136
- # colorized and the stream is reset. Otherwise, only the code for the given
137
- # color name is returned.
138
-
139
- def color colorstr, obj = self, &blk
140
- # ^^^^ this is the Module self
141
-
142
- colornames = self.class.parse_colors colorstr
143
- result = names_to_code colornames
144
-
145
- if blk
146
- result << blk.call
147
- result << names_to_code("reset")
148
- elsif obj.kind_of? String
149
- result << obj
150
- result << names_to_code("reset")
151
- end
152
- result
153
- end
154
-
155
- # returns the code for the given color string, which is in the format:
156
- # foreground* [on background]?
157
- #
158
- # Note that the foreground and background sections can have modifiers
159
- # (attributes).
160
- #
161
- # Examples:
162
- # black
163
- # blue on white
164
- # bold green on yellow
165
- # underscore bold magenta on cyan
166
- # underscore red on cyan
167
-
168
- def code str
169
- fg, bg = str.split(/\s*\bon_?\s*/)
170
- (fg ? foreground(fg) : "") + (bg ? background(bg) : "")
171
- end
172
-
173
- # Returns the code for the given background color(s).
174
- def background bgcolor
175
- names_to_code "on_" + bgcolor
176
- end
177
-
178
- # Returns the code for the given foreground color(s).
179
- def foreground fgcolor
180
- fgcolor.split(/\s+/).collect { |fg| names_to_code fg }.join("")
181
- end
182
-
183
- end
184
-
185
- # Highlights using ANSI escape sequences.
186
- class ANSIHighlighter < Highlighter
187
- ATTRIBUTES = Hash[
188
- 'none' => '0',
189
- 'reset' => '0',
190
- 'bold' => '1',
191
- 'underscore' => '4',
192
- 'underline' => '4',
193
- 'blink' => '5',
194
- 'negative' => '7',
195
- 'concealed' => '8',
196
- 'black' => '30',
197
- 'red' => '31',
198
- 'green' => '32',
199
- 'yellow' => '33',
200
- 'blue' => '34',
201
- 'magenta' => '35',
202
- 'cyan' => '36',
203
- 'white' => '37',
204
- 'on_black' => '40',
205
- 'on_red' => '41',
206
- 'on_green' => '42',
207
- 'on_yellow' => '43',
208
- 'on_blue' => '44',
209
- 'on_magenta' => '45',
210
- 'on_cyan' => '46',
211
- 'on_white' => '47',
212
- ]
213
-
214
- RESET = "\e[0m"
215
-
216
- def self.make str
217
- colors = parse_colors str
218
- ANSIHighlighter.new colors
219
- end
220
-
221
- def initialize colors = DEFAULT_COLORS
222
- super
223
- @code = nil
224
- end
225
-
226
- # Returns the escape sequence for the given names.
227
- def names_to_code names
228
- str = ""
229
- names = [ names ] unless names.kind_of? Array
230
- names.each do |name|
231
- code = ATTRIBUTES[name]
232
- if code
233
- str << "\e[#{code}m"
234
- end
235
- end
236
- str
237
- end
238
-
239
- def highlight str
240
- @code ||= begin
241
- @code = @colors.collect do |color|
242
- names_to_code color
243
- end.join ""
244
- end
245
-
246
- @code + str + RESET
247
- end
248
-
249
- end
250
-
251
- # Highlights using HTML. Fonts are highlighted using <span> tags, not <font>.
252
- # Also note that negative is translated to white on black.
253
- # According to http://www.w3.org/TR/REC-CSS2/syndata.html#value-def-color,
254
- # valid color keywords are: aqua, black, blue, fuchsia, gray, green, lime,
255
- # maroon, navy, olive, purple, red, silver, teal, white, and yellow.
256
- # Thus, no magenta or cyan.
257
-
258
- class HTMLHighlighter < Highlighter
259
- def initialize
260
- # we need to know what we're resetting from (bold, font, underlined ...)
261
- @stack = []
262
- end
263
-
264
- # Returns the start tag for the given name.
265
-
266
- def start_style name
267
- case name
268
- when "negative"
269
- "<span style=\"color: white; background-color: black\">"
270
- when /on_(\w+)/
271
- colval = color_value $1
272
- "<span style=\"background-color: #{colval}\">"
273
- else
274
- colval = color_value name
275
- "<span style=\"color: #{colval}\">"
276
- end
277
- end
278
-
279
- # Returns the end tag ("</span>").
280
-
281
- def end_style
282
- "</span>"
283
- end
284
-
285
- def color_value cname
286
- case cname
287
- when "cyan"
288
- "#00FFFF"
289
- when "magenta"
290
- "#FF00FF"
291
- else
292
- cname
293
- end
294
- end
295
-
296
- # Returns the code for the given name.
297
- def names_to_code names
298
- str = ""
299
-
300
- names = [ names ] unless names.kind_of? Array
301
-
302
- names.each do |name|
303
- @stack << name
304
-
305
- case name
306
- when "none", "reset"
307
- @stack.pop
308
- if @stack.length > 0
309
- begin
310
- prev = @stack.pop
311
- case prev
312
- when "bold"
313
- str << "</b>"
314
- when "underscore", "underline"
315
- str << "</u>"
316
- when "blink"
317
- str << "</blink>"
318
- when "concealed"
319
- str << " -->"
320
- else
321
- str << end_style
322
- end
323
- end while @stack.length > 0
324
- end
325
- str
326
- when "bold"
327
- str << "<b>"
328
- when "underscore", "underline"
329
- str << "<u>"
330
- when "blink"
331
- str << "<blink>"
332
- when "concealed"
333
- str << "<!-- "
334
- else
335
- str << start_style(name)
336
- end
337
- end
338
-
339
- str
340
- end
341
- end
342
-
343
- # Does no highlighting.
344
-
345
- class NonHighlighter < Highlighter
346
- def initialize
347
- super nil
348
- end
349
-
350
- # Since the NonHighlighter does no highlighting, and thus its name, this
351
- # returns an empty string.
352
- def names_to_code colorname
353
- ""
354
- end
355
- end
356
-
357
- # An object that can be highlighted. This is used by the String class.
358
-
359
- module Highlightable
360
- # The highlighter for the class in which this module is included.
361
- @@highlighter = ANSIHighlighter.new Text::Highlighter::DEFAULT_COLORS
362
-
363
- if false
364
- Text::Highlighter::ATTRIBUTES.each do |name|
365
- meth = Array.new
366
- meth << "def #{name}(&blk)"
367
- meth << " @@highlighter.color(\"#{name}\", self, &blk)"
368
- meth << "end"
369
-
370
- self.class_eval meth.join("\n")
371
- end
372
- end
373
-
374
- # this dynamically adds methods for individual colors.
375
- def method_missing(meth, *args, &blk)
376
- if Text::Highlighter::all_colors.include? meth.to_s
377
- methdecl = Array.new
378
- methdecl << "def #{meth}(&blk)"
379
- methdecl << " @@highlighter.color(\"#{meth}\", self, &blk)"
380
- methdecl << "end"
381
- self.class.class_eval methdecl.join("\n")
382
- send meth, *args, &blk
383
- else
384
- super
385
- end
386
- end
387
-
388
- # Sets the highlighter for this class. This can be either by type or by
389
- # String.
390
- def highlighter= hl
391
- $VERBOSE = false
392
- @@highlighter = case hl
393
- when Text::Highlighter
394
- hl
395
- when Text::Highlighter::NONE, "NONE", nil
396
- Text::NonHighlighter.new # unless @@highlighter.kind_of?(Text::NonHighlighter)
397
- when Text::Highlighter::HTML, "HTML"
398
- Text::HTMLHighlighter.new # unless @@highlighter.kind_of?(Text::HTMLHighlighter)
399
- when Text::Highlighter::ANSI, "ANSI"
400
- Text::ANSIHighlighter.new
401
- else
402
- Text::NonHighlighter.new
403
- end
404
-
405
- end
406
- end
407
- $HAVE_TEXT_HIGHLIGHT = true
408
- end
409
-
410
- # String is extended to support highlighting.
411
-
412
- class String
413
- include Text::Highlightable
414
- extend Text::Highlightable
415
- end
4
+ require 'riel/text/string'
data/lib/riel.rb CHANGED
@@ -4,7 +4,7 @@ $:.unshift(riellibdir) unless
4
4
  $:.include?(riellibdir) || $:.include?(File.expand_path(riellibdir))
5
5
 
6
6
  module RIEL
7
- VERSION = '1.1.12'
7
+ VERSION = '1.1.13'
8
8
  end
9
9
 
10
10
  rbfiles = Dir[riellibdir + "/riel/**/*.rb"]
@@ -5,18 +5,19 @@ require 'test/unit'
5
5
  require 'riel/command'
6
6
 
7
7
  class CommandTestCase < Test::Unit::TestCase
8
- def test_all
9
- assert_equal [ "/bin/ls\n" ], Command.run("ls", "/bin/ls")
10
- assert_equal [ "/bin/grep\n", "/bin/ls\n" ], Command.run("ls", "/bin/ls", "/bin/grep" )
8
+ def run_command_test expected, *args
9
+ assert_equal expected, Command.run(*args)
10
+ end
11
11
 
12
- lnum = 0
13
- expected = [ "/bin/grep\n", "/bin/ls\n" ]
14
- lines = Command.run("ls", "/bin/ls", "/bin/grep" ) do |line|
15
- assert_equal expected[lnum], line
16
- lnum += 1
17
- end
18
- assert_equal expected, lines
12
+ def test_one_returned
13
+ run_command_test [ "/bin/ls\n" ], "ls", "/bin/ls"
14
+ end
15
+
16
+ def test_two_returned
17
+ run_command_test [ "/bin/grep\n", "/bin/ls\n" ], "ls", "/bin/ls", "/bin/grep"
18
+ end
19
19
 
20
+ def test_with_line_numbers
20
21
  expected = [ "/bin/grep\n", "/bin/ls\n" ]
21
22
  lines = Command.run("ls", "/bin/ls", "/bin/grep" ) do |line, ln|
22
23
  assert_equal expected[ln], line
@@ -5,11 +5,28 @@ require 'test/unit'
5
5
  require 'riel/date'
6
6
 
7
7
  class DateTestCase < Test::Unit::TestCase
8
- def test_simple
9
- assert_equal 28, Date.days_in_month(2010, 2)
10
- assert_equal 31, Date.days_in_month(2010, 1)
11
- assert_equal 29, Date.days_in_month(2008, 2)
12
- assert_equal 30, Date.days_in_month(2007, 6)
13
- assert_equal 31, Date.days_in_month(2010, 12)
8
+ def run_date_test expected, year, month
9
+ dim = Date.days_in_month(year, month)
10
+ assert_equal expected, dim, "year: #{year}; month: #{month}"
11
+ end
12
+
13
+ def test_january
14
+ run_date_test 31, 2010, 1
15
+ end
16
+
17
+ def test_june
18
+ run_date_test 30, 1997, 6
19
+ end
20
+
21
+ def test_december
22
+ run_date_test 31, 1967, 12
23
+ end
24
+
25
+ def test_february_non_leap_year
26
+ run_date_test 28, 2010, 2
27
+ end
28
+
29
+ def test_february_leap_year
30
+ run_date_test 29, 2008, 2
14
31
  end
15
32
  end
@@ -23,7 +23,7 @@ class FileTestCase < Test::Unit::TestCase
23
23
  end
24
24
 
25
25
  def create_binary_file
26
- stringio_so = "/usr/lib/ruby/1.8/i386-linux/stringio.so"
26
+ stringio_so = '/usr/lib/ruby/1.9.1/x86_64-linux/socket.so'
27
27
 
28
28
  tempname = nil
29
29
 
@@ -39,17 +39,28 @@ class FileTestCase < Test::Unit::TestCase
39
39
  tempname
40
40
  end
41
41
 
42
- def test_file_types
42
+ def test_text_is_text
43
43
  text_file = create_text_file
44
44
  assert File.text?(text_file)
45
+ end
46
+
47
+ def test_text_is_not_binary
48
+ text_file = create_text_file
45
49
  assert !File.binary?(text_file)
46
-
50
+ end
51
+
52
+ def test_binary_is_binary
47
53
  if binary_file = create_binary_file
48
- assert !File.text?(binary_file)
49
54
  assert File.binary?(binary_file)
50
55
  end
51
56
  end
52
57
 
58
+ def test_binary_is_not_binary
59
+ if binary_file = create_binary_file
60
+ assert !File.text?(binary_file)
61
+ end
62
+ end
63
+
53
64
  def test_is_file_is_directory
54
65
  file = create_text_file
55
66
 
data/test/riel/io_test.rb CHANGED
@@ -24,11 +24,11 @@ class IOTestCase < Test::Unit::TestCase
24
24
  writelines << "this is line one"
25
25
  writelines << "line two is this"
26
26
 
27
- puts "writelines: <<<#{writelines}>>>"
28
-
29
27
  IO.writelines name, writelines
30
28
 
31
29
  readlines = IO::readlines name
32
30
  assert_equal writelines.collect { |line| line + "\n" }, readlines
31
+
32
+ File.delete name
33
33
  end
34
34
  end
@@ -5,19 +5,40 @@ require 'test/unit'
5
5
  require 'riel/setdiff'
6
6
 
7
7
  class SetDiffTestCase < Test::Unit::TestCase
8
- def run_test a, b, expected
8
+ def assert_diff_type a, b, expected
9
9
  sd = SetDiff.new a, b
10
10
  assert_equal expected, sd.diff_type
11
11
  end
12
12
 
13
- def test_all
14
- run_test %w{ one two three }, %w{ two one three }, :identical
15
- run_test %w{ one two }, %w{ two one three }, :b_contains_a
16
- run_test %w{ one two three }, %w{ two }, :a_contains_b
17
- run_test %w{ one two three }, %w{ four five six }, :no_common
18
- run_test %w{ one two three }, %w{ }, :no_common
19
- run_test %w{ }, %w{ one two three }, :no_common
20
- run_test %w{ }, %w{ }, :no_common
21
- run_test %w{ one two three}, %w{ two three four }, :common
13
+ def test_identical_same_order
14
+ assert_diff_type %w{ one two three }, %w{ one two three }, :identical
15
+ end
16
+
17
+ def test_identical_different_order
18
+ assert_diff_type %w{ one two three }, %w{ two one three }, :identical
19
+ end
20
+
21
+ def test_common
22
+ assert_diff_type %w{ one two three }, %w{ one dos three }, :common
23
+ end
24
+
25
+ def test_b_contains_a
26
+ assert_diff_type %w{ one two }, %w{ two one three }, :b_contains_a
27
+ end
28
+
29
+ def test_a_contains_b
30
+ assert_diff_type %w{ one two three }, %w{ one two }, :a_contains_b
31
+ end
32
+
33
+ def test_no_common_nonempty
34
+ assert_diff_type %w{ one two three }, %w{ four five six }, :no_common
35
+ end
36
+
37
+ def test_no_common_empty
38
+ assert_diff_type %w{ one two three }, %w{ }, :no_common
39
+ end
40
+
41
+ def test_no_common_both_empty
42
+ assert_diff_type %w{ }, %w{ }, :no_common
22
43
  end
23
44
  end
@@ -52,9 +52,4 @@ class StringToRangeTestCase < Test::Unit::TestCase
52
52
  assert_equal "foo", "food" - "d"
53
53
  assert_equal "fd", "food" - %r{o+}
54
54
  end
55
-
56
- def test_highlight
57
- assert_equal "...\e[34mthis\e[0m... is blue", "...this... is blue".highlight(%r{this}, "blue")
58
- assert_equal "...\e[34m\e[42mthis\e[0m... is blue", "...this... is blue".highlight(%r{this}, "blue on green")
59
- end
60
55
  end