Skip to content

Commit 1430908

Browse files
committed
Improved formatting and tracing
1 parent 29898f1 commit 1430908

File tree

12 files changed

+355
-105
lines changed

12 files changed

+355
-105
lines changed

optparse-applicative.cabal

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ library
8787
, Options.Applicative.Common
8888
, Options.Applicative.Extra
8989
, Options.Applicative.Help
90+
, Options.Applicative.Help.Ann
9091
, Options.Applicative.Help.Chunk
9192
, Options.Applicative.Help.Core
9293
, Options.Applicative.Help.Levenshtein
@@ -96,10 +97,10 @@ library
9697
, Options.Applicative.Types
9798
, Options.Applicative.Internal
9899

99-
build-depends: base == 4.*
100-
, transformers >= 0.2 && < 0.6
101-
, transformers-compat >= 0.3 && < 0.7
102-
, ansi-wl-pprint >= 0.6.8 && < 0.7
100+
build-depends: base == 4.*
101+
, transformers >= 0.2 && < 0.6
102+
, transformers-compat >= 0.3 && < 0.7
103+
, prettyprinter >= 1.7.0 && < 1.8
103104

104105
if flag(process)
105106
build-depends: process >= 1.0 && < 1.7

src/Options/Applicative.hs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,9 @@ module Options.Applicative (
9494
showDefault,
9595
metavar,
9696
noArgError,
97+
helpAlignUsageOverflow,
98+
helpHangUsageOverflow,
99+
helpRenderHelp,
97100
hidden,
98101
internal,
99102
style,

src/Options/Applicative/BashCompletion.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ bashCompletionQuery pinfo pprefs richness ws i _ = case runCompletion compl ppre
142142
-- If there was a line break, it would come across as a different completion
143143
-- possibility.
144144
render_line :: Int -> Doc -> String
145-
render_line len doc = case lines (displayS (renderPretty 1 len doc) "") of
145+
render_line len doc = case lines (renderShowS (layoutPretty (LayoutOptions (AvailablePerLine len 1.0)) doc) "") of
146146
[] -> ""
147147
[x] -> x
148148
x : _ -> x ++ "..."

src/Options/Applicative/Builder.hs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,10 @@ module Options.Applicative.Builder (
8888
columns,
8989
helpLongEquals,
9090
helpShowGlobals,
91+
helpAlignUsageOverflow,
92+
helpHangUsageOverflow,
9193
helpIndent,
94+
helpRenderHelp,
9295
prefs,
9396
defaultPrefs,
9497

@@ -116,8 +119,9 @@ import Options.Applicative.Builder.Completer
116119
import Options.Applicative.Builder.Internal
117120
import Options.Applicative.Common
118121
import Options.Applicative.Types
119-
import Options.Applicative.Help.Pretty
120122
import Options.Applicative.Help.Chunk
123+
import Options.Applicative.Help.Pretty
124+
import Options.Applicative.Help.Types (renderHelp)
121125

122126
-- Readers --
123127

@@ -521,6 +525,17 @@ helpLongEquals = PrefsMod $ \p -> p { prefHelpLongEquals = True }
521525
helpShowGlobals :: PrefsMod
522526
helpShowGlobals = PrefsMod $ \p -> p { prefHelpShowGlobal = True }
523527

528+
-- | Align usage overflow to the right
529+
helpAlignUsageOverflow :: PrefsMod
530+
helpAlignUsageOverflow = PrefsMod $ \p -> p { prefUsageOverflow = UsageOverflowAlign }
531+
532+
-- | Hang usage overflow to the specified indent
533+
helpHangUsageOverflow :: Int -> PrefsMod
534+
helpHangUsageOverflow indentation = PrefsMod $ \p -> p { prefUsageOverflow = UsageOverflowHang indentation }
535+
536+
helpRenderHelp :: (Int -> ParserHelp -> String) -> PrefsMod
537+
helpRenderHelp f = PrefsMod $ \p -> p { prefRenderHelp = f }
538+
524539
-- | Set fill width in help text presentation.
525540
helpIndent :: Int -> PrefsMod
526541
helpIndent w = PrefsMod $ \p -> p { prefTabulateFill = w }
@@ -540,7 +555,10 @@ prefs m = applyPrefsMod m base
540555
, prefColumns = 80
541556
, prefHelpLongEquals = False
542557
, prefHelpShowGlobal = False
543-
, prefTabulateFill = 24 }
558+
, prefUsageOverflow = UsageOverflowAlign
559+
, prefTabulateFill = 24
560+
, prefRenderHelp = renderHelp
561+
}
544562

545563
-- Convenience shortcuts
546564

src/Options/Applicative/Extra.hs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ module Options.Applicative.Extra (
1313
handleParseResult,
1414
parserFailure,
1515
renderFailure,
16+
renderFailure',
1617
ParserFailure(..),
1718
overFailure,
1819
ParserResult(..),
@@ -104,19 +105,22 @@ execParser = customExecParser defaultPrefs
104105
customExecParser :: ParserPrefs -> ParserInfo a -> IO a
105106
customExecParser pprefs pinfo
106107
= execParserPure pprefs pinfo <$> getArgs
107-
>>= handleParseResult
108+
>>= handleParseResult' pprefs
108109

109110
-- | Handle `ParserResult`.
110111
handleParseResult :: ParserResult a -> IO a
111-
handleParseResult (Success a) = return a
112-
handleParseResult (Failure failure) = do
112+
handleParseResult = handleParseResult' defaultPrefs
113+
114+
handleParseResult' :: ParserPrefs -> ParserResult a -> IO a
115+
handleParseResult' _ (Success a) = return a
116+
handleParseResult' pprefs (Failure failure) = do
113117
progn <- getProgName
114-
let (msg, exit) = renderFailure failure progn
118+
let (msg, exit) = renderFailure' pprefs failure progn
115119
case exit of
116120
ExitSuccess -> putStrLn msg
117121
_ -> hPutStrLn stderr msg
118122
exitWith exit
119-
handleParseResult (CompletionInvoked compl) = do
123+
handleParseResult' _ (CompletionInvoked compl) = do
120124
progn <- getProgName
121125
msg <- execCompletion compl progn
122126
putStr msg
@@ -328,6 +332,9 @@ parserFailure pprefs pinfo msg ctx0 = ParserFailure $ \progn ->
328332
_ -> prefShowHelpOnError pprefs
329333

330334
renderFailure :: ParserFailure ParserHelp -> String -> (String, ExitCode)
331-
renderFailure failure progn =
335+
renderFailure = renderFailure' defaultPrefs
336+
337+
renderFailure' :: ParserPrefs -> ParserFailure ParserHelp -> String -> (String, ExitCode)
338+
renderFailure' pprefs failure progn =
332339
let (h, exit, cols) = execFailure failure progn
333-
in (renderHelp cols h, exit)
340+
in (prefRenderHelp pprefs cols h, exit)

src/Options/Applicative/Help/Ann.hs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{-# LANGUAGE FlexibleInstances #-}
2+
3+
module Options.Applicative.Help.Ann (
4+
Ann(..),
5+
CanAnnotate(..)
6+
) where
7+
8+
import Prettyprinter (Doc, annotate)
9+
10+
data Ann = AnnTrace
11+
Int -- ^ Trace level
12+
String -- ^ Trace message
13+
deriving (Eq, Show)
14+
15+
-- | The minimum trace level for tracing to be included
16+
minTraceLevel :: Int
17+
minTraceLevel = 2
18+
19+
class CanAnnotate a where
20+
annTrace :: Int -> String -> a -> a
21+
22+
instance CanAnnotate (Doc Ann) where
23+
annTrace n = if n >= minTraceLevel then annotate . AnnTrace n else const id

src/Options/Applicative/Help/Chunk.hs

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
{-# LANGUAGE FlexibleInstances #-}
2+
13
module Options.Applicative.Help.Chunk
24
( Chunk(..)
35
, chunked
@@ -11,6 +13,8 @@ module Options.Applicative.Help.Chunk
1113
, paragraph
1214
, extractChunk
1315
, tabulate
16+
, chunkFlatAlt
17+
, chunkIsEffectivelyEmpty
1418
) where
1519

1620
import Control.Applicative
@@ -20,13 +24,17 @@ import Data.Maybe
2024
import Data.Semigroup
2125
import Prelude
2226

27+
import Options.Applicative.Help.Ann
2328
import Options.Applicative.Help.Pretty
2429

2530
-- | The free monoid on a semigroup 'a'.
2631
newtype Chunk a = Chunk
2732
{ unChunk :: Maybe a }
2833
deriving (Eq, Show)
2934

35+
instance CanAnnotate (Chunk Doc) where
36+
annTrace n = fmap . annTrace n
37+
3038
instance Functor Chunk where
3139
fmap f = Chunk . fmap f . unChunk
3240

@@ -89,20 +97,20 @@ extractChunk = fromMaybe mempty . unChunk
8997
-- Unlike '<+>' for 'Doc', this operation has a unit element, namely the empty
9098
-- 'Chunk'.
9199
(<<+>>) :: Chunk Doc -> Chunk Doc -> Chunk Doc
92-
(<<+>>) = chunked (<+>)
100+
(<<+>>) = fmap (annTrace 1 "(<<+>>)") . chunked (<+>)
93101

94102
-- | Concatenate two 'Chunk's with a softline in between. This is exactly like
95103
-- '<<+>>', but uses a softline instead of a space.
96104
(<</>>) :: Chunk Doc -> Chunk Doc -> Chunk Doc
97-
(<</>>) = chunked (</>)
105+
(<</>>) = fmap (annTrace 1 "(<</>>)") . chunked (</>)
98106

99107
-- | Concatenate 'Chunk's vertically.
100108
vcatChunks :: [Chunk Doc] -> Chunk Doc
101-
vcatChunks = foldr (chunked (.$.)) mempty
109+
vcatChunks = fmap (annTrace 1 "vcatChunks") . foldr (chunked (.$.)) mempty
102110

103111
-- | Concatenate 'Chunk's vertically separated by empty lines.
104112
vsepChunks :: [Chunk Doc] -> Chunk Doc
105-
vsepChunks = foldr (chunked (\x y -> x .$. mempty .$. y)) mempty
113+
vsepChunks = annTrace 1 "vsepChunks" . foldr (chunked (\x y -> x .$. mempty .$. y)) mempty
106114

107115
-- | Whether a 'Chunk' is empty. Note that something like 'pure mempty' is not
108116
-- considered an empty chunk, even though the underlying 'Doc' is empty.
@@ -114,8 +122,8 @@ isEmpty = isNothing . unChunk
114122
-- > isEmpty . stringChunk = null
115123
-- > extractChunk . stringChunk = string
116124
stringChunk :: String -> Chunk Doc
117-
stringChunk "" = mempty
118-
stringChunk s = pure (string s)
125+
stringChunk "" = annTrace 0 "stringChunk" mempty
126+
stringChunk s = annTrace 0 "stringChunk" $ pure (string s)
119127

120128
-- | Convert a paragraph into a 'Chunk'. The resulting chunk is composed by the
121129
-- words of the original paragraph separated by softlines, so it will be
@@ -125,12 +133,19 @@ stringChunk s = pure (string s)
125133
--
126134
-- > isEmpty . paragraph = null . words
127135
paragraph :: String -> Chunk Doc
128-
paragraph = foldr (chunked (</>) . stringChunk) mempty
129-
. words
136+
paragraph = annTrace 0 "paragraph"
137+
. foldr (chunked (</>) . stringChunk) mempty
138+
. words
130139

131140
-- | Display pairs of strings in a table.
132141
tabulate :: Int -> [(Doc, Doc)] -> Chunk Doc
133-
tabulate _ [] = mempty
134-
tabulate size table = pure $ vcat
142+
tabulate _ [] = annTrace 1 "tabulate" mempty
143+
tabulate size table = annTrace 1 "tabulate" . pure $ vcat
135144
[ indent 2 (fillBreak size key <+> value)
136145
| (key, value) <- table ]
146+
147+
chunkFlatAlt :: Chunk Doc -> Chunk Doc -> Chunk Doc
148+
chunkFlatAlt a b = pure (flatAlt (extractChunk a) (extractChunk b))
149+
150+
chunkIsEffectivelyEmpty :: Chunk Doc -> Bool
151+
chunkIsEffectivelyEmpty = fromMaybe True . fmap isEffectivelyEmpty . unChunk

0 commit comments

Comments
 (0)