Class: ANSI::Table

### Home Classes Methods

### In Files

- lib/ansi/table.rb

### Parent

Object

### Methods

- ::new
- #to_s

### Files

- COPYING.rdoc
- DEMO.rdoc
- HISTORY.rdoc
- README.rdoc

### Class/Module Index

Quicksearch

- ANSI
- ANSI::BBCode
- ANSI::Chain
- ANSI::Code
- ANSI::Columns
- ANSI::Constants
- ANSI::Diff
- ANSI::HexDump
- ANSI::Logger
- ANSI::Mixin
- ANSI::ProgressBar
- ANSI::String
- ANSI::Table
- ANSI::Terminal
- Object
- String

No matching classes.

# ANSI::Table

### Attributes

align[RW]
border[RW]
fit[RW]

Fit to scree width.

format[RW]
padding[RW]
table[RW]

### Public Class Methods

new(table, options={}, &format) click to toggle source

The Table class can be used to output nicely formatted tables with division lines and alignment.

table - array of array

options - align :left or :right options - space to add to each cell options - fit to screen width options -

The `format` block must return ANSI codes to apply to each cell.

Other Implementations:

- github.com/visionmedia/terminal-table

- github.com/aptinio/text-table

TODO: Support for table headers and footers.

    # File lib/ansi/table.rb, line 27
    def initialize(table, options={}, &format)
      @table   = table
      @padding = options[:padding] || 0
      @align   = options[:align]
      @fit     = options[:fit]
      @border  = options[:border]
      #@ansi    = [options[:ansi]].flatten
      @format  = format

      @pad = " " * @padding
    end

### Public Instance Methods

to_s() click to toggle source

    # File lib/ansi/table.rb, line 58
    def to_s #(fit=false)
      #row_count = table.size
      #col_count = table[0].size

      max = max_columns(fit)

      div = dividing_line
      top = div #.gsub('+', ".")
      bot = div #.gsub('+', "'")

      body = []
      table.each_with_index do |row, r|
         body_row = []
         row.each_with_index do |cell, c|
           t = cell_template(max[c])
           s = t % cell.to_s
           body_row << apply_format(s, cell, c, r)
         end
         body << "| " + body_row.join(' | ') + " |"
      end

      if border
        body = body.join("\n#{div}\n")
      else
        body = body.join("\n")
      end

      "#{top}\n#{body}\n#{bot}\n"
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.