module Sifflet.Text.Pretty (Pretty(..) , indentLine, sepLines, sepLines2 , sepComma, sepCommaSp, sepSpace) where import Data.List (intercalate) -- | The class of types that can be pretty-printed. -- (Unfortunately this is not very useful, because -- and Expr can be pretty Haskell or pretty Python or pretty Scheme, -- leading to overlapping instance declarations.) -- -- pretty x is a pretty String representation of x. -- prettyList prefix infix postfix xs is a pretty String representation -- of the list xs, with prefix, infix, and postfix specifying the -- punctuation. For example, if (pretty x) => "x", -- then prettyList "[" ", " "]" [x, x, x] => "[x, x, x]". -- -- Minimal complete implementation: define pretty. class Pretty a where pretty :: a -> String prettyList :: String -> String -> String -> [a] -> String prettyList pre tween post xs = pre ++ intercalate tween (map pretty xs) ++ post -- | Indent a single line n spaces. indentLine :: Int -> String -> String indentLine n line = replicate n ' ' ++ line -- | sepLines is like unlines, but omits the \n at the end of the -- last line. sepLines :: [String] -> String sepLines = intercalate "\n" -- | sepLines2 is like sepLines, but adds an extra \n between each -- pair of lines so they are "double spaced." sepLines2 :: [String] -> String sepLines2 = intercalate "\n\n" -- | Separate strings by commas and nothing else (",") sepComma :: [String] -> String sepComma = intercalate "," -- | Separate strings by commas and spaces (", ") sepCommaSp :: [String] -> String sepCommaSp = intercalate ", " -- | Separate strings by just spaces (" ") sepSpace :: [String] -> String sepSpace = unwords