Class: TablePrint

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

Constant Summary collapse

DELIM =
'|'

Instance Method Summary collapse

Constructor Details

#initialize(*titles) ⇒ TablePrint

Returns a new instance of TablePrint.



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/print.rb', line 19

def initialize(sep_char, *titles)
  @ncols = titles.length
  @lines = [ ]
  @width = nil
  @sep_char = sep_char
  @lines << titles
  separator = [ ]
  titles.each { |title|
    separator << char_repeat(title.length, @sep_char)
  }
  @lines << separator
end

Instance Method Details

#append(*columns) ⇒ Object



32
33
34
# File 'lib/print.rb', line 32

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

#calc_column(line) ⇒ Object



50
51
52
# File 'lib/print.rb', line 50

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

#calc_max_widths ⇒ Object



80
81
82
83
84
85
86
87
88
# File 'lib/print.rb', line 80

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



7
8
9
10
11
12
13
# File 'lib/print.rb', line 7

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

#fill_to_next_column(line) ⇒ Object



54
55
56
57
# File 'lib/print.rb', line 54

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

#indent(column) ⇒ Object



36
37
38
39
40
41
42
43
# File 'lib/print.rb', line 36

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

#next_unprinted_column(col_array, start = 0) ⇒ Object



73
74
75
76
77
78
# File 'lib/print.rb', line 73

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


143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/print.rb', line 143

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


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
134
135
136
137
138
139
140
141
# File 'lib/print.rb', line 107

def print_staggered(maxwidth, term_width, header)
  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
    if lineno == 0 && ! header
      next
    end
    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

TODO: suppress headers



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/print.rb', line 167

def print_table(header = true)
  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, header)
  else
    print_staggered(maxwidth, term_width, header)
  end
end


90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/print.rb', line 90

def print_table_normally(maxwidth, header)
  @lines.each_with_index { |line, lineno|
    if lineno < 2 && ! header
      next
    end
    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



60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/print.rb', line 60

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



103
104
105
# File 'lib/print.rb', line 103

def separator(width)
  char_repeat(width, @sep_char)
end

#spaces(n) ⇒ Object



15
16
17
# File 'lib/print.rb', line 15

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

#strip(line) ⇒ Object



45
46
47
48
# File 'lib/print.rb', line 45

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