From 953b23229f38f1b76d130086a213d565f13cdc06 Mon Sep 17 00:00:00 2001 From: Tom Smeding Date: Tue, 25 May 2021 21:45:22 +0200 Subject: Make it work --- SourceFile.hs | 38 ++++++++++++++++++++++---------------- 1 file changed, 22 insertions(+), 16 deletions(-) (limited to 'SourceFile.hs') diff --git a/SourceFile.hs b/SourceFile.hs index 0a054c0..8fc9dbf 100644 --- a/SourceFile.hs +++ b/SourceFile.hs @@ -19,19 +19,25 @@ data Occurrence = Occurrence , occArgs :: [(Text, Text)] } -- prefix spacing, argument value deriving (Show) -instance Pretty SourceFile where - pretty' (SourceFile cs) = mconcat (map pretty' cs) - -instance Pretty Chunk where - pretty' (CSkip s) = B.fromLazyText s - pretty' (COcc occ) = pretty' occ - -instance Pretty Occurrence where - pretty' (Occurrence name args) = - ansiBG '1' (B.fromLazyText name) - <> mconcat [ansiBG '4' (B.fromLazyText pre) - <> ansiBG '3' (B.fromLazyText val) - | (pre, val) <- args] - where - ansiBG :: Char -> Builder -> Builder - ansiBG c b = B.fromString ("\x1B[1;4" ++ c : "m") <> b <> B.fromString "\x1B[0m" +occurrenceToBuilder :: Bool -> Occurrence -> Builder +occurrenceToBuilder useClr (Occurrence name args) = + ansiBG '1' (B.fromLazyText name) + <> mconcat [ansiBG '4' (B.fromLazyText pre) + <> ansiBG '3' (B.fromLazyText val) + | (pre, val) <- args] + where + ansiBG :: Char -> Builder -> Builder + ansiBG c b + | useClr && stdoutIsTTY = B.fromString ("\x1B[1;4" ++ c : "m") <> b <> B.fromString "\x1B[0m" + | otherwise = b + +chunkToBuilder :: Bool -> Chunk -> Builder +chunkToBuilder _ (CSkip s) = B.fromLazyText s +chunkToBuilder useClr (COcc occ) = occurrenceToBuilder useClr occ + +sourceFileToBuilder :: Bool -> SourceFile -> Builder +sourceFileToBuilder useClr (SourceFile cs) = mconcat (map (chunkToBuilder useClr) cs) + +instance Pretty SourceFile where pretty' = sourceFileToBuilder True +instance Pretty Chunk where pretty' = chunkToBuilder True +instance Pretty Occurrence where pretty' = occurrenceToBuilder True -- cgit v1.2.3-70-g09d2