11{-# LANGUAGE CPP #-}
2- {-# OPTIONS_GHC -fno-warn-warnings-deprecations #-}
32module Options.Applicative.Help.Pretty
4- ( module Text.PrettyPrint.ANSI.Leijen
3+ ( module Prettyprinter
4+ , module Prettyprinter.Render.Terminal
55 , Doc
6- , indent
7- , renderPretty
8- , displayS
6+ , SimpleDoc
7+
98 , (.$.)
9+ , (</>)
10+
1011 , groupOrNestLine
1112 , altSep
1213 , hangAtIfOver
14+
15+ , prettyString
1316 ) where
1417
1518#if !MIN_VERSION_base(4,11,0)
16- import Data.Semigroup ((<>) )
19+ import Data.Semigroup ((<>) , mempty )
1720#endif
1821
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
2126
2227import Prelude
2328
24- type Doc = PP. Doc
29+ type Doc = PP. Doc Prettyprinter.Render.Terminal. AnsiStyle
30+ type SimpleDoc = SimpleDocStream AnsiStyle
2531
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
3434
3535(.$.) :: Doc -> Doc -> Doc
36- (.$.) = (PP. <$>)
37-
36+ x .$. y = x <> line <> y
37+ (</>) :: Doc -> Doc -> Doc
38+ x </> y = x <> softline <> y
3839
3940-- | Apply the function if we're not at the
4041-- start of our nesting level.
@@ -58,7 +59,6 @@ ifElseAtRoot f g doc =
5859 then f doc
5960 else g doc
6061
61-
6262-- | Render flattened text on this line, or start
6363-- a new line before rendering any text.
6464--
@@ -81,7 +81,7 @@ groupOrNestLine =
8181-- next line.
8282altSep :: Doc -> Doc -> Doc
8383altSep x y =
84- group (x <+> char ' |' <> line) <//> y
84+ group (x <+> pretty ' |' <> line) <> group linebreak <> y
8585
8686
8787-- | Printer hacks to get nice indentation for long commands
@@ -102,3 +102,18 @@ hangAtIfOver i j d =
102102 align d
103103 else
104104 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