mirror of
https://github.com/ilyakooo0/optparse-applicative.git
synced 2024-09-11 10:45:25 +03:00
Improve readability of help text
Union types, which are surrounded by either brackets or parenthesis, shouldn't dangle both sides in the middle of a line. If we see a union, we should either fit it on the current line, or start a new line, unless we're at the start of the nesting group.
This commit is contained in:
parent
4a446427f5
commit
f41e35ac36
@ -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))
|
||||
|
@ -1,10 +1,35 @@
|
||||
module Options.Applicative.Help.Pretty
|
||||
( module Text.PrettyPrint.ANSI.Leijen
|
||||
, (.$.)
|
||||
, groupOrLine
|
||||
) where
|
||||
|
||||
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)
|
||||
|
Loading…
Reference in New Issue
Block a user