diff options
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 |