module ExRender.Base
( module ExRender.Base
, module Text.PrettyPrint
) where
import Text.PrettyPrint
exWrapWidth ∷ Int
exWrapWidth = 80
class ExRender a where
exDisp ∷ a → Doc
class ExRenderQ a where
exDispQ ∷ a → Doc
dquoted ∷ String → String
dquoted [] = []
dquoted ('\\':xs) = "\\\\" ++ dquoted xs
dquoted ('"':xs) = "\\\"" ++ dquoted xs
dquoted ('`':xs) = "\\`" ++ dquoted xs
dquoted ('$':xs) = "\\$" ++ dquoted xs
dquoted (x:xs) = x : dquoted xs
softWidth ∷ Int → [String] → [[String]]
softWidth width = build 0 [] where
build _ ys [] = [reverse ys]
build 0 [] (w:ws) = build (length w) [w] ws
build n ys (w:ws) | n' > width = reverse ys : build 0 [] (w:ws)
| otherwise = build n' (w : ys) ws
where
n' = length w + n
reflow ∷ Int → String → Doc
reflow width = vcat . map (text . unwords) . softWidth width . words
spaces ∷ Doc → Doc
spaces doc | isEmpty doc = empty
| otherwise = space <> doc <> space
nbrackets ∷ Doc → Doc
nbrackets doc | isEmpty doc = empty
| otherwise = brackets doc
exFieldDoc ∷ String → Doc → Doc
exFieldDoc name value | isEmpty value = empty
| otherwise = vcat [text name <> text "=\"", value, char '"']
exField ∷ String → String → Doc
exField _ "" = empty
exField name x | length singleLine < exWrapWidth = text singleLine
| otherwise = exFieldDoc name (reflow exWrapWidth (dquoted x))
where
singleLine = name ++ "=\"" ++ dquoted x ++ "\""
instance ExRenderQ String where
exDispQ = text . dquoted