summaryrefslogtreecommitdiff
path: root/SourceFile.hs
blob: 8fc9dbfb8c6e03bdebd6871777bfb6be59c9b7b0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
module SourceFile where

import qualified Data.Text.Lazy.Builder as B
import Data.Text.Lazy.Builder (Builder)
import Data.Text.Lazy (Text)

import Pretty


data SourceFile = SourceFile [Chunk]
  deriving (Show)

data Chunk = CSkip Text
           | COcc Occurrence
  deriving (Show)

data Occurrence = Occurrence
    { occName :: Text
    , occArgs :: [(Text, Text)] }  -- prefix spacing, argument value
  deriving (Show)

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