0 | module Text.Show.Pretty
4 | import Text.PrettyPrint.Bernardy
6 | import public Text.Show.Value
7 | import public Text.Show.PrettyVal
8 | import public Text.Show.PrettyVal.Derive
16 | dropTrailingZeros : SnocList Char -> String
17 | dropTrailingZeros [<] = "0"
18 | dropTrailingZeros [<'.'] = "0"
19 | dropTrailingZeros (i :< '.') = fastPack (i <>> [])
20 | dropTrailingZeros (i :< '0') = dropTrailingZeros i
21 | dropTrailingZeros sc = fastPack (sc <>> [])
23 | roundUp : SnocList Char -> String
25 | roundUp [<'.'] = "1"
26 | roundUp (i :< '.') = show $
cast {to = Integer} (fastPack (i <>> [])) + 1
27 | roundUp (i :< '9') = roundUp i
28 | roundUp (i :< c) = fastPack (i <>> [chr $
ord c + 1])
33 | printDouble : (precision : Nat) -> Double -> String
34 | printDouble prec v =
35 | case forget $
split ('.' ==) (fastUnpack $
show v) of
37 | case splitAt prec y of
38 | (pre,[]) => dropTrailingZeros ([<] <>< (x ++ '.' :: pre))
41 | then dropTrailingZeros ([<] <>< (x ++ '.' :: pre))
42 | else roundUp ([<] <>< (x ++ '.' :: pre))
46 | isInfixAtom : Value -> Bool
47 | isInfixAtom (InfixCons _ _) = False
48 | isInfixAtom (Neg _) = False
49 | isInfixAtom _ = True
51 | isAtom : Value -> Bool
52 | isAtom (Con _ (_ :: _)) = False
53 | isAtom v = isInfixAtom v
55 | onLineAfter : {opts : _} -> Doc opts -> Doc opts -> Doc opts
56 | onLineAfter l d = vappend d (indent 2 l)
58 | toDoc : {opts : _} -> Value -> Doc opts
61 | Con (MkName "") vs => sep $
atoms vs
62 | Con (MkName c) [] => line c
63 | Con (MkName c) vs => prettyCon Open c (atoms vs)
64 | InfixCons v1 cvs => sep (infx v1 cvs)
65 | Rec (MkName c) fs => prettyRecord Open c (fields fs)
66 | Lst vs => list (docs vs)
67 | Tuple v1 v2 vs => tuple (toDoc v1 :: toDoc v2 :: docs vs)
68 | Neg v => line "-" <+> atom v
75 | atom : Value -> Doc opts
76 | atom v = if isAtom v then toDoc v else parens (toDoc v)
80 | atoms : List Value -> List (Doc opts)
82 | atoms (x :: xs) = atom x :: atoms xs
84 | infixAtom : Value -> Doc opts
85 | infixAtom v = if isInfixAtom v then toDoc v else parens (toDoc v)
87 | field : (VName,Value) -> Doc opts
88 | field (MkName s,v) =
89 | let name := line s <++> equals
91 | in ifMultiline (name <++> val) (val `onLineAfter` name)
93 | fields : List (VName,Value) -> List (Doc opts)
95 | fields (p :: ps) = field p :: fields ps
97 | docs : List Value -> List (Doc opts)
99 | docs (x :: xs) = toDoc x :: docs xs
101 | infx : Value -> List (VName,Value) -> List (Doc opts)
102 | infx v [] = [infixAtom v]
103 | infx v ((MkName n,v2)::ps) = (infixAtom v <++> line n) :: infx v2 ps
106 | dfltOpts : LayoutOpts
113 | valToDoc : {opts : _} -> Value -> Doc opts
120 | valToStr : Value -> String
121 | valToStr = render dfltOpts . valToDoc
128 | reify : Show a => a -> Maybe Value
129 | reify = parseValue . show
134 | ppDoc : {opts : _} -> Show a => a -> Doc opts
137 | in maybe (fromString txt) valToDoc (parseValue txt)
141 | ppShow : Show a => a -> String
142 | ppShow = render dfltOpts . ppDoc
148 | ppDocList : {opts : _} -> (Foldable f, Show a) => f a -> Doc opts
149 | ppDocList = list . map ppDoc . toList
155 | ppShowList : (Foldable f, Show a) => f a -> String
156 | ppShowList = render dfltOpts . ppDocList
161 | pPrint : Show a => a -> IO ()
162 | pPrint = putStrLn . ppShow
168 | pPrintList : (Foldable f, Show a) => f a -> IO ()
169 | pPrintList = putStrLn . ppShowList
179 | dumpDoc : {opts : _} -> PrettyVal a => a -> Doc opts
180 | dumpDoc = valToDoc . prettyVal
186 | dumpStr : PrettyVal a => a -> String
187 | dumpStr = render dfltOpts . dumpDoc
192 | dumpIO : PrettyVal a => a -> IO ()
193 | dumpIO = putStrLn . dumpStr
201 | record PreProc a where
202 | constructor MkPreProc
203 | preProc : Value -> Value
207 | PrettyVal a => PrettyVal (PreProc a) where
208 | prettyVal (MkPreProc p v) = p (prettyVal v)
212 | ppHide : (VName -> Bool) -> a -> PreProc a
213 | ppHide p = MkPreProc (hideCon False p)
218 | ppHideNested : (VName -> Bool) -> a -> PreProc a
219 | ppHideNested p = MkPreProc (hideCon True p)