Skip to content

Commit 791f618

Browse files
committed
fix: fix alignment issues when command names are too long
1 parent ef39f09 commit 791f618

2 files changed

Lines changed: 10 additions & 7 deletions

File tree

‎src/Data/CLI/Impl.hs‎

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -313,13 +313,16 @@ commandTree :: Parser a -> Doc
313313
commandTree = vsep . commands
314314
where
315315
commands :: Parser x -> [Doc]
316-
commands = concat . mapParser (const entries)
317-
entries :: Option x -> [Doc]
316+
commands p = (\(n, b) -> fill w (pretty n) <+> b) <$> cs
317+
where
318+
cs = concat (mapParser (const entries) p)
319+
w = maximum (0 : fmap (length . fst) cs)
320+
entries :: Option x -> [(String, Doc)]
318321
entries o = case optMain o of
319-
CmdReader _ cs -> uncurry entry <$> cs
322+
CmdReader _ cs -> fmap body <$> cs
320323
_ -> []
321-
entry :: String -> ParserInfo x -> Doc
322-
entry n i =
323-
fill 12 (pretty n) <+> align (fromMaybe mempty (unChunk (infoProgDesc i))) <> case commands (infoParser i) of
324+
body :: ParserInfo x -> Doc
325+
body i =
326+
align (fromMaybe mempty (unChunk (infoProgDesc i))) <> case commands (infoParser i) of
324327
[] -> mempty
325328
sub -> line <> indent 2 (vsep sub) <> line

‎test/Spec.hs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ main = hspec $ do
137137
subparser (leaf "a")
138138
<|> subparser (hidden <> command "b" (info (subparser (leaf "c")) (progDesc "b desc")))
139139
in show (commandTree p)
140-
`shouldBe` "a a desc\nb b desc\n c c desc"
140+
`shouldBe` "a a desc\nb b desc\n c c desc\n"
141141

142142
describe "config keys" $ do
143143
it "parses TOML literals, falling back to strings" $

0 commit comments

Comments
 (0)