Class: TablePrint
- Inherits:
-
Object
- Object
- TablePrint
- Defined in:
- lib/print.rb,
lib/table_print.rb
Constant Summary collapse
- DELIM =
'|'
Instance Method Summary collapse
- #append(*columns) ⇒ Object
- #calc_column(line) ⇒ Object
- #calc_max_widths ⇒ Object
- #char_repeat(n, char) ⇒ Object
- #fill_to_next_column(line) ⇒ Object
- #generate_separator_line(maxwidth) ⇒ Object
- #indent(column) ⇒ Object
-
#initialize(*titles) ⇒ TablePrint
constructor
A new instance of TablePrint.
- #next_unprinted_column(col_array, start = 0) ⇒ Object
- #print_line_normally(maxwidth, line) ⇒ Object
- #print_old_staggered(maxwidth, term_width) ⇒ Object
- #print_staggered(maxwidth, term_width) ⇒ Object
- #print_table ⇒ Object
- #print_table_normally(maxwidth) ⇒ Object
-
#println(line) ⇒ Object
prints a line with DELIMs following in proper columns.
- #separator(width) ⇒ Object
- #set_titles(sep_char, titles) ⇒ Object
- #spaces(n) ⇒ Object
- #strip(line) ⇒ Object
Constructor Details
#initialize(*titles) ⇒ TablePrint
Returns a new instance of TablePrint.
18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/table_print.rb', line 18 def initialize(*titles) @ncols = titles.length @lines = [ ] @width = nil @lines << titles separator = [ ] titles.each { |title| separator << char_repeat(title.length, '-') } @lines << separator end |
Instance Method Details
#append(*columns) ⇒ Object
28 29 30 |
# File 'lib/print.rb', line 28 def append(columns) @lines << columns end |
#calc_column(line) ⇒ Object
46 47 48 |
# File 'lib/print.rb', line 46 def calc_column(line) line.length / @width end |
#calc_max_widths ⇒ Object
76 77 78 79 80 81 82 83 84 |
# File 'lib/print.rb', line 76 def calc_max_widths maxwidth = Array.new(@ncols, 0) @lines.each { |line| line.each_with_index { |value, index| maxwidth[index] = [value.to_s.length, maxwidth[index]].max } } maxwidth end |
#char_repeat(n, char) ⇒ Object
8 9 10 11 12 13 14 |
# File 'lib/print.rb', line 8 def char_repeat(n, char) result = "" (1..n).each { |i| result = result + char } result end |
#fill_to_next_column(line) ⇒ Object
50 51 52 53 |
# File 'lib/print.rb', line 50 def fill_to_next_column(line) tab = @width - (line.length % @width) line = line + spaces(tab-1) + DELIM end |
#generate_separator_line(maxwidth) ⇒ Object
94 95 96 97 98 99 100 |
# File 'lib/print.rb', line 94 def generate_separator_line(maxwidth) line = [] maxwidth.each { |width| line << separator(width) } line end |
#indent(column) ⇒ Object
32 33 34 35 36 37 38 39 |
# File 'lib/print.rb', line 32 def indent(column) if column == 0 "" else segment = spaces(@width-1) + DELIM segment * column end end |
#next_unprinted_column(col_array, start = 0) ⇒ Object
69 70 71 72 73 74 |
# File 'lib/print.rb', line 69 def next_unprinted_column(col_array, start=0) for i in (start...col_array.length) return i if col_array[i] end nil end |
#print_line_normally(maxwidth, line) ⇒ Object
86 87 88 89 90 91 92 |
# File 'lib/print.rb', line 86 def print_line_normally(maxwidth, line) output = "" line.each_with_index { |value, index| output = output + value.to_s + spaces(maxwidth[index] - value.to_s.length + 2) } puts output[0...-2] end |
#print_old_staggered(maxwidth, term_width) ⇒ Object
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/print.rb', line 147 def print_old_staggered(maxwidth, term_width) puts separator(term_width) @lines.each_with_index { |line, index| next if index == 1 output = "" column = 0 line.each_with_index { |value, column| value = value.to_s output = indent(column) if output.length == 0 if value.length > (@width - 1) output = output + value println(output) output = "" else output = output + value + spaces(@width - value.length - 1) + DELIM end column += 1 } println(output) if output.length > 0 puts separator(term_width) } end |
#print_staggered(maxwidth, term_width) ⇒ Object
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/print.rb', line 115 def print_staggered(maxwidth, term_width) minw = term_width maxwidth.each_with_index { |w, index| next if index == 0 width = (term_width - w) / index minw = [width, minw].min } @width = minw puts separator(term_width) @lines.each_with_index { |line, lineno| output = "" cols_left = Array.new(@ncols, true) while column = next_unprinted_column(cols_left) output = indent(column) if output.length == 0 while column value = line[column].to_s cols_left[column] = false # indicate this column has been printed output = output + value output = fill_to_next_column(output) next_column = calc_column(output) column = next_unprinted_column(cols_left, next_column) if column && (column > next_column) output = output + (spaces(@width-1) + DELIM) * (column - next_column) end end println(output) output = "" end puts separator(term_width) } end |
#print_table ⇒ Object
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/print.rb', line 170 def print_table return if @lines.nil? return if @ncols == 0 if @ncols == 1 @lines.each { |line| puts line } return end maxwidth = calc_max_widths # will lines fit in terminal width_needed = maxwidth.reduce(:+) + (@ncols-1) * 2 term_width, h = Util.detect_terminal_size if (width_needed <= term_width) print_table_normally(maxwidth) else print_staggered(maxwidth, term_width) end end |
#print_table_normally(maxwidth) ⇒ Object
102 103 104 105 106 107 108 109 |
# File 'lib/print.rb', line 102 def print_table_normally(maxwidth) @lines.each_with_index { |line, lineno| print_line_normally(maxwidth, line) if lineno == 0 print_line_normally(maxwidth, generate_separator_line(maxwidth)) end } end |
#println(line) ⇒ Object
prints a line with DELIMs following in proper columns
56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/print.rb', line 56 def println(line) column = calc_column(line) if (column < @ncols) line = fill_to_next_column(line) column = calc_column(line) end if (column < @ncols) segment = spaces(@width-1) + DELIM line = line + segment * (@ncols - column) end puts strip(line) end |
#separator(width) ⇒ Object
111 112 113 |
# File 'lib/print.rb', line 111 def separator(width) char_repeat(width, @sep_char) end |
#set_titles(sep_char, titles) ⇒ Object
20 21 22 23 24 25 26 |
# File 'lib/print.rb', line 20 def set_titles(sep_char, titles) @ncols = titles.length @lines = [ ] @width = nil @sep_char = sep_char @lines << titles end |
#spaces(n) ⇒ Object
16 17 18 |
# File 'lib/print.rb', line 16 def spaces(n) char_repeat(n, ' ') end |
#strip(line) ⇒ Object
41 42 43 44 |
# File 'lib/print.rb', line 41 def strip(line) regexp = Regexp.new(' *' + Regexp.escape(DELIM) + '*\Z') line.sub(regexp, "") end |