module Text.PrettyPrint.NCol (asColumns, asColumnsWithBuff) 
        where

import Text.PrettyPrint
import Data.List


asColumns :: [[Doc]] -> Doc
asColumns = flip asColumnsWithBuff $ 1

asColumnsWithBuff :: [[Doc]] -> Int -> Doc
asColumnsWithBuff lls q = normalize
        where normalize = vcat $ map hsep 
                        $ map (\x -> pad (length x) longColumnLen empty x) 
                        $ pad' longEntryLen q
                        $ transpose lls -- normalize column height
              longColumnLen = maximum (map length lls)
              longEntryLen = maximum $ map docLen (concat lls)

docLen d = length $ render d 

pad :: Int -> Int -> a -> [a] -> [a]
pad lx max b ls = ls ++ replicate (max - lx) b

pad' _ _ []       = []
pad' mx q (ls:xs) = map buf ls : pad' mx q xs 
        where buf x = x <> (hcat $ replicate q space) <> (hcat $ replicate (mx - (docLen x)) space)

test = map (map text) $ [["One", "Two","Three"],["1","2","3"],["a","b"]]