{-# LANGUAGE CPP #-}
module Options.Applicative.Help.Pretty
( module Text.PrettyPrint.ANSI.Leijen
, (.$.)
, groupOrNestLine
, altSep
, hangAtIfOver
) where
import Control.Applicative
#if !MIN_VERSION_base(4,11,0)
import Data.Semigroup ((<>))
#endif
import Text.PrettyPrint.ANSI.Leijen hiding ((<$>), (<>), columns)
import Text.PrettyPrint.ANSI.Leijen.Internal (Doc (..), flatten)
import qualified Text.PrettyPrint.ANSI.Leijen as PP
import Prelude
(.$.) :: Doc -> Doc -> Doc
.$. :: Doc -> Doc -> Doc
(.$.) = Doc -> Doc -> Doc
(PP.<$>)
ifNotAtRoot :: (Doc -> Doc) -> Doc -> Doc
ifNotAtRoot :: (Doc -> Doc) -> Doc -> Doc
ifNotAtRoot =
(Doc -> Doc) -> (Doc -> Doc) -> Doc -> Doc
ifElseAtRoot forall a. a -> a
id
ifAtRoot :: (Doc -> Doc) -> Doc -> Doc
ifAtRoot :: (Doc -> Doc) -> Doc -> Doc
ifAtRoot =
forall a b c. (a -> b -> c) -> b -> a -> c
flip (Doc -> Doc) -> (Doc -> Doc) -> Doc -> Doc
ifElseAtRoot forall a. a -> a
id
ifElseAtRoot :: (Doc -> Doc) -> (Doc -> Doc) -> Doc -> Doc
ifElseAtRoot :: (Doc -> Doc) -> (Doc -> Doc) -> Doc -> Doc
ifElseAtRoot Doc -> Doc
f Doc -> Doc
g Doc
doc =
(Int -> Doc) -> Doc
Nesting forall a b. (a -> b) -> a -> b
$ \Int
i ->
(Int -> Doc) -> Doc
Column forall a b. (a -> b) -> a -> b
$ \Int
j ->
if Int
i forall a. Eq a => a -> a -> Bool
== Int
j
then Doc -> Doc
f Doc
doc
else Doc -> Doc
g Doc
doc
groupOrNestLine :: Doc -> Doc
groupOrNestLine :: Doc -> Doc
groupOrNestLine =
Doc -> Doc -> Doc
Union
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Doc -> Doc
flatten
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (Doc -> Doc) -> Doc -> Doc
ifNotAtRoot (Doc
line forall a. Semigroup a => a -> a -> a
<>) forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Doc -> Doc
nest Int
2
altSep :: Doc -> Doc -> Doc
altSep :: Doc -> Doc -> Doc
altSep Doc
x Doc
y =
Doc -> Doc
group (Doc
x Doc -> Doc -> Doc
<+> Char -> Doc
char Char
'|' forall a. Semigroup a => a -> a -> a
<> Doc
line) Doc -> Doc -> Doc
<//> Doc
y
hangAtIfOver :: Int -> Int -> Doc -> Doc
hangAtIfOver :: Int -> Int -> Doc -> Doc
hangAtIfOver Int
i Int
j Doc
d =
(Int -> Doc) -> Doc
Column forall a b. (a -> b) -> a -> b
$ \Int
k ->
if Int
k forall a. Ord a => a -> a -> Bool
<= Int
j then
Doc -> Doc
align Doc
d
else
Doc
linebreak forall a. Semigroup a => a -> a -> a
<> (Doc -> Doc) -> Doc -> Doc
ifAtRoot (Int -> Doc -> Doc
indent Int
i) Doc
d