-- To be added to Text.PrettyPrint
module Table (
              alignRight, -- :: [Doc] -> [Doc]
              alignLeft,  -- :: [Doc] -> [Doc]
              table       -- :: [[Doc]] -> Doc
             )
    where

import Text.PrettyPrint
import Data.List

alignRight :: [Doc] -> [Doc]
alignRight = align flip

alignLeft :: [Doc] -> [Doc]
alignLeft = align id

align :: forall c. ((Doc -> Doc -> Doc) -> Doc -> Doc -> c) -> [Doc] -> [c]
align f xs = 
    zipWith (\d dlen -> pad (colwidth-dlen) d) xs xlens
        where xlens = map (length . render) xs
              colwidth = maximum xlens
              pad n d = (f (<>)) d (text (replicate n ' '))

table :: [[Doc]] -> Doc
table cols =
    vcat (map (foldr (<+>) empty)
              (transpose cols))


{-
-- I'm not sure what this is, perhaps ongoing work to support more general operations?
-- Now that I look at it, I sincerely doubt that I wrote it.
-- Jeremy, is this your thing for supporting variable terminals?

getWidth=undefined
renderWidth :: Doc -> IO String       
renderWidth doc =
    do columns <- return . fromMaybe 80 =<< getWidth
       return $ renderStyle (Style PageMode columns 1.0) doc

-}
