0 | module Text.PrettyPrint.Bernardy.Interface
4 | import Data.SortedMap
5 | import Data.SortedSet
6 | import Text.PrettyPrint.Bernardy.Combinators
7 | import Text.PrettyPrint.Bernardy.Core
12 | interface Pretty a where
13 | constructor MkPretty
14 | prettyPrec : {opts : _} -> Prec -> a -> Doc opts
17 | public export %inline
18 | pretty : Pretty a => {opts : _} -> a -> Doc opts
19 | pretty = prettyPrec Open
22 | public export %inline
23 | prettyArg : Pretty a => {opts : _} -> a -> Doc opts
24 | prettyArg = prettyPrec App
26 | onLineAfter : {opts : _} -> Doc opts -> Doc opts -> Doc opts
27 | onLineAfter l d = vappend d (indent 2 l)
49 | prettyCon : {opts : _} -> Prec -> String -> List (Doc opts) -> Doc opts
50 | prettyCon p c [] = line c
51 | prettyCon p c args =
53 | in parenthesise (p >= App) $
54 | ifMultiline (hsep $
con :: args) (vsep args `onLineAfter` con)
65 | prettyField : Pretty a => {opts : _} -> String -> a -> Doc opts
67 | let name := line s <++> equals
69 | in ifMultiline (name <++> val) (val `onLineAfter` name)
89 | prettyRecord : {opts : _} -> Prec -> String -> List (Doc opts) -> Doc opts
90 | prettyRecord p c [] = line c
91 | prettyRecord p c args =
94 | in parenthesise (p >= App) $
95 | ifMultiline (con <++> flds) (flds `onLineAfter` con)
101 | public export %inline
103 | prettyPrec _ = line . show
105 | public export %inline
106 | Pretty String where
107 | prettyPrec _ = text . show
109 | public export %inline
111 | prettyPrec _ = line . show
113 | public export %inline
114 | Pretty Bits16 where
115 | prettyPrec _ = line . show
117 | public export %inline
118 | Pretty Bits32 where
119 | prettyPrec _ = line . show
121 | public export %inline
122 | Pretty Bits64 where
123 | prettyPrec _ = line . show
125 | public export %inline
127 | prettyPrec _ = line . show
129 | public export %inline
131 | prettyPrec _ = line . show
133 | public export %inline
135 | prettyPrec _ = line . show
137 | public export %inline
139 | prettyPrec _ = line . show
141 | public export %inline
142 | Pretty Integer where
143 | prettyPrec _ = line . show
145 | public export %inline
146 | Pretty Double where
147 | prettyPrec _ = line . show
149 | public export %inline
151 | prettyPrec _ () = line "()"
153 | public export %inline
155 | prettyPrec _ True = "True"
156 | prettyPrec _ False = "False"
158 | public export %inline
159 | Pretty Ordering where
160 | prettyPrec _ LT = "LT"
161 | prettyPrec _ EQ = "EQ"
162 | prettyPrec _ GT = "GT"
164 | public export %inline
166 | prettyPrec _ = line . show
168 | public export %inline
170 | prettyPrec _ = line . show
173 | Pretty a => Pretty (Maybe a) where
174 | prettyPrec p Nothing = line "Nothing"
175 | prettyPrec p (Just x) = prettyCon p "Just" [prettyArg x]
178 | Pretty a => Pretty b => Pretty (Either a b) where
179 | prettyPrec p (Left x) = prettyCon p "Left" [prettyArg x]
180 | prettyPrec p (Right x) = prettyCon p "Right" [prettyArg x]
182 | public export %inline
183 | Pretty a => Pretty (List a) where
184 | prettyPrec _ = list . map pretty
186 | public export %inline
187 | Pretty a => Pretty (List1 a) where
188 | prettyPrec p vs = prettyPrec p (toList vs)
190 | public export %inline
191 | Pretty a => Pretty (SnocList a) where
192 | prettyPrec _ = snocList . map pretty . (<>> [])
195 | Pretty a => Pretty b => Pretty (a,b) where
196 | prettyPrec _ (x,y) = tuple [pretty x, pretty y]
199 | Pretty a => Pretty (Vect n a) where
200 | prettyPrec p vs = prettyPrec p (toList vs)
203 | Pretty k => Pretty v => Pretty (SortedMap k v) where
204 | prettyPrec p m = prettyCon p "fromList" [prettyArg $
SortedMap.toList m]
207 | Pretty k => Pretty (SortedSet k) where
208 | prettyPrec p m = prettyCon p "fromList" [prettyArg $
Prelude.toList m]
212 | export %defaulthint
213 | ThroughPretty : Pretty a => (layout : LayoutOpts) => Interpolation a
214 | ThroughPretty = TP where [TP] Interpolation a where interpolate = render layout . pretty