Skip to content

Commit a7b1af4

Browse files
committed
Fix ANSI colored output
The `ansi-wl-pprint` changelog for 1.0.2 says that "Using `show` won't preserve formatting anymore, as `prettyprinter`s `Show Doc` instance is annotation invariant." This patch applies their suggested workaround, `Text.unpack . renderLazy . layoutPretty defaultLayoutOptions`. See: https://github.com/ekmett/ansi-wl-pprint/blob/master/Changelog.md#102
1 parent 5efb53e commit a7b1af4

File tree

3 files changed

+39
-29
lines changed

3 files changed

+39
-29
lines changed

src/Data/TreeDiff/Golden.hs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
{-# LANGUAGE OverloadedStrings #-}
12
-- | "Golden tests" using 'ediff' comparison.
23
module Data.TreeDiff.Golden (
34
ediffGolden,
@@ -8,10 +9,13 @@ import System.Console.ANSI (SGR (Reset), setSGRCode)
89
import Text.Parsec (eof, parse)
910
import Text.Parsec.Text ()
1011

11-
import qualified Data.ByteString as BS
12-
import qualified Data.Text as T
13-
import qualified Data.Text.Encoding as TE
14-
import qualified Text.PrettyPrint.ANSI.Leijen as WL
12+
import qualified Data.ByteString as BS
13+
import qualified Data.Text as T
14+
import qualified Data.Text.Encoding as TE
15+
import Prettyprinter
16+
(LayoutOptions (LayoutOptions, layoutPageWidth),
17+
PageWidth (AvailablePerLine), layoutPretty, unAnnotate)
18+
import Prettyprinter.Render.Terminal (renderStrict)
1519

1620
-- | Make a golden tests.
1721
--
@@ -52,8 +56,6 @@ ediffGolden impl testName fp x = impl testName expect actual cmp wrt
5256
cmp a b
5357
| a == b = return Nothing
5458
| otherwise = return $ Just $
55-
setSGRCode [Reset] ++ showWL (ansiWlEditExprCompact $ ediff a b)
56-
wrt expr = BS.writeFile fp $ TE.encodeUtf8 $ T.pack $ showWL (WL.plain (ansiWlExpr expr)) ++ "\n"
57-
58-
showWL :: WL.Doc -> String
59-
showWL doc = WL.displayS (WL.renderSmart 0.4 80 doc) ""
59+
setSGRCode [Reset] ++ T.unpack (render $ ansiWlEditExprCompact $ ediff a b)
60+
wrt expr = BS.writeFile fp $ TE.encodeUtf8 $ render (unAnnotate (ansiWlExpr expr)) `T.append` "\n"
61+
render = renderStrict . layoutPretty LayoutOptions {layoutPageWidth=AvailablePerLine 80 0.4}

src/Data/TreeDiff/QuickCheck.hs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,18 @@ module Data.TreeDiff.QuickCheck (
33
ediffEq,
44
) where
55

6-
import Data.TreeDiff
7-
import System.Console.ANSI (SGR (Reset), setSGRCode)
8-
import Test.QuickCheck (Property, counterexample)
6+
import qualified Data.Text.Lazy as TL
7+
import Data.TreeDiff
8+
import Prettyprinter
9+
(defaultLayoutOptions, layoutPretty)
10+
import Prettyprinter.Render.Terminal (renderLazy)
11+
import System.Console.ANSI (SGR (Reset), setSGRCode)
12+
import Test.QuickCheck (Property, counterexample)
913

1014
-- | A variant of '===', which outputs a diff when values are inequal.
1115
ediffEq :: (Eq a, ToExpr a) => a -> a -> Property
1216
ediffEq x y = counterexample
13-
(setSGRCode [Reset] ++ show (ansiWlEditExpr $ ediff x y))
17+
(setSGRCode [Reset] ++ render (ansiWlEditExpr $ ediff x y))
1418
(x == y)
19+
where
20+
render = TL.unpack . renderLazy . layoutPretty defaultLayoutOptions

tree-diff.cabal

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -91,21 +91,23 @@ library
9191
, time ^>=1.8.0.2 || ^>=1.9.3 || ^>=1.10 || ^>=1.11 || ^>=1.12
9292

9393
build-depends:
94-
, aeson ^>=2.2.0.0
95-
, ansi-terminal ^>=1.1
96-
, ansi-wl-pprint ^>=1.0.2
97-
, hashable ^>=1.4.4.0 || ^>=1.5.0.0
98-
, parsers ^>=0.12.11
99-
, primitive ^>=0.9.0.0
100-
, QuickCheck ^>=2.14.2 || ^>=2.15
101-
, scientific ^>=0.3.8.0
102-
, semialign ^>=1.3.1
103-
, strict ^>=0.5
104-
, tagged ^>=0.8.8
105-
, these ^>=1.2.1
106-
, unordered-containers ^>=0.2.20
107-
, uuid-types ^>=1.0.6
108-
, vector ^>=0.13.1.0
94+
, aeson ^>=2.2.0.0
95+
, ansi-terminal ^>=1.1
96+
, prettyprinter ^>=1.7.1
97+
, prettyprinter-ansi-terminal ^>=1.1.3
98+
, prettyprinter-compat-ansi-wl-pprint ^>=1.0.2
99+
, hashable ^>=1.4.4.0 || ^>=1.5.0.0
100+
, parsers ^>=0.12.11
101+
, primitive ^>=0.9.0.0
102+
, QuickCheck ^>=2.14.2 || ^>=2.15
103+
, scientific ^>=0.3.8.0
104+
, semialign ^>=1.3.1
105+
, strict ^>=0.5
106+
, tagged ^>=0.8.8
107+
, these ^>=1.2.1
108+
, unordered-containers ^>=0.2.20
109+
, uuid-types ^>=1.0.6
110+
, vector ^>=0.13.1.0
109111

110112
if (impl(ghc >=8) && !impl(ghc >=9.4))
111113
build-depends: data-array-byte ^>=0.1.0.1
@@ -134,7 +136,7 @@ test-suite tree-diff-test
134136
-- dependencies from library
135137
build-depends:
136138
, ansi-terminal
137-
, ansi-wl-pprint
139+
, prettyprinter-compat-ansi-wl-pprint
138140
, base
139141
, parsec
140142
, primitive

0 commit comments

Comments
 (0)