summaryrefslogtreecommitdiff
path: root/src/Conftrack/Pretty.hs
blob: 8a11204bd8f4b92f28d85ad49ba1944573512402 (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
{-# LANGUAGE OverloadedStrings #-}

module Conftrack.Pretty where


import Conftrack.Value (Origin (..), ConfigError, ConfigValue (..))
import Conftrack (Warning)
import Data.Map (Map)
import Conftrack.Value (Key)
import qualified Data.Map.Strict as M
import qualified Data.Text.IO as T
import qualified Data.Text as T
import Data.List (sortOn)
import GHC.Exts (groupWith)


printConfigErrors :: [ConfigError] -> IO ()
printConfigErrors = mapM_ print

-- TODO: perhaps sort it by source, not by key?
-- also, shadowed values are currently never read
printConfigOrigins :: Map Key [Origin] -> IO ()
printConfigOrigins =
  mapM_ (T.putStrLn . prettyOrigin)
  . groupWith ((\(Origin _ s) -> s) . head . snd)
  . filter (not . null . snd)
  . M.toList
  where prettyOrigin origins =
          T.concat $ originSource (snd (head origins)) : fmap prettyKey origins
        prettyKey (key, []) = "\n  " <> T.pack (show key)
        prettyKey (key, (Origin val _):shadowed) = T.concat $
          ["\n  ", T.pack $ show key, " = ", prettyValue val]
          <> fmap (\(Origin _ text) -> "\n    (occurrance in "<>text<>" shadowed)") shadowed
        originSource [] = "default value"
        originSource (Origin _ text:_) = text



printConfigWarnings :: [Warning] -> IO ()
printConfigWarnings = mapM_ print