class Tefil::ColumnFormer

Public Class Methods

new(options = {}) click to toggle source
Calls superclass method Tefil::TextFilterBase::new
# File lib/tefil/columnformer.rb, line 29
def initialize(options = {})
  @just = options[:just] || :left
  @separator = options[:separator] || ' '
  @transpose = options[:transpose]
  super(options)
end

Public Instance Methods

form(matrix, io = $stdout, indent = 0) click to toggle source

def form(matrix, io = $stdout, separator = “ ”, left = false)

# File lib/tefil/columnformer.rb, line 37
def form(matrix, io = $stdout, indent = 0)
  #Obtain max length for each column.

  matrix = matrix.transpose if @transpose

  max_lengths = []
  matrix.each do |row|
    row.each_with_index do |item, index|
      item = item.to_s
      max_lengths[index] ||= 0
      size = print_size(item)
      max_lengths[index] = size if max_lengths[index] < size
    end
  end

  #Output
  matrix.each do |row|
    new_items = []
    row.each_with_index do |item, index|
      item = item.to_s
      method = (@just.to_s + "_just").to_sym
      new_items[index] = item.send(method, max_lengths[index])
    end
    io.print(" " * indent)
    io.puts new_items.join(@separator).sub(/ +$/, "")
  end
end

Private Instance Methods

print_size(string) click to toggle source
process_stream(in_io, out_io) click to toggle source
# File lib/tefil/columnformer.rb, line 68
def process_stream(in_io, out_io)
  space_width = []
  rows = in_io.readlines.map do |line|
    #pp line
    line =~ /^(\s*)/
    space_width << $1.length
    line.strip.split(INPUT_SEPARATOR)
  end
  #form(rows, out_io, OPTIONS[:separator], OPTIONS[:left])
  form(rows, out_io, space_width.min)
end