Class: Terminal::Table

Inherits:
Object show all
Defined in:
lib/terminal-table/table.rb,
lib/terminal-table/cell.rb,
lib/terminal-table/heading.rb,
lib/terminal-table/version.rb,
lib/terminal-table/table_helper.rb

Overview

Generates an ASCII table for terminal usage.

This class can be used in many ways, however ultimately you should require 'terminal-table/import' (which requires terminal-table itself) to use the Kernel helper method which is easier, and cleaner to use. View Object#table for examples.

Examples:

table = Terminal::Table.new
table.headings = ['Characters', { :value => 'Nums', :align => :right }]
table << [{ :value => 'a', :align => :center }, 1]
table << ['b', 222222222222222]
table << ['c', 3]
puts table.render     # or simply puts table

+------------+-----------------+
| Characters |            Nums |
+------------+-----------------+
|     a      | 1               |
| b          | 222222222222222 |
| c          | 3               |
+------------+-----------------+

Defined Under Namespace

Modules: TableHelper Classes: Cell, Error, Heading

Constant Summary collapse

VERSION =
'1.2.0'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}, &block) ⇒ Table

Generates a ASCII table.



48
49
50
51
52
# File 'lib/terminal-table/table.rb', line 48

def initialize options = {}, &block
  @headings = options.fetch :headings, []
  @rows = options.fetch :rows, []
  yield_or_eval &block if block_given?
end

Instance Attribute Details

#headingsObject

Returns the value of attribute headings.



43
44
45
# File 'lib/terminal-table/table.rb', line 43

def headings
  @headings
end

#rowsObject

Returns the value of attribute rows.



43
44
45
# File 'lib/terminal-table/table.rb', line 43

def rows
  @rows
end

Instance Method Details

#==(other) ⇒ Object

Check if other is equal to self. other is considered equal if it contains the same headings and rows.



185
186
187
188
189
# File 'lib/terminal-table/table.rb', line 185

def == other
  if other.respond_to?(:headings) && other.respond_to?(:rows)
    headings == other.headings && rows == other.rows
  end
end

#add_row(row) ⇒ Object Also known as: <<

Add a row.



105
106
107
# File 'lib/terminal-table/table.rb', line 105

def add_row row
  rows << row
end

#align_column(n, alignment) ⇒ Object

Align column n to alignment of :center, :left, or :right.



168
169
170
171
172
# File 'lib/terminal-table/table.rb', line 168

def align_column n, alignment
  column(n).each_with_index do |col, i|
    @rows[i][n] = {:value => col, :alignment => alignment} unless col.is_a? Hash
  end
end

#column(n) ⇒ Object

Return n column.



120
121
122
# File 'lib/terminal-table/table.rb', line 120

def column n
  rows.map { |row| row[n] }.compact 
end

#column_with_headings(n) ⇒ Object

Return n column including headings.



127
128
129
# File 'lib/terminal-table/table.rb', line 127

def column_with_headings n
  headings_with_rows.map { |row| row[n] }.compact  
end

#columnsObject

Return columns.



134
135
136
# File 'lib/terminal-table/table.rb', line 134

def columns 
  (0..number_of_columns-1).map { |n| column n } 
end

#has_headings?Boolean

Weither or not any headings are present, since they are optional.

Returns:

  • (Boolean)


113
114
115
# File 'lib/terminal-table/table.rb', line 113

def has_headings?
  !headings.empty?
end

#headings_with_rowsObject

Return headings combined with rows.



177
178
179
# File 'lib/terminal-table/table.rb', line 177

def headings_with_rows
  [headings] + rows
end

#largest_cell_in_column(n) ⇒ Object

Return the largest cell found within column n.



141
142
143
# File 'lib/terminal-table/table.rb', line 141

def largest_cell_in_column n
  column_with_headings(n).sort_by { |cell| Cell.new(0, cell).length }.last
end

#length_of_column(n) ⇒ Object

Return length of column n.



148
149
150
151
152
153
154
155
# File 'lib/terminal-table/table.rb', line 148

def length_of_column n
  largest_cell = largest_cell_in_column(n)
  if largest_cell.is_a? Hash
    largest_cell[:value].length# - 2
  else
    largest_cell.to_s.length
  end
end

#number_of_columnsObject

Return total number of columns available.

Raises:



160
161
162
163
# File 'lib/terminal-table/table.rb', line 160

def number_of_columns
  return rows.first.length unless rows.empty?
  raise Error, 'your table needs some rows.'
end

#renderObject Also known as: to_s

Render the table. Often you will simply call puts with an instance in order to output it to the terminal.



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/terminal-table/table.rb', line 58

def render
  buffer = seperator << "\n" 
  if has_headings?
    buffer << Y + headings.map_with_index do |heading, i|
      width = 0
      if heading.is_a?(Hash) and !heading[:colspan].nil?
        i.upto(i + heading[:colspan] - 1) do |col|
          width += length_of_column(col)
        end
        width += (heading[:colspan] - 1) * (Y.length + 2)
      else
        width = length_of_column(i)
      end
      Heading.new( width, heading).render
    end.join(Y) + Y
    buffer << "\n#{seperator}\n"
  end
  buffer << rows.map do |row| 
    Y + row.map_with_index do |cell, i|
      width = 0
      if cell.is_a?(Hash) and !cell[:colspan].nil?
        i.upto(i + cell[:colspan] - 1) do |col|
          width += length_of_column(col)
        end
        width += (cell[:colspan] - 1) * (Y.length + 2)
      else
        width = length_of_column(i)
      end
      Cell.new(width, cell).render
    end.join(Y) + Y 
  end.join("\n")
  buffer << "\n#{seperator}\n"
end

#seperatorObject

Create a seperator based on colum lengths.



96
97
98
99
100
# File 'lib/terminal-table/table.rb', line 96

def seperator
  I + columns.collect_with_index do |col, i| 
    X * (length_of_column(i) + 2) 
  end.join(I) + I 
end