From eaba4b2faceab9c2236c5f1448d90c5eb7092706 Mon Sep 17 00:00:00 2001 From: Jens Petersen Date: Tue, 31 May 2022 13:24:36 +0800 Subject: [PATCH] further reduce newlines for compact output (#84) added a doctest for a simple Value type --- src/Text/Pretty/Simple.hs | 6 ++++++ src/Text/Pretty/Simple/Internal/Printer.hs | 5 ++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/Text/Pretty/Simple.hs b/src/Text/Pretty/Simple.hs index c6a21b0..37eda57 100644 --- a/src/Text/Pretty/Simple.hs +++ b/src/Text/Pretty/Simple.hs @@ -680,6 +680,12 @@ layoutStringAnsi opts = fmap convertStyle . layoutString opts -- , B -- ( B ( B A ) ) ] ) -- +-- >>> data Value = ValueStruct [(String,Value)] | ValueInt Int deriving Show +-- +-- >>> pPrintOpt CheckColorTty defaultOutputOptionsDarkBg {outputOptionsCompact = True} $ ValueStruct [("id", ValueInt 123), ("state", ValueInt 1)] +-- ValueStruct +-- [ ( "id", ValueInt 123 ), ( "state", ValueInt 1 ) ] +-- -- __Initial indent__ -- -- >>> pPrintOpt CheckColorTty defaultOutputOptionsDarkBg {outputOptionsInitialIndent = 3} $ B ( B ( B ( B A ) ) ) diff --git a/src/Text/Pretty/Simple/Internal/Printer.hs b/src/Text/Pretty/Simple/Internal/Printer.hs index 14c64ae..8c3c5c4 100644 --- a/src/Text/Pretty/Simple/Internal/Printer.hs +++ b/src/Text/Pretty/Simple/Internal/Printer.hs @@ -255,7 +255,10 @@ prettyExpr opts = (if outputOptionsCompact opts then group else id) . \case spaceIfNeeded = \case Other (' ' : _) : _ -> mempty _ -> space - lineAndCommaSep x y = x <> line' <> annotate Comma "," <> y + lineAndCommaSep x y = + if outputOptionsCompact opts + then x <> annotate Comma "," <> y + else x <> line' <> annotate Comma "," <> y -- | Determine whether this expression should be displayed on a single line. isSimple :: Expr -> Bool