From 3e71d0ab05c7f5fd71a75b86eeac6f8a1edc3a44 Mon Sep 17 00:00:00 2001 From: stuebinm Date: Mon, 10 Jun 2024 22:30:08 +0200 Subject: pretty-printing of config value sources --- src/Conftrack/Pretty.hs | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 src/Conftrack/Pretty.hs (limited to 'src/Conftrack/Pretty.hs') diff --git a/src/Conftrack/Pretty.hs b/src/Conftrack/Pretty.hs new file mode 100644 index 0000000..8a11204 --- /dev/null +++ b/src/Conftrack/Pretty.hs @@ -0,0 +1,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 -- cgit v1.2.3