Merge pull request #354 from pcapriotti/topic/improve-readability

Improve readability of help text
This commit is contained in:
Huw Campbell 2019-07-17 23:50:21 +10:00 committed by GitHub
commit 5478fc16cb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 2 deletions

View File

@ -112,7 +112,7 @@ briefDesc' showOptional pprefs
, descHidden = False }
-- | Wrap a doc in parentheses or brackets if required.
wrap :: AltNodeType -> (Chunk Doc, Wrapping) -> Chunk Doc
wrap :: AltNodeType -> (Chunk Doc, Wrapping) -> Chunk Doc
wrap altnode (chunk, wrapping)
| altnode == MarkDefault
= fmap brackets chunk
@ -130,11 +130,13 @@ foldTree (MultNode xs)
= (foldr ((<</>>) . wrap NoDefault . foldTree) mempty xs, Bare)
foldTree (AltNode b xs)
= (\x -> (x, Bare))
. fmap groupOrLine
. wrap b
. alt_node
. filter (not . isEmpty . fst)
. map foldTree $ xs
where
alt_node :: [(Chunk Doc, Wrapping)] -> (Chunk Doc, Wrapping)
alt_node [n] = n
alt_node ns = (\y -> (y, Wrapped))

View File

@ -1,10 +1,35 @@
module Options.Applicative.Help.Pretty
( module Text.PrettyPrint.ANSI.Leijen
, (.$.)
, groupOrLine
) where
import Text.PrettyPrint.ANSI.Leijen hiding ((<$>), (<>), columns)
import Control.Applicative
import Data.Monoid (mappend)
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
(.$.) = (PP.<$>)
-- | Apply the funcion if we're not at the
-- start of our nesting level.
ifNotAtRoot :: (Doc -> Doc) -> Doc -> Doc
ifNotAtRoot f doc =
Nesting $ \i ->
Column $ \j ->
if i == j
then doc
else f doc
-- | Render flattened text on this line, or start
-- a new line before rendering any text.
groupOrLine :: Doc -> Doc
groupOrLine =
Union
<$> flatten
<*> ifNotAtRoot (mappend line)