riel 1.1.2 → 1.1.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -9,7 +9,6 @@
9
9
  # 40=black 41=red 42=green 43=yellow 44=blue 45=magenta 46=cyan 47=white
10
10
 
11
11
  module ANSIColor
12
-
13
12
  ATTRIBUTES = Hash[
14
13
  'none' => '0',
15
14
  'reset' => '0',
@@ -58,24 +57,24 @@ module ANSIColor
58
57
  # underscore bold magenta on cyan
59
58
  # underscore red on bold cyan
60
59
 
61
- def ANSIColor.code(str)
60
+ def ANSIColor.code str
62
61
  fg, bg = str.split(/\s*\bon_?\s*/)
63
62
  (fg ? foreground(fg) : "") + (bg ? background(bg) : "")
64
63
  end
65
64
 
66
65
  # returns the code for the given background color(s)
67
- def ANSIColor.background(bgcolor)
66
+ def ANSIColor.background bgcolor
68
67
  make_code("on_" + bgcolor)
69
68
  end
70
69
 
71
70
  # returns the code for the given foreground color(s)
72
- def ANSIColor.foreground(fgcolor)
71
+ def ANSIColor.foreground fgcolor
73
72
  make_code(fgcolor)
74
73
  end
75
74
 
76
75
  protected
77
76
 
78
- def ANSIColor.make_code(str)
77
+ def ANSIColor.make_code str
79
78
  if str
80
79
  str.split.collect do |s|
81
80
  if attr = ATTRIBUTES[s]
@@ -89,5 +88,4 @@ module ANSIColor
89
88
  ""
90
89
  end
91
90
  end
92
-
93
91
  end
data/lib/riel/array.rb CHANGED
@@ -13,6 +13,7 @@ class Array
13
13
  end
14
14
  $-w = true
15
15
 
16
+ # $$$ this is the same as Array.sample
16
17
  def rand
17
18
  return self[Kernel.rand(length)]
18
19
  end
@@ -0,0 +1,82 @@
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
@@ -0,0 +1,26 @@
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
@@ -0,0 +1,106 @@
1
+ #!/usr/bin/ruby -w
2
+ # -*- ruby -*-
3
+
4
+ require 'riel/log'
5
+
6
+ module RIEL
7
+ module ASCIITable
8
+ class Row
9
+ include Loggable
10
+
11
+ attr_accessor :table
12
+ attr_accessor :num
13
+
14
+ def initialize table, num
15
+ @table = table
16
+ @num = num
17
+ end
18
+
19
+ def print columns, align = nil
20
+ tocol = @table.last_column
21
+ col = 0
22
+ fmtdvalues = Array.new
23
+ while col <= tocol
24
+ aln = align || @table.column_align(col)
25
+ cell = @table.cell(col, @num)
26
+ width = @table.column_width col
27
+ fmtdvalues << cell.formatted_value(width, aln)
28
+ if cell.span
29
+ col += (cell.span - col)
30
+ end
31
+ col += 1
32
+ end
33
+ print_cells fmtdvalues
34
+ end
35
+
36
+ def print_cells values
37
+ $stdout.puts "| " + values.join(" | ") + " |"
38
+ end
39
+ end
40
+
41
+ class BannerRow < Row
42
+ def initialize table, char
43
+ @char = char
44
+ super(table, nil)
45
+ end
46
+
47
+ def print
48
+ banner = (0 .. @table.last_column).collect { |col| bc = BannerCell.new(@char, col, 1) }
49
+ bannervalues = banner.collect_with_index do |bc, col|
50
+ width = @table.column_width col
51
+ bc.formatted_value width, :center
52
+ end
53
+ print_cells bannervalues
54
+ end
55
+ end
56
+
57
+ class StatRow < Row
58
+ def initialize table
59
+ firstdatarow = table.data_rows.first
60
+ lastdatarow = table.data_rows.last
61
+ statrownum = table.last_row + 1
62
+
63
+ nrows = lastdatarow + 1 - firstdatarow
64
+
65
+ super table, statrownum
66
+
67
+ ncolumns = @table.last_column
68
+
69
+ @table.set 0, statrownum, name
70
+
71
+ 1.upto(ncolumns - 1) do |col|
72
+ val = calculate col, firstdatarow, lastdatarow
73
+ @table.set col, statrownum, val
74
+ end
75
+ end
76
+
77
+ def name
78
+ end
79
+
80
+ def calculate col, firstdatarow, lastdatarow
81
+ end
82
+ end
83
+
84
+ class TotalRow < StatRow
85
+ def name
86
+ "total"
87
+ end
88
+
89
+ def calculate col, fromrow, torow
90
+ @table.column(col).total fromrow, torow
91
+ end
92
+ end
93
+
94
+ class AverageRow < StatRow
95
+ def name
96
+ "average"
97
+ end
98
+
99
+ def calculate col, fromrow, torow
100
+ nrows = torow + 1 - fromrow
101
+ total = @table.column(col).total fromrow, torow
102
+ avg = total / nrows
103
+ end
104
+ end
105
+ end
106
+ end
@@ -0,0 +1,295 @@
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
@@ -0,0 +1,68 @@
1
+ #!/usr/bin/ruby -w
2
+ # -*- ruby -*-
3
+
4
+ module RIEL
5
+ # File and directory processor, with filtering.
6
+
7
+ class FileDirFilter
8
+ def initialize args
9
+ @dirsonly = args[:dirsonly]
10
+ @filesonly = args[:filesonly]
11
+ @basename = args[:basename]
12
+ @dirname = args[:dirname]
13
+ @extname = args[:extname]
14
+ end
15
+
16
+ def match? fd
17
+ if @dirsonly && !fd.directory?
18
+ false
19
+ elsif @filesonly && !fd.file?
20
+ false
21
+ elsif @basename && @basename != fd.basename.to_s
22
+ false
23
+ elsif @dirname && @dirname != fd.parent.to_s
24
+ false
25
+ elsif @extname && @extname != fd.extname
26
+ false
27
+ else
28
+ true
29
+ end
30
+ end
31
+ end
32
+
33
+ class FileDirProcessor
34
+ def initialize args, filters = Array.new
35
+ @filters = filters
36
+ args.each do |arg|
37
+ process Pathname.new arg
38
+ end
39
+ end
40
+
41
+ def process_file file
42
+ end
43
+
44
+ def process_directory dir
45
+ dir.children.sort.each do |fd|
46
+ next if @filter && @filter.include?(fd.basename.to_s)
47
+ process fd
48
+ end
49
+ end
50
+
51
+ def process fd
52
+ @filters.each do |filter|
53
+ return nil unless filter.match? fd
54
+ end
55
+
56
+ if fd.directory?
57
+ process_directory fd
58
+ elsif fd.file?
59
+ process_file fd
60
+ else
61
+ process_unknown_type fd
62
+ end
63
+ end
64
+
65
+ def process_unknown_type fd
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,23 @@
1
+ #!/usr/bin/ruby -w
2
+ # -*- ruby -*-
3
+
4
+ class Integer
5
+ POS_NEG_NUMERIC_RE = Regexp.new('^([\-\+])?(\d+)$')
6
+
7
+ class << self
8
+ # returns the value as an integer, if it is not negative
9
+ def negative? val
10
+ negnumre = Regexp.new '^(\-\d+)$'
11
+
12
+ if val.nil?
13
+ nil
14
+ elsif val.kind_of? Integer
15
+ val < 0 && val
16
+ elsif md = negnumre.match(val.to_s)
17
+ md[1].to_i
18
+ else
19
+ nil
20
+ end
21
+ end
22
+ end
23
+ end
data/lib/riel/io.rb CHANGED
@@ -3,31 +3,36 @@
3
3
 
4
4
  require 'riel/file'
5
5
 
6
-
7
6
  class IO
8
-
9
- $-w = false
10
-
11
7
  # Reads the stream into an array. It works even when $/ == nil, which
12
8
  # works around a problem in Ruby 1.8.1.
13
- def readlines
14
- contents = []
15
- while ((line = gets) && line.length > 0)
16
- contents << line
9
+ if VERSION == "1.8.1"
10
+ $-w = false
11
+
12
+ def readlines
13
+ contents = []
14
+ while ((line = gets) && line.length > 0)
15
+ contents << line
16
+ end
17
+ contents
17
18
  end
18
- contents
19
+
20
+ $-w = true
19
21
  end
20
22
 
21
- $-w = true
23
+ class << self
24
+ def writelines name, lines
25
+ fpn = File._to_pathname name
26
+
27
+ fpn.open(File::WRONLY | File::TRUNC | File::CREAT) do |f|
28
+ f.puts lines
29
+ end
22
30
 
23
- def self.writelines(name, lines)
24
- fpn = File._to_pathname(name)
31
+ fpn
32
+ end
25
33
 
26
- fpn.open(File::WRONLY | File::TRUNC | File::CREAT) do |f|
27
- f.puts lines
34
+ def numlines io
35
+ io.readlines.size
28
36
  end
29
-
30
- fpn
31
37
  end
32
-
33
38
  end