Skip to content

Instantly share code, notes, and snippets.

@ox
Last active November 7, 2016 20:59
Show Gist options
  • Save ox/df9828aafc644d804d290fb0cb72265e to your computer and use it in GitHub Desktop.
Save ox/df9828aafc644d804d290fb0cb72265e to your computer and use it in GitHub Desktop.
Formats input separated by tabs into columns
module Main where
import Data.List (transpose, words, lines, unwords, unlines)
import Data.Text (justifyLeft, unpack, pack)
prettyText :: [[String]] -> [[String]]
prettyText inputRows =
let colWidths = map (maximum . map length) (transpose inputRows)
indentFn = map (\x -> unpack . justifyLeft (x+2) ' ' . pack) colWidths -- right-pad each elem by col width + 2
in map (zipWith ($) indentFn) inputRows -- apply each column's padding to each element
main = do
input <- getContents
putStr $ unlines $ map unwords $ prettyText $ map words $ lines input
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment