riel 1.1.16 → 1.1.17

Sign up to get free protection for your applications and to get access to all the features.
Files changed (50) hide show
  1. data/README.md +2 -37
  2. data/lib/riel/log/log.rb +17 -24
  3. data/lib/riel/log/loggable.rb +13 -15
  4. data/lib/riel/log/logger.rb +34 -66
  5. data/lib/riel/log.rb +0 -63
  6. data/lib/riel/optproc.rb +0 -3
  7. data/lib/riel/string.rb +0 -2
  8. data/lib/riel.rb +1 -1
  9. data/test/riel/log/format_test.rb +48 -0
  10. data/test/riel/optproc_test.rb +1 -30
  11. data/test/riel/{log_stack_test.rb → testlog/log_stack_test.rb} +0 -0
  12. data/test/riel/testlog/log_test.rb +254 -0
  13. data/test/riel/testlog/loggable_test.rb +31 -0
  14. metadata +89 -133
  15. data/lib/riel/ansicolor.rb +0 -91
  16. data/lib/riel/asciitable/cell.rb +0 -82
  17. data/lib/riel/asciitable/column.rb +0 -26
  18. data/lib/riel/asciitable/row.rb +0 -105
  19. data/lib/riel/asciitable/table.rb +0 -295
  20. data/lib/riel/text/ansi/ansi_color.rb +0 -31
  21. data/lib/riel/text/ansi/ansi_colors.rb +0 -16
  22. data/lib/riel/text/ansi/ansi_highlight.rb +0 -69
  23. data/lib/riel/text/ansi/ansi_list.rb +0 -17
  24. data/lib/riel/text/ansi/ansi_palette.rb +0 -48
  25. data/lib/riel/text/ansi/attributes.rb +0 -21
  26. data/lib/riel/text/ansi/backgrounds.rb +0 -13
  27. data/lib/riel/text/ansi/color.rb +0 -62
  28. data/lib/riel/text/ansi/foregrounds.rb +0 -12
  29. data/lib/riel/text/ansi/grey.rb +0 -30
  30. data/lib/riel/text/ansi/grey_palette.rb +0 -36
  31. data/lib/riel/text/ansi/palette.rb +0 -18
  32. data/lib/riel/text/ansi/rgb_color.rb +0 -28
  33. data/lib/riel/text/ansi/rgb_highlighter.rb +0 -73
  34. data/lib/riel/text/ansi/rgb_palette.rb +0 -49
  35. data/lib/riel/text/highlight.rb +0 -130
  36. data/lib/riel/text/highlightable.rb +0 -85
  37. data/lib/riel/text/html_highlight.rb +0 -98
  38. data/lib/riel/text/non_highlight.rb +0 -17
  39. data/lib/riel/text/string.rb +0 -10
  40. data/lib/riel/text.rb +0 -4
  41. data/test/riel/asciitable/table_test.rb +0 -77
  42. data/test/riel/log_test.rb +0 -164
  43. data/test/riel/text/ansi/ansi_highlight_test.rb +0 -116
  44. data/test/riel/text/ansi/ansi_palette_test.rb +0 -65
  45. data/test/riel/text/ansi/grey_palette_test.rb +0 -39
  46. data/test/riel/text/ansi/rgb_highlighter_test.rb +0 -99
  47. data/test/riel/text/ansi/rgb_palette_test.rb +0 -122
  48. data/test/riel/text/highlightable_test.rb +0 -24
  49. data/test/riel/text/string_test.rb +0 -47
  50. data/test/riel/text_test.rb +0 -62
@@ -1,82 +0,0 @@
1
- #!/usr/bin/ruby -w
2
- # -*- ruby -*-
3
-
4
- require 'riel/log'
5
-
6
- module RIEL
7
- class Cell
8
- include Loggable
9
-
10
- attr_reader :column
11
- attr_reader :row
12
-
13
- attr_accessor :value
14
- attr_accessor :colors
15
- attr_accessor :span
16
-
17
- def initialize column, row, value = nil, colors = Array.new
18
- @column = column
19
- @row = row
20
- @value = value
21
- @colors = colors
22
- @span = span
23
- end
24
-
25
- def _value width
26
- value.nil? ? "" : value.to_s
27
- end
28
-
29
- def inspect
30
- "(#{@column}, #{@row}) => #{@value}"
31
- end
32
-
33
- def to_s
34
- "(#{@column}, #{@row}) => #{@value}"
35
- end
36
-
37
- def formatted_value width, align
38
- strval = _value width
39
-
40
- if @span
41
- ncolumns = @span - @column
42
- width = width * (1 + ncolumns) + (3 * ncolumns)
43
- end
44
-
45
- diff = width - strval.length
46
-
47
- lhs, rhs = case align
48
- when :left
49
- [ 0, diff ]
50
- when :right
51
- [ diff, 0 ]
52
- when :center
53
- l = diff / 2
54
- r = diff - l
55
- [ l, r ]
56
- else
57
- $stderr.puts "oh my!: #{align}"
58
- end
59
-
60
- str = (" " * lhs) + strval + (" " * rhs)
61
-
62
- if colors
63
- colors.each do |cl|
64
- str = str.send cl
65
- end
66
- end
67
-
68
- str
69
- end
70
- end
71
-
72
- class BannerCell < Cell
73
- def initialize char, col, row
74
- @char = char
75
- super(col, row)
76
- end
77
-
78
- def _value width
79
- @char * width
80
- end
81
- end
82
- end
@@ -1,26 +0,0 @@
1
- #!/usr/bin/ruby -w
2
- # -*- ruby -*-
3
-
4
- require 'riel/log'
5
-
6
- module RIEL
7
- module ASCIITable
8
- class Column
9
- attr_accessor :width
10
- attr_accessor :num
11
- attr_accessor :align
12
- attr_accessor :table
13
-
14
- def initialize table, num, width = nil, align = nil
15
- @table = table
16
- @num = num
17
- @width = width
18
- @align = align
19
- end
20
-
21
- def total fromrow, torow
22
- @table.cells_in_column(@num).inject(0) { |sum, cell| sum + (cell.row >= fromrow && cell.row <= torow ? cell.value.to_i : 0) }
23
- end
24
- end
25
- end
26
- end
@@ -1,105 +0,0 @@
1
- #!/usr/bin/ruby -w
2
- # -*- ruby -*-
3
-
4
- require 'riel/log'
5
- require 'riel/enumerable'
6
-
7
- module RIEL
8
- module ASCIITable
9
- class Row
10
- include Loggable
11
-
12
- attr_accessor :table
13
- attr_accessor :num
14
-
15
- def initialize table, num
16
- @table = table
17
- @num = num
18
- end
19
-
20
- def print columns, align = nil
21
- tocol = @table.last_column
22
- col = 0
23
- fmtdvalues = Array.new
24
- while col <= tocol
25
- aln = align || @table.column_align(col)
26
- cell = @table.cell(col, @num)
27
- width = @table.column_width col
28
- fmtdvalues << cell.formatted_value(width, aln)
29
- if cell.span
30
- col += (cell.span - col)
31
- end
32
- col += 1
33
- end
34
- print_cells fmtdvalues
35
- end
36
-
37
- def print_cells values
38
- $stdout.puts "| " + values.join(" | ") + " |"
39
- end
40
- end
41
-
42
- class BannerRow < Row
43
- def initialize table, char
44
- @char = char
45
- super(table, nil)
46
- end
47
-
48
- def print
49
- banner = (0 .. @table.last_column).collect { |col| BannerCell.new(@char, col, 1) }
50
- bannervalues = banner.collect_with_index do |bc, col|
51
- width = @table.column_width col
52
- bc.formatted_value width, :center
53
- end
54
- print_cells bannervalues
55
- end
56
- end
57
-
58
- class StatRow < Row
59
- def initialize table
60
- firstdatarow = table.data_rows.first
61
- lastdatarow = table.data_rows.last
62
- statrownum = table.last_row + 1
63
-
64
- super table, statrownum
65
-
66
- ncolumns = @table.last_column
67
-
68
- @table.set 0, statrownum, name
69
-
70
- 1.upto(ncolumns - 1) do |col|
71
- val = calculate col, firstdatarow, lastdatarow
72
- @table.set col, statrownum, val
73
- end
74
- end
75
-
76
- def name
77
- end
78
-
79
- def calculate col, firstdatarow, lastdatarow
80
- end
81
- end
82
-
83
- class TotalRow < StatRow
84
- def name
85
- "total"
86
- end
87
-
88
- def calculate col, fromrow, torow
89
- @table.column(col).total fromrow, torow
90
- end
91
- end
92
-
93
- class AverageRow < StatRow
94
- def name
95
- "average"
96
- end
97
-
98
- def calculate col, fromrow, torow
99
- nrows = torow + 1 - fromrow
100
- total = @table.column(col).total fromrow, torow
101
- total / nrows
102
- end
103
- end
104
- end
105
- end
@@ -1,295 +0,0 @@
1
- #!/usr/bin/ruby -w
2
- # -*- ruby -*-
3
-
4
- require 'riel/log'
5
- require 'riel/asciitable/cell'
6
- require 'riel/asciitable/column'
7
- require 'riel/asciitable/row'
8
-
9
- module RIEL
10
- module ASCIITable
11
- class TableData
12
- def keys
13
- end
14
-
15
- def fields
16
- end
17
-
18
- def value key, field
19
- end
20
- end
21
-
22
- class Table
23
- include Loggable
24
-
25
- def initialize args
26
- @cells = Array.new
27
- @cellwidth = args[:cellwidth] || 12
28
- @align = args[:align] || :left
29
- @columns = Array.new
30
- @separator_rows = Hash.new
31
- @default_value = args[:default_value] || ""
32
-
33
- set_headings
34
- set_cells
35
-
36
- if sepival = args[:separators]
37
- add_separators sepival
38
- end
39
-
40
- totrow = args[:has_total_row]
41
- avgrow = args[:has_average_row]
42
-
43
- if totrow || avgrow
44
- add_separator_row '='
45
- if totrow
46
- TotalRow.new(self)
47
- end
48
-
49
- if avgrow
50
- AverageRow.new(self)
51
- end
52
- end
53
-
54
- if args[:has_total_columns]
55
- add_total_columns
56
- end
57
-
58
- if args[:highlight_max_cells]
59
- highlight_max_cells
60
- end
61
- end
62
-
63
- # sets a separator for the row preceding +rownum+. Does not change the
64
- # coordinates for any other cells.
65
- def set_separator_row rownum, char = '-'
66
- @separator_rows[rownum] = char
67
- end
68
-
69
- def add_separator_row char = '-'
70
- @separator_rows[last_row + 1] = char
71
- end
72
-
73
- def add_separators nbetween
74
- # banner every N rows
75
- drows = data_rows
76
-
77
- drows.first.upto((drows.last - 1) / nbetween) do |num|
78
- set_separator_row 1 + num * nbetween, '-'
79
- end
80
- end
81
-
82
- def last_column
83
- @cells.collect { |cell| cell.column }.max
84
- end
85
-
86
- def last_row
87
- @cells.collect { |cell| cell.row }.max
88
- end
89
-
90
- def cells_in_column col
91
- @cells.select { |cell| cell.column == col }
92
- end
93
-
94
- def cells_in_row row
95
- @cells.select { |cell| cell.row == row }
96
- end
97
-
98
- def cell col, row
99
- cl = @cells.detect { |c| c.row == row && c.column == col }
100
- unless cl
101
- cl = Cell.new(col, row, @default_value)
102
- @cells << cl
103
- end
104
- cl
105
- end
106
-
107
- def set_column_align col, align
108
- column(col).align = align
109
- end
110
-
111
- def set_column_width col, width
112
- column(col).width = width
113
- end
114
-
115
- def column_width col
116
- ((c = @columns[col]) && c.width) || @cellwidth
117
- end
118
-
119
- def column_align col
120
- ((c = @columns[col]) && c.align) || @align
121
- end
122
-
123
- def column col
124
- @columns[col] ||= Column.new(self, col, @cellwidth, @align)
125
- end
126
-
127
- def set_cellspan fromcol, tocol, row
128
- cell(fromcol, row).span = tocol
129
- end
130
-
131
- def set col, row, val
132
- cell(col, row).value = val
133
- end
134
-
135
- def set_color col, row, *colors
136
- cell(col, row).colors = colors
137
- end
138
-
139
- def print_row row, align = nil
140
- Row.new(self, row).print @columns, align
141
- end
142
-
143
- def print_banner char = '-'
144
- BannerRow.new(self, char).print
145
- end
146
-
147
- def print_header
148
- # cells in the header are always centered
149
- print_row 0, :center
150
- print_banner
151
- end
152
-
153
- def print
154
- print_header
155
-
156
- (1 .. last_row).each do |row|
157
- if char = @separator_rows[row]
158
- print_banner char
159
- end
160
- print_row row
161
- end
162
- end
163
-
164
- # returns the values in cells, sorted and unique
165
- def sort_values cells
166
- cells.collect { |cell| cell.value }.uniq.sort.reverse
167
- end
168
-
169
- def data_cells_for_row row, offset
170
- cells_for_row row, offset, fields.length * data_cell_span
171
- end
172
-
173
- def get_highlight_colors
174
- Array.new
175
- end
176
-
177
- def highlight_cells_in_row row, offset
178
- cells = data_cells_for_row row, offset
179
- highlight_cells cells
180
- end
181
-
182
- def highlight_cells cells
183
- vals = sort_values cells
184
-
185
- colors = get_highlight_colors
186
-
187
- cells.each do |cell|
188
- idx = vals.index cell.value
189
- if cols = colors[idx]
190
- cell.colors = cols
191
- end
192
- end
193
- end
194
-
195
- def highlight_cells_in_column col
196
- cells = cells_in_column(col)[data_rows.first .. data_rows.last]
197
- highlight_cells cells
198
- end
199
-
200
- def highlight_max_cells
201
- (1 .. last_row).each do |row|
202
- (0 .. (data_cell_span - 1)).each do |offset|
203
- highlight_cells_in_row row, offset
204
- end
205
- end
206
-
207
- 0.upto(1) do |n|
208
- highlight_cells_in_column data_columns.last + n
209
- end
210
- end
211
-
212
- def data_cell_span
213
- 1
214
- end
215
-
216
- def set_headings
217
- cellspan = data_cell_span
218
-
219
- colidx = 0
220
- set colidx, 0, headings[0]
221
-
222
- colidx += 1
223
-
224
- (1 ... headings.length).each do |hi|
225
- set colidx, 0, headings[hi]
226
- if cellspan > 1
227
- set_cellspan colidx, colidx - 1 + cellspan, 0
228
- end
229
- colidx += cellspan
230
- end
231
- end
232
-
233
- def add_total_columns
234
- last_data_col = data_columns.last
235
- totcol = last_data_col + 1
236
-
237
- (1 .. last_row).each do |row|
238
- (0 .. (data_cell_span - 1)).each do |offset|
239
- rowcells = cells_for_row row, offset, last_data_col
240
- total = rowcells.collect { |cell| cell.value }.inject(0) { |sum, num| sum + num }
241
- set totcol + offset, row, total
242
- end
243
- end
244
- end
245
-
246
- # returns the cells up to the given column
247
- def cells_for_row row, offset, maxcol
248
- cells = cells_in_row row
249
- cells = cells[1 .. maxcol]
250
- cells.select_with_index { |cell, cidx| (cidx % data_cell_span) == offset }
251
- end
252
-
253
- def set_cells
254
- rownum = 1
255
- keys.each_with_index do |name, nidx|
256
- set 0, rownum, name
257
-
258
- colidx = 1
259
- fields.each do |field|
260
- (0 .. (data_cell_span - 1)).each do |didx|
261
- val = value name, field, didx
262
- set colidx, rownum, val
263
- colidx += 1
264
- end
265
- end
266
- rownum += 1
267
- end
268
- end
269
-
270
- def data
271
- nil
272
- end
273
-
274
- def keys
275
- data.keys
276
- end
277
-
278
- def fields
279
- data.fields
280
- end
281
-
282
- def value key, field, index
283
- data.value key, field, index
284
- end
285
-
286
- def data_rows
287
- (1 .. keys.length)
288
- end
289
-
290
- def data_columns
291
- (1 .. fields.length * data_cell_span)
292
- end
293
- end
294
- end
295
- end
@@ -1,31 +0,0 @@
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
@@ -1,16 +0,0 @@
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
@@ -1,69 +0,0 @@
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 'riel/text/ansi/rgb_highlighter'
13
- require 'singleton'
14
-
15
- module Text
16
- # Highlights using ANSI escape sequences.
17
- class ANSIHighlighter < Highlighter
18
- include Singleton, RGBHighlighter
19
-
20
- ATTRIBUTES = Hash.new
21
- [ Attributes, Foregrounds, Backgrounds ].each { |cls| ATTRIBUTES.merge! cls.new.colors }
22
-
23
- RGB_RE = Regexp.new '(on_?)?(\d)(\d)(\d)'
24
-
25
- def initialize
26
- super
27
- @default_codes = nil
28
- end
29
-
30
- def default_codes limit = -1
31
- @default_codes ||= DEFAULT_COLORS.collect { |color| to_codes color }
32
- @default_codes[0 .. limit]
33
- end
34
-
35
- def to_codes str
36
- names = parse_colors str
37
- if names.empty?
38
- return to_rgb_codes str
39
- end
40
- names_to_code names
41
- end
42
-
43
- def codes names
44
- names.collect { |name| ATTRIBUTES[name] }.compact
45
- end
46
-
47
- # Returns the escape sequence for the given names.
48
- def names_to_code names
49
- names = [ names ] unless names.kind_of? Array
50
- names.collect { |name| ATTRIBUTES[name].to_s }.join ''
51
- end
52
-
53
- def to_grey str, value, meth
54
- color = Grey.new 232 + value
55
- color.send(meth) + str + color.reset
56
- end
57
-
58
- def grey str, value
59
- to_grey str, value, :fg
60
- end
61
-
62
- def on_grey str, value
63
- to_grey str, value, :bg
64
- end
65
-
66
- alias_method :gray, :grey
67
- alias_method :on_gray, :on_grey
68
- end
69
- end
@@ -1,17 +0,0 @@
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
@@ -1,48 +0,0 @@
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
@@ -1,21 +0,0 @@
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