Skip to content

Commit

Permalink
Small improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
HuwCampbell committed Sep 25, 2024
1 parent 2ea40a9 commit 740a022
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 24 deletions.
4 changes: 2 additions & 2 deletions src/Options/Applicative/Common.hs
Original file line number Diff line number Diff line change
Expand Up @@ -266,8 +266,8 @@ mapParser f = flatten . treeMapParser f

-- | Like 'mapParser', but collect the results in a tree structure.
treeMapParser :: (forall x. ArgumentReachability -> Option x -> b)
-> Parser a
-> OptTree b
-> Parser a
-> OptTree b
treeMapParser g = simplify . go False g
where
has_default :: Parser a -> Bool
Expand Down
41 changes: 19 additions & 22 deletions src/Options/Applicative/Help/Core.hs
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,10 @@ briefDesc' :: Bool -> ParserPrefs -> Parser a -> Chunk Doc
briefDesc' showOptional pprefs =
wrapOver NoDefault MaybeRequired
. foldTree pprefs style
. mfilterOptional
. treeMapParser (\a -> (\(_, x, y) -> (x, y)) . optDesc pprefs style a)
. mFilterOptional
. treeMapParser optDesc'
where
mfilterOptional
mFilterOptional
| showOptional =
id
| otherwise =
Expand All @@ -129,6 +129,12 @@ briefDesc' showOptional pprefs =
descHidden = False,
descGlobal = False
}
optDesc' reach opt =
let
(_, a, b) =
optDesc pprefs style reach opt
in
(a, b)

-- | Wrap a doc in parentheses or brackets if required.
wrapOver :: AltNodeType -> Parenthetic -> (Chunk Doc, Parenthetic) -> Chunk Doc
Expand Down Expand Up @@ -331,21 +337,18 @@ optionsDesc global pprefs p =
descGlobal = global
}

--
-- Prints all parent titles that have not already been printed
-- (i.e. in printedGroups).
mkParentDocs :: [String] -> [(Int, String)] -> Doc
mkParentDocs printedGroups = foldl' g (pretty "") . reverse
mkParentDocs printedGroups =
foldr g mempty
where
g :: Doc -> (Int, String) -> Doc
g acc (i, s) =
if s `List.elem` printedGroups
then acc
else
if i == 0
-- Top-level parent has no special formatting
then pretty s .$. acc
-- Nested parent is hyphenated and possibly indented.
else lvlIndentNSub1 i $ hyphenate s .$. acc
g :: (Int, String) -> Doc -> Doc
g (i, s) acc
| s `List.elem` printedGroups = acc
| i == 0 = pretty s .$. acc
| otherwise = lvlIndentNSub1 i $ hyphenate s .$. acc

hyphenate s = pretty ("- " <> s)

Expand Down Expand Up @@ -478,12 +481,6 @@ groupFstAll =
zipWithIndex :: [(a, b)] -> [(Int, (a, b))]
zipWithIndex = zip [1 ..]

-- | From base-4.19.0.0.
unsnoc :: [a] -> Maybe ([a], a)
unsnoc [] = Nothing
unsnoc [x] = Just ([], x)
unsnoc (x:xs) = Just (x:a, b)
where
(a, b) = case unsnoc xs of
Just y -> y
Nothing ->
error "Options.Applicative.Help.Core.unsnoc: impossible"
unsnoc = foldr (\x -> Just . maybe ([], x) (\(~(a, b)) -> (x : a, b))) Nothing

0 comments on commit 740a022

Please sign in to comment.