35 lines
1.3 KiB
Plaintext
35 lines
1.3 KiB
Plaintext
#! /usr/bin/mira -exec
|
|
main :: [sys_message]
|
|
main = [Stdout (align (alignment algm) '$' (read file))]
|
|
where [cmd, file, algm] = $*
|
|
|
|
alignment :: [char]->num->[char]->[char]
|
|
alignment "left" = ljustify
|
|
alignment "center" = cjustify
|
|
alignment "right" = rjustify
|
|
alignment x = error "Alignment must be left, center, or right"
|
|
|
|
align :: (num->[char]->[char])->char->[char]->[char]
|
|
align just sep text = (lay . map (alignline just sep cols) . lines) text
|
|
where cols = colwidths sep text
|
|
|
|
split :: *->[*]->[[*]]
|
|
split sep = s []
|
|
where s acc [] = [acc]
|
|
s acc (a:as) = acc:s [] as, if a==sep
|
|
= s (acc++[a]) as, otherwise
|
|
|
|
colwidths :: char->[char]->[num]
|
|
colwidths sep text = (map max . transpose . map (extend maxwidth 0)) widths
|
|
where widths = map (map (#) . split sep) (lines text)
|
|
maxwidth = max (map (#) widths)
|
|
|
|
alignline :: (num->[char]->[char])->char->[num]->[char]->[char]
|
|
alignline just sep cols = concat . map (++" ") . zipwith just cols . split sep
|
|
|
|
zipwith :: (*->**->***)->[*]->[**]->[***]
|
|
zipwith f xs ys = map f' (zip2 xs ys) where f' (x,y) = f x y
|
|
|
|
extend :: num->*->[*]->[*]
|
|
extend n k ls = ls ++ take (n-#ls) (repeat k)
|