#!/usr/bin/env python3

import os,sys
from argparse import ArgumentParser
from pprint import pprint

ap=ArgumentParser(usage='''
Read single-column text from standard input. Output multi-column text to
standard output. The default number of columns (2) and lines (50) can be
overridden, and well as the direction (default is --down).

usage: %(prog)s [options]
''')
ap.add_argument('-a','--across',dest='dir',action='store_const',const='across',default='down',
  help='Input data will be output across columns and then down the page.')
ap.add_argument('-d','--down',dest='dir',action='store_const',const='down',default='down',
  help='Input data will be output down the page and then across columns.')
ap.add_argument('-f','--float',dest='bFloat',default=False,action='store_true',
  help='Allow columns to float, as opposed to all being the same width.')
ap.add_argument('-c','--columns',dest='cols',type=int,default=2,
  help='Number of columns per page. (default: %(default)r)')
ap.add_argument('-l','--lines',dest='lines',type=int,default=50,
  help='Number of lines per page. (default: %(default)r)')
ap.add_argument('-s','--seperation',dest='sep',type=int,default=2,
  help='Number of space characters between columns. (default: %(default)r)')
ap.add_argument('-w','--width',dest='width',type=int,default=79,
  help='Number of characters per line on the page. (default: %(default)r)')
opt=ap.parse_args()

class Page(object):
  def __init__(self,rows=50,cols=2,sep=2,dir='down',bFloat=False):
    self.rows=rows
    self.cols=cols
    self.sep=sep
    self.dir=dir
    self.bFloat=bFloat
    self.clear()

  def clear(self):
    self.page=[]
    for c in range(self.cols):
      self.page.append([])
      for l in range(self.rows):
        self.page[c].append('')
    self.row=self.col=0

  def write(self):
    width=[max([len(r) for r in c]) for c in self.page]
    #print 'widths: %r'%width
    if max(width)>0:
      #pprint(self.page)
      if self.bFloat:
        fmt=(' '*self.sep).join(['%%-%ds'%w for w in width])
      else:
        fmt=(' '*self.sep).join(['%%-%ds'%max(width) for w in width])
      for l in range(len(self.page[0])):
        #print '%s %r'%(fmt,tuple([self.page[c][l] for c in range(len(self.page))]))
        print(fmt%tuple([self.page[c][l] for c in range(len(self.page))]))
      self.clear()

  def add_item(self,s):
    self.page[self.col][self.row]=s
    #print 'page[%r][%r]=%r'%(self.col,self.row,s)
    if self.dir=='down':
      self.row+=1
      if self.row>=self.rows:
        self.row,self.col=0,self.col+1
      if self.col>=self.cols:
        self.write()
    else:
      self.col+=1
      if self.col>=self.cols:
        self.row,self.col=self.row+1,0
      if self.row>=self.rows:
        self.write()

p=Page(rows=opt.lines,cols=opt.cols,sep=opt.sep,dir=opt.dir,bFloat=opt.bFloat)
for s in sys.stdin:
  if s[-1]=='\n':
    s=s[:-1]
  p.add_item(s)
p.write()
