### Home Classes Methods

### In Files

- lib/terminal-table/version.rb
- lib/terminal-table/table.rb
- lib/terminal-table/separator.rb
- lib/terminal-table/style.rb
- lib/terminal-table/table_helper.rb
- lib/terminal-table/cell.rb
- lib/terminal-table/row.rb

### Parent

Object

### Namespace

- MODULE Terminal::Table::TableHelper
- CLASS Terminal::Table::Cell
- CLASS Terminal::Table::Row
- CLASS Terminal::Table::Separator
- CLASS Terminal::Table::Style

### Methods

- ::new
- #<<
- #==
- #add_row
- #add_separator
- #align_column
- #cell_padding
- #cell_spacing
- #column
- #column_width
- #column_with_headings
- #columns
- #headings=
- #length_of_column
- #number_of_columns
- #render
- #rows
- #rows=
- #style
- #style=
- #title=
- #to_s

### Files

- README.rdoc

### Class/Module Index

Quicksearch

- Terminal
- Terminal::Table
- Terminal::Table::Cell
- Terminal::Table::Row
- Terminal::Table::Separator
- Terminal::Table::Style
- Terminal::Table::TableHelper
- Object
- String

No matching classes.

# Terminal::Table

### Constants

VERSION

### Attributes

headings[R]
title[R]

### Public Class Methods

new(options = {}) click to toggle source

Generates a ASCII table with the given options.

    # File lib/terminal-table/table.rb, line 11
    def initialize options = {}, &block
      @column_widths = []
      self.style = options.fetch :style, {}
      self.headings = options.fetch :headings, []
      self.rows = options.fetch :rows, []
      self.title = options.fetch :title, nil
      yield_or_eval(&block) if block
    end

### Public Instance Methods

<<(array) click to toggle source
Alias for: add_row
==(other) click to toggle source

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

    # File lib/terminal-table/table.rb, line 153
    def == other
      if other.respond_to? :render and other.respond_to? :rows
        self.headings == other.headings and self.rows == other.rows
      end
    end

add_row(array) click to toggle source

Add a row.

    # File lib/terminal-table/table.rb, line 34
    def add_row array
      row = array == :separator ? Separator.new(self) : Row.new(self, array)
      @rows << row
      recalc_column_widths row
    end

Also aliased as: <<
add_separator() click to toggle source

Add a separator.

    # File lib/terminal-table/table.rb, line 44
    def add_separator
      self << :separator
    end

align_column(n, alignment) click to toggle source

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

    # File lib/terminal-table/table.rb, line 23
    def align_column n, alignment
      r = rows
      column(n).each_with_index do |col, i|
        cell = r[i][n]
        cell.alignment = alignment unless cell.alignment?
      end
    end

cell_padding() click to toggle source

    # File lib/terminal-table/table.rb, line 52
    def cell_padding
      style.padding_left + style.padding_right
    end

cell_spacing() click to toggle source

    # File lib/terminal-table/table.rb, line 48
    def cell_spacing
      cell_padding + style.border_y.length
    end

column(n, method = :value, array = rows) click to toggle source

Return column n.

    # File lib/terminal-table/table.rb, line 59
    def column n, method = :value, array = rows
      array.map { |row|
        cell = row[n]
        cell && method ? cell.__send__(method) : cell
      }.compact
    end

column_width(n) click to toggle source

Return length of column n.

    # File lib/terminal-table/table.rb, line 83
    def column_width n
      width = @column_widths[n] || 0
      width + additional_column_widths[n].to_i
    end

Also aliased as: length_of_column
column_with_headings(n, method = :value) click to toggle source

Return n column including headings.

    # File lib/terminal-table/table.rb, line 69
    def column_with_headings n, method = :value
      column n, method, headings_with_rows
    end

columns() click to toggle source

Return columns.

    # File lib/terminal-table/table.rb, line 76
    def columns
      (0...number_of_columns).map { |n| column n }
    end

headings=(array) click to toggle source

Set the headings

    # File lib/terminal-table/table.rb, line 99
    def headings= array
      @headings = Row.new(self, array)
      recalc_column_widths @headings
    end

length_of_column(n) click to toggle source
Alias for: column_width
number_of_columns() click to toggle source

Return total number of columns available.

    # File lib/terminal-table/table.rb, line 92
    def number_of_columns
      headings_with_rows.map { |r| r.cells.size }.max
    end

render() click to toggle source

Render the table.

    # File lib/terminal-table/table.rb, line 107
    def render
      separator = Separator.new(self)
      buffer = [separator]
      unless @title.nil?
        buffer << Row.new(self, [title_cell_options])
        buffer << separator
      end
      unless @headings.cells.empty?
        buffer << @headings
        buffer << separator
      end
      buffer += @rows
      buffer << separator
      buffer.map { |r| r.render }.join("\n")
    end

Also aliased as: to_s
rows() click to toggle source

Return rows without separator rows.

    # File lib/terminal-table/table.rb, line 127
    def rows
      @rows.reject { |row| row.is_a? Separator }
    end

rows=(array) click to toggle source

    # File lib/terminal-table/table.rb, line 131
    def rows= array
      @rows = []
      array.each { |arr| self << arr }
    end

style() click to toggle source

    # File lib/terminal-table/table.rb, line 140
    def style
      @style ||= Style.new
    end

style=(options) click to toggle source

    # File lib/terminal-table/table.rb, line 136
    def style=(options)
      style.apply options
    end

title=(title) click to toggle source

    # File lib/terminal-table/table.rb, line 144
    def title=(title)
      @title = title
      recalc_column_widths Row.new(self, [title_cell_options])
    end

to_s() click to toggle source
Alias for: render

[Validate]

Generated with the Darkfish Rdoc Generator 2.