Class: Term::Table

Inherits:
Object show all
Defined in:
lib/epitools/term.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, options = {}) ⇒ Table


111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/epitools/term.rb', line 111

def initialize(data, options={})
  @data         = data.map(&:to_s)
  @strip_color = options[:ansi] || options[:colorized] || options[:colored] || options[:strip_color] || options[:strip_ansi]

  if strip_color
    @max_size = @data.map { |e| e.strip_color.size }.max
  else
    @max_size = @data.map(&:size).max
  end

  @indent   = options[:indent]  || 0
  @border   = options[:border]
  @columns  = options[:columns]
  @padding  = options[:padding] || 1

  if (options.keys & [:horiz, :horizontal, :horizontally]).any?
    @direction = :horizontal
  else
    @direction = :vertical
  end

  # Update the terminal size
  @width, @height = Term.size
end

Instance Attribute Details

#borderObject

TODO:

  • make Table's configuration eaiser to remember by putting the formatting parameters in initialize eg: Table.new(elements, :sort=>:vertical).to_s


105
106
107
# File 'lib/epitools/term.rb', line 105

def border
  @border
end

#columnsObject

TODO:

  • make Table's configuration eaiser to remember by putting the formatting parameters in initialize eg: Table.new(elements, :sort=>:vertical).to_s


105
106
107
# File 'lib/epitools/term.rb', line 105

def columns
  @columns
end

#heightObject

TODO:

  • make Table's configuration eaiser to remember by putting the formatting parameters in initialize eg: Table.new(elements, :sort=>:vertical).to_s


105
106
107
# File 'lib/epitools/term.rb', line 105

def height
  @height
end

#indentObject

TODO:

  • make Table's configuration eaiser to remember by putting the formatting parameters in initialize eg: Table.new(elements, :sort=>:vertical).to_s


105
106
107
# File 'lib/epitools/term.rb', line 105

def indent
  @indent
end

#paddingObject

TODO:

  • make Table's configuration eaiser to remember by putting the formatting parameters in initialize eg: Table.new(elements, :sort=>:vertical).to_s


105
106
107
# File 'lib/epitools/term.rb', line 105

def padding
  @padding
end

#strip_colorObject

TODO:

  • make Table's configuration eaiser to remember by putting the formatting parameters in initialize eg: Table.new(elements, :sort=>:vertical).to_s


105
106
107
# File 'lib/epitools/term.rb', line 105

def strip_color
  @strip_color
end

#widthObject

TODO:

  • make Table's configuration eaiser to remember by putting the formatting parameters in initialize eg: Table.new(elements, :sort=>:vertical).to_s


105
106
107
# File 'lib/epitools/term.rb', line 105

def width
  @width
end

Class Method Details

.[](data) ⇒ Object


107
108
109
# File 'lib/epitools/term.rb', line 107

def self.[](data)
  self.new(data)
end

Instance Method Details

#column_orderObject


148
149
150
151
152
153
154
155
# File 'lib/epitools/term.rb', line 148

def column_order
  cols = []
  @data.each_slice(num_rows) { |col| cols << col }
  if (diff = cols.first.size - cols.last.size) > 0
    cols.last.concat [''] * diff
  end
  cols.transpose
end

#displayObject

(opts={})


188
189
190
191
192
193
194
195
# File 'lib/epitools/term.rb', line 188

def display #(opts={})
  case @direction
  when :horizontal
    puts in_rows
  when :vertical
    puts in_columns
  end
end

#in_columnsObject Also known as: by_columns, by_cols


175
176
177
178
# File 'lib/epitools/term.rb', line 175

def in_columns
  return '' if @data.empty?
  render sliced_into(num_rows).transpose
end

#in_rowsObject Also known as: by_rows


182
183
184
185
# File 'lib/epitools/term.rb', line 182

def in_rows
  return '' if @data.empty?
  render sliced_into(num_columns)
end

#num_columnsObject


136
137
138
139
140
141
142
# File 'lib/epitools/term.rb', line 136

def num_columns
  return @columns if @columns
  w = @width
  w -= indent
  cols = (w-2) / (@max_size + @padding)
  cols > 0 ? cols : 1
end

#num_rowsObject


144
145
146
# File 'lib/epitools/term.rb', line 144

def num_rows
  (@data.size / num_columns.to_f).ceil
end

#render(rows, options = {}) ⇒ Object


201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/epitools/term.rb', line 201

def render(rows, options={})
  num_cols  = rows.first.size
  result    = []

  if @border
    separator = "+#{(["-" * @max_size] * num_cols).join('+')}+"
    result << separator
  end

  for row in rows

    justified = row.map do |e|
      if (diff = @max_size - e.strip_color.size) > 0
        e = e + (" " * diff)
      end
      e
    end

    if @border
      line = "|#{justified.join('|')}|"
    else
      line = justified.join(' '*@padding)
    end

    result << (" "*indent) + line
  end

  result << separator if @border

  result.join("\n")
end

#row_orderObject


157
158
159
160
161
162
163
164
# File 'lib/epitools/term.rb', line 157

def row_order
  rows = []
  @data.each_slice(num_columns) { |row| rows << row }
  if (diff = rows.first.size - rows.last.size) > 0
    rows.last.concat [''] * diff
  end
  rows
end

#sliced_into(n) ⇒ Object


166
167
168
169
170
171
172
173
# File 'lib/epitools/term.rb', line 166

def sliced_into(n)
  elems = []
  @data.each_slice(n) { |e| elems << e }
  if (diff = elems.first.size - elems.last.size) > 0
    elems.last.concat [''] * diff
  end
  elems
end

#to_sObject


197
198
199
# File 'lib/epitools/term.rb', line 197

def to_s
  by_rows
end