Class: TablePrint

Inherits:
Object
  • Object
show all
Defined in:
lib/table_print.rb

Constant Summary collapse

DELIM =
'|'

Instance Method Summary collapse

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



30
31
32
# File 'lib/table_print.rb', line 30

def append(*columns)
  @lines << columns
end

#calc_column(line) ⇒ Object



48
49
50
# File 'lib/table_print.rb', line 48

def calc_column(line)
  line.length / @width
end

#calc_max_widths ⇒ Object



78
79
80
81
82
83
84
85
86
# File 'lib/table_print.rb', line 78

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



6
7
8
9
10
11
12
# File 'lib/table_print.rb', line 6

def char_repeat(n, char)
  result = ""
  (1..n).each { |i|
    result = result + char
  }
  result
end

#fill_to_next_column(line) ⇒ Object



52
53
54
55
# File 'lib/table_print.rb', line 52

def fill_to_next_column(line)
  tab = @width - (line.length % @width)
  line = line + spaces(tab-1) + DELIM
end

#indent(column) ⇒ Object



34
35
36
37
38
39
40
41
# File 'lib/table_print.rb', line 34

def indent(column)
  if column == 0
    ""
  else
    segment = spaces(@width-1) + DELIM
    segment * column
  end
end

#next_unprinted_column(col_array, start = 0) ⇒ Object



71
72
73
74
75
76
# File 'lib/table_print.rb', line 71

def next_unprinted_column(col_array, start=0)
  for i in (start...col_array.length)
    return i if col_array[i]
  end
  nil
end


135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/table_print.rb', line 135

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


102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/table_print.rb', line 102

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|
    next if lineno == 1   # skip line of separators
    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


158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/table_print.rb', line 158

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


88
89
90
91
92
93
94
95
96
# File 'lib/table_print.rb', line 88

def print_table_normally(maxwidth)
  @lines.each { |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

#println(line) ⇒ Object

prints a line with DELIMs following in proper columns



58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/table_print.rb', line 58

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



98
99
100
# File 'lib/table_print.rb', line 98

def separator(width)
  char_repeat(width, '-')
end

#spaces(n) ⇒ Object



14
15
16
# File 'lib/table_print.rb', line 14

def spaces(n)
  char_repeat(n, ' ')
end

#strip(line) ⇒ Object



43
44
45
46
# File 'lib/table_print.rb', line 43

def strip(line)
  regexp = Regexp.new(' *' + Regexp.escape(DELIM) + '*\Z')
  line.sub(regexp, "")
end