Skip to content

Commit

Permalink
Don't print out excessive ANSI codes
Browse files Browse the repository at this point in the history
The ANSI renderer was printing out astonishing quantities of SGR 0s,
which don't hurt anything but has bad code aesthetics. Fix it to only
print out font requests when the font changes.

before:

```
♪ pandoc$ cabal exec -- pandoc -t ansi doc/customizing-pandoc.md  | wc -c
   17986
```

after:

```
♪ pandoc$ cabal exec -- pandoc -t ansi doc/customizing-pandoc.md  | wc -c
    5297
```
  • Loading branch information
silby committed Mar 7, 2024
1 parent 6a65ff0 commit 7eee81a
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions src/Text/DocLayout.hs
Original file line number Diff line number Diff line change
Expand Up @@ -289,8 +289,8 @@ render :: HasChars a => Maybe Int -> Doc a -> a
render = renderPlain

renderANSI :: HasChars a => Maybe Int -> Doc a -> a
renderANSI n d = go $ prerender n d where
go (Attributed s) = foldMap attrRender s
renderANSI n d = snd $ go $ prerender n d where
go (Attributed s) = foldl attrRender (baseFont, "") s

renderPlain :: HasChars a => Maybe Int -> Doc a -> a
renderPlain n d = go $ prerender n d where
Expand All @@ -300,10 +300,11 @@ attrStrip :: HasChars a => Attr a -> a
attrStrip (Attr _ y) | isNull y = ""
| otherwise = y

attrRender :: HasChars a => Attr a -> a
attrRender a = go a <> renderFont baseFont where
go (Attr f y) | isNull y = ""
| otherwise = renderFont f <> y
attrRender :: HasChars a => (Font, a) -> Attr a -> (Font, a)
attrRender (f, acc) (Attr g y)
| isNull y = (f, acc)
| f == g = (f, acc <> y)
| otherwise = (g, acc <> renderFont g <> y)

prerender :: HasChars a => Maybe Int -> Doc a -> Attributed a
prerender linelen doc = fromList . reverse . output $
Expand Down

0 comments on commit 7eee81a

Please sign in to comment.