Sha256: b17893a57ce419e681ae0b4af2e25dce26d727be1d741ac44158d76ccfa58288

Contents?: true

Size: 1.14 KB

Versions: 2

Compression:

Stored size: 1.14 KB

Contents

#! /usr/bin/env ruby
# coding: utf-8
#
# USAGE: formcolumn [options] files ...

INPUT_SEPARATOR = /\s+/


require "pp"
require "optparse"
require "rubygems"
require "tefil"

# option analysis
OPTIONS = {:separator => ' '}
op = OptionParser.new
op.on("-o"    , "--overwrite"    , "Overwrite."){    OPTIONS[:overwrite] = true}
op.on("-s char"    , "--separator=char"    , "Indicate separator."){|val| OPTIONS[:separator] = val}
op.parse!(ARGV)

module TextFilter
  def self.process_stream(in_io, out_io)
    rows = in_io.readlines.map { |line| line.strip.split(INPUT_SEPARATOR) }

    #Obtain max length for each column.
    max_lengths = []
    rows.each do |row|
      row.each_with_index do |item, index|
        max_lengths[index] ||= 0
        size = item.size
        max_lengths[index] = size if max_lengths[index] < size
      end
    end

    #Output
    rows.each do |row|
      new_items = []
      row.each_with_index do |item, index|
        new_items[index] = sprintf("%#{max_lengths[index]}s", item )
      end
      out_io.puts new_items.join(OPTIONS[:separator])
    end
  end
end

OPTIONS[:overwrite] ||= false

TextFilter.run(ARGV, OPTIONS[:overwrite])

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
tefil-0.0.2 bin/formcolumn
tefil-0.0.1 bin/formcolumn