0 | module Idris.Pretty.Render
 1 |
 2 | import Core.Context
 3 |
 4 | import Idris.REPL.Opts
 5 |
 6 | import Libraries.Text.PrettyPrint.Prettyprinter
 7 | import public Libraries.Text.PrettyPrint.Prettyprinter.Render.Terminal
 8 |
 9 | import System.Term
10 |
11 | %default total
12 |
13 | getPageWidth : {auto o : Ref ROpts REPLOpts} -> Core PageWidth
14 | getPageWidth = do
15 |   consoleWidth <- getConsoleWidth
16 |   case consoleWidth of
17 |     Nothing => do
18 |       cols <- coreLift getTermCols
19 |       pure $ if cols == 0 then Unbounded else AvailablePerLine cols 1
20 |     Just 0 => pure $ Unbounded
21 |     Just cw => pure $ AvailablePerLine (cast cw) 1
22 |
23 | export
24 | render' : PageWidth ->
25 |           Maybe (ann -> AnsiStyle) ->
26 |           Doc ann -> String
27 | render' pageWidth stylerAnn doc = do
28 |   let opts = MkLayoutOptions pageWidth
29 |   let layout = layoutPretty opts doc
30 |   renderString $ case stylerAnn of
31 |     Just stylerAnn => reAnnotateS stylerAnn layout
32 |     Nothing => unAnnotateS layout
33 |
34 | export
35 | render : {auto o : Ref ROpts REPLOpts} ->
36 |          (ann -> AnsiStyle) ->
37 |          Doc ann -> Core String
38 | render stylerAnn doc = pure $ render' !getPageWidth (toMaybe !getColor stylerAnn) doc
39 |
40 | export
41 | renderWithoutColor : {auto o : Ref ROpts REPLOpts} -> Doc ann -> Core String
42 | renderWithoutColor doc = do
43 |   pageWidth <- getPageWidth
44 |   let opts = MkLayoutOptions pageWidth
45 |   let layout = layoutPretty opts doc
46 |   pure $ renderString $ unAnnotateS layout
47 |
48 | export
49 | renderWithSpans : {auto o : Ref ROpts REPLOpts} ->
50 |   Doc ann ->
51 |   Core (String, List (Span ann))
52 | renderWithSpans doc = do
53 |   pageWidth <- getPageWidth
54 |   let opts = MkLayoutOptions pageWidth
55 |   let layout = layoutPretty opts doc
56 |   pure $ displaySpans layout
57 |