1
1
{-# LANGUAGE CPP #-}
2
- {-# OPTIONS_GHC -fno-warn-warnings-deprecations #-}
3
2
module Options.Applicative.Help.Pretty
4
- ( module Text.PrettyPrint.ANSI.Leijen
3
+ ( module Prettyprinter
4
+ , module Prettyprinter.Render.Terminal
5
5
, Doc
6
- , indent
7
- , renderPretty
8
- , displayS
6
+ , SimpleDoc
7
+
9
8
, (.$.)
9
+ , (</>)
10
+
10
11
, groupOrNestLine
11
12
, altSep
12
13
, hangAtIfOver
14
+
15
+ , prettyString
13
16
) where
14
17
15
18
#if !MIN_VERSION_base(4,11,0)
16
- import Data.Semigroup ((<>) )
19
+ import Data.Semigroup ((<>) , mempty )
17
20
#endif
18
21
19
- import Text.PrettyPrint.ANSI.Leijen hiding (Doc , (<$>) , (<>) , columns , indent , renderPretty , displayS )
20
- import qualified Text.PrettyPrint.ANSI.Leijen as PP
22
+ import Prettyprinter hiding (Doc )
23
+ import qualified Prettyprinter as PP
24
+ import qualified Prettyprinter.Render.String as PP
25
+ import Prettyprinter.Render.Terminal
21
26
22
27
import Prelude
23
28
24
- type Doc = PP. Doc
29
+ type Doc = PP. Doc Prettyprinter.Render.Terminal. AnsiStyle
30
+ type SimpleDoc = SimpleDocStream AnsiStyle
25
31
26
- indent :: Int -> PP. Doc -> PP. Doc
27
- indent = PP. indent
28
-
29
- renderPretty :: Float -> Int -> PP. Doc -> SimpleDoc
30
- renderPretty = PP. renderPretty
31
-
32
- displayS :: SimpleDoc -> ShowS
33
- displayS = PP. displayS
32
+ linebreak :: Doc
33
+ linebreak = flatAlt line mempty
34
34
35
35
(.$.) :: Doc -> Doc -> Doc
36
- (.$.) = (PP. <$>)
37
-
36
+ x .$. y = x <> line <> y
37
+ (</>) :: Doc -> Doc -> Doc
38
+ x </> y = x <> softline <> y
38
39
39
40
-- | Apply the function if we're not at the
40
41
-- start of our nesting level.
@@ -58,7 +59,6 @@ ifElseAtRoot f g doc =
58
59
then f doc
59
60
else g doc
60
61
61
-
62
62
-- | Render flattened text on this line, or start
63
63
-- a new line before rendering any text.
64
64
--
@@ -81,7 +81,7 @@ groupOrNestLine =
81
81
-- next line.
82
82
altSep :: Doc -> Doc -> Doc
83
83
altSep x y =
84
- group (x <+> char ' |' <> line) <//> y
84
+ group (x <+> pretty ' |' <> line) <> group linebreak <> y
85
85
86
86
87
87
-- | Printer hacks to get nice indentation for long commands
@@ -102,3 +102,18 @@ hangAtIfOver i j d =
102
102
align d
103
103
else
104
104
linebreak <> ifAtRoot (indent i) d
105
+
106
+
107
+ renderPretty :: Double -> Int -> Doc -> SimpleDocStream AnsiStyle
108
+ renderPretty ribbonFraction lineWidth
109
+ = layoutSmart LayoutOptions
110
+ { layoutPageWidth = AvailablePerLine lineWidth ribbonFraction }
111
+
112
+ prettyString :: Double -> Int -> Doc -> String
113
+ prettyString ribbonFraction lineWidth
114
+ = streamToString
115
+ . renderPretty ribbonFraction lineWidth
116
+
117
+ streamToString :: SimpleDocStream AnsiStyle -> String
118
+ streamToString stream =
119
+ PP. renderShowS stream " "
0 commit comments