diff options
author | Tom Smeding <tom@tomsmeding.com> | 2021-05-25 21:45:22 +0200 |
---|---|---|
committer | Tom Smeding <tom@tomsmeding.com> | 2021-05-25 21:45:22 +0200 |
commit | 953b23229f38f1b76d130086a213d565f13cdc06 (patch) | |
tree | d59282f7a65fd2dc9ae2f40886930c632595232f /SourceFile.hs | |
parent | b03f51f3a363f861f9d5de30ec6a337fec316383 (diff) |
Diffstat (limited to 'SourceFile.hs')
-rw-r--r-- | SourceFile.hs | 38 |
1 files changed, 22 insertions, 16 deletions
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 |