on 2aa7dcd help sign in
elk/src/Reporting/PrettyPrint.hs
46 lines 1.1 kB history raw
1{-# LANGUAGE FlexibleInstances #-}
2module Reporting.PrettyPrint where
4import Text.PrettyPrint as P
5import qualified AST.Helpers as Help
8class Pretty a where
9 pretty :: Bool -> a -> Doc
12instance Pretty String where
13 pretty _ str =
14 P.text str
17commaCat :: [Doc] -> Doc
18commaCat docs =
19 cat (punctuate comma docs)
22commaSep :: [Doc] -> Doc
23commaSep docs =
24 sep (punctuate comma docs)
27parensIf :: Bool -> Doc -> Doc
28parensIf bool doc =
29 if bool then parens doc else doc
32variable :: String -> Doc
33variable x =
34 if Help.isOp x then parens (text x) else text x
37eightyCharLines :: Int -> String -> String
38eightyCharLines indent message = answer
39 where
40 (answer,_,_) = foldl step (spaces, indent-1, "") chunks
42 chunks = map (\w -> (w, length w)) (words message)
43 spaces = replicate indent ' '
44 step (sentence, slen, space) (word, wlen)
45 | slen + wlen > 79 = (sentence ++ "\n" ++ spaces ++ word, indent + wlen, " ")
46 | otherwise = (sentence ++ space ++ word, slen + wlen + length space, " ")