0 | module Text.PrettyPrint.Prettyprinter.Render.Terminal
4 | import public Control.ANSI
5 | import Control.Monad.ST
6 | import Text.PrettyPrint.Prettyprinter.Doc
12 | AnsiStyle = List SGR
15 | color : Color -> AnsiStyle
16 | color c = pure $
SetForeground c
19 | bgColor : Color -> AnsiStyle
20 | bgColor c = pure $
SetBackground c
24 | bold = pure $
SetStyle Bold
28 | italic = pure $
SetStyle Italic
31 | underline : AnsiStyle
32 | underline = pure $
SetStyle SingleUnderline
36 | strike = pure $
SetStyle Striked
39 | renderString : SimpleDocStream AnsiStyle -> String
40 | renderString sdoc = fromMaybe "<internal pretty printing error>" $
runST $
do
41 | styleStackRef <- newSTRef {a = List AnsiStyle} [neutral]
42 | outputRef <- newSTRef {a = String} neutral
43 | go styleStackRef outputRef sdoc
44 | readSTRef styleStackRef >>= \case
46 | [_] => Just <$> readSTRef outputRef
49 | push : STRef s (List AnsiStyle) -> List SGR -> ST s ()
50 | push stack x = modifySTRef stack (x ::)
52 | peek : STRef s (List AnsiStyle) -> ST s (Maybe AnsiStyle)
54 | (x :: _) <- readSTRef stack | [] => pure Nothing
57 | pop : STRef s (List AnsiStyle) -> ST s (Maybe AnsiStyle)
59 | (x :: xs) <- readSTRef stack | [] => pure Nothing
63 | writeOutput : STRef s String -> String -> ST s ()
64 | writeOutput out x = modifySTRef out (<+> x)
66 | go : STRef s (List AnsiStyle) -> STRef s String -> SimpleDocStream AnsiStyle -> ST s ()
67 | go stack out SEmpty = pure ()
68 | go stack out (SChar c rest) = do
69 | writeOutput out (singleton c)
71 | go stack out (SText l t rest) = do
74 | go stack out (SLine l rest) = do
75 | writeOutput out (singleton '\n' <+> textSpaces l)
77 | go stack out (SAnnPush style rest) = do
78 | Just currentStyle <- peek stack
79 | | Nothing => writeSTRef stack []
80 | let newStyle = style <+> currentStyle
82 | writeOutput out (escapeSGR newStyle)
84 | go stack out (SAnnPop rest) = do
86 | Just newStyle <- peek stack
87 | | Nothing => writeSTRef stack []
88 | writeOutput out (escapeSGR (Reset :: newStyle))
92 | renderIO : SimpleDocStream AnsiStyle -> IO ()
93 | renderIO = putStrLn . renderString
96 | putDoc : Doc AnsiStyle -> IO ()
97 | putDoc = renderIO . layoutPretty defaultLayoutOptions