Skip to content

Commit 753ac6c

Browse files
committed
Only reset colors when NO_COLOR isn't set
1 parent c7c4d5f commit 753ac6c

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

src/Tldr.hs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,19 @@ toSGR color cons = case color of
5151
, SetBlinkSpeed (blink cons)
5252
]
5353

54+
reset :: ColorSetting -> IO ()
55+
reset color = case color of
56+
NoColor -> pure ()
57+
UseColor -> setSGR [Reset]
58+
5459
renderNode :: NodeType -> ColorSetting -> Handle -> IO ()
55-
renderNode nt@(TEXT txt) color handle = changeConsoleSetting color nt >> TIO.hPutStrLn handle (txt <> "\n") >> setSGR [Reset]
56-
renderNode nt@(HTML_BLOCK txt) color handle = changeConsoleSetting color nt >> TIO.hPutStrLn handle txt >> setSGR [Reset]
57-
renderNode nt@(CODE_BLOCK _ txt) color handle = changeConsoleSetting color nt >> TIO.hPutStrLn handle txt >> setSGR [Reset]
58-
renderNode nt@(HTML_INLINE txt) color handle = changeConsoleSetting color nt >> TIO.hPutStrLn handle txt >> setSGR [Reset]
60+
renderNode nt@(TEXT txt) color handle = changeConsoleSetting color nt >> TIO.hPutStrLn handle (txt <> "\n") >> reset color
61+
renderNode nt@(HTML_BLOCK txt) color handle = changeConsoleSetting color nt >> TIO.hPutStrLn handle txt >> reset color
62+
renderNode nt@(CODE_BLOCK _ txt) color handle = changeConsoleSetting color nt >> TIO.hPutStrLn handle txt >> reset color
63+
renderNode nt@(HTML_INLINE txt) color handle = changeConsoleSetting color nt >> TIO.hPutStrLn handle txt >> reset color
5964
renderNode (CODE txt) color handle = renderCode color txt handle
60-
renderNode nt@LINEBREAK color handle = changeConsoleSetting color nt >> TIO.hPutStrLn handle "" >> setSGR [Reset]
61-
renderNode nt@(LIST _) color handle = changeConsoleSetting color nt >> TIO.hPutStrLn handle "" >> TIO.hPutStr handle " - " >> setSGR [Reset]
65+
renderNode nt@LINEBREAK color handle = changeConsoleSetting color nt >> TIO.hPutStrLn handle "" >> reset color
66+
renderNode nt@(LIST _) color handle = changeConsoleSetting color nt >> TIO.hPutStrLn handle "" >> TIO.hPutStr handle " - " >> reset color
6267
renderNode _ _ _ = return ()
6368

6469
renderCode :: ColorSetting -> Text -> Handle -> IO ()
@@ -67,9 +72,9 @@ renderCode color txt handle = do
6772
case parseOnly codeParser txt of
6873
Right xs -> do
6974
forM_ xs $ \case
70-
Left x -> changeConsoleSetting color (CODE txt) >> TIO.hPutStr handle x >> setSGR [Reset]
75+
Left x -> changeConsoleSetting color (CODE txt) >> TIO.hPutStr handle x >> reset color
7176
Right x -> TIO.hPutStr handle x
72-
Left _ -> changeConsoleSetting color (CODE txt) >> TIO.hPutStr handle txt >> setSGR [Reset]
77+
Left _ -> changeConsoleSetting color (CODE txt) >> TIO.hPutStr handle txt >> reset color
7378
TIO.hPutStr handle ("\n")
7479

7580
changeConsoleSetting :: ColorSetting -> NodeType -> IO ()
@@ -107,7 +112,7 @@ handleNode (Node _ ntype xs) handle color = do
107112
(\(Node _ ntype' ns) ->
108113
renderNode ntype' color handle >> mapM_ (\n -> handleNode n handle color) ns)
109114
xs
110-
setSGR [Reset]
115+
reset color
111116

112117
parsePage :: FilePath -> IO Node
113118
parsePage fname = do

0 commit comments

Comments
 (0)