module GHC.Utils.Ppr.Colour where
import GHC.Prelude.Basic

import Data.Maybe (fromMaybe)
import GHC.Data.Bool
import Data.Semigroup as Semi

-- | A colour\/style for use with 'coloured'.
newtype PprColour = PprColour { PprColour -> String
renderColour :: String }

instance Semi.Semigroup PprColour where
  PprColour String
s1 <> :: PprColour -> PprColour -> PprColour
<> PprColour String
s2 = String -> PprColour
PprColour (String
s1 forall a. Semigroup a => a -> a -> a
<> String
s2)

-- | Allow colours to be combined (e.g. bold + red);
--   In case of conflict, right side takes precedence.
instance Monoid PprColour where
  mempty :: PprColour
mempty = String -> PprColour
PprColour forall a. Monoid a => a
mempty
  mappend :: PprColour -> PprColour -> PprColour
mappend = forall a. Semigroup a => a -> a -> a
(<>)

renderColourAfresh :: PprColour -> String
renderColourAfresh :: PprColour -> String
renderColourAfresh PprColour
c = PprColour -> String
renderColour (PprColour
colReset forall a. Monoid a => a -> a -> a
`mappend` PprColour
c)

colCustom :: String -> PprColour
colCustom :: String -> PprColour
colCustom String
"" = forall a. Monoid a => a
mempty
colCustom String
s  = String -> PprColour
PprColour (String
"\27[" forall a. [a] -> [a] -> [a]
++ String
s forall a. [a] -> [a] -> [a]
++ String
"m")

colReset :: PprColour
colReset :: PprColour
colReset = String -> PprColour
colCustom String
"0"

colBold :: PprColour
colBold :: PprColour
colBold = String -> PprColour
colCustom String
";1"

colBlackFg :: PprColour
colBlackFg :: PprColour
colBlackFg = String -> PprColour
colCustom String
"30"

colRedFg :: PprColour
colRedFg :: PprColour
colRedFg = String -> PprColour
colCustom String
"31"

colGreenFg :: PprColour
colGreenFg :: PprColour
colGreenFg = String -> PprColour
colCustom String
"32"

colYellowFg :: PprColour
colYellowFg :: PprColour
colYellowFg = String -> PprColour
colCustom String
"33"

colBlueFg :: PprColour
colBlueFg :: PprColour
colBlueFg = String -> PprColour
colCustom String
"34"

colMagentaFg :: PprColour
colMagentaFg :: PprColour
colMagentaFg = String -> PprColour
colCustom String
"35"

colCyanFg :: PprColour
colCyanFg :: PprColour
colCyanFg = String -> PprColour
colCustom String
"36"

colWhiteFg :: PprColour
colWhiteFg :: PprColour
colWhiteFg = String -> PprColour
colCustom String
"37"

data Scheme =
  Scheme
  { Scheme -> PprColour
sHeader  :: PprColour
  , Scheme -> PprColour
sMessage :: PprColour
  , Scheme -> PprColour
sWarning :: PprColour
  , Scheme -> PprColour
sError   :: PprColour
  , Scheme -> PprColour
sFatal   :: PprColour
  , Scheme -> PprColour
sMargin  :: PprColour
  }

defaultScheme :: Scheme
defaultScheme :: Scheme
defaultScheme =
  Scheme
  { sHeader :: PprColour
sHeader  = forall a. Monoid a => a
mempty
  , sMessage :: PprColour
sMessage = PprColour
colBold
  , sWarning :: PprColour
sWarning = PprColour
colBold forall a. Monoid a => a -> a -> a
`mappend` PprColour
colMagentaFg
  , sError :: PprColour
sError   = PprColour
colBold forall a. Monoid a => a -> a -> a
`mappend` PprColour
colRedFg
  , sFatal :: PprColour
sFatal   = PprColour
colBold forall a. Monoid a => a -> a -> a
`mappend` PprColour
colRedFg
  , sMargin :: PprColour
sMargin  = PprColour
colBold forall a. Monoid a => a -> a -> a
`mappend` PprColour
colBlueFg
  }

-- | Parse the colour scheme from a string (presumably from the @GHC_COLORS@
-- environment variable).
parseScheme :: String -> (OverridingBool, Scheme) -> (OverridingBool, Scheme)
parseScheme :: String -> (OverridingBool, Scheme) -> (OverridingBool, Scheme)
parseScheme String
"always" (OverridingBool
_, Scheme
cs) = (OverridingBool
Always, Scheme
cs)
parseScheme String
"auto"   (OverridingBool
_, Scheme
cs) = (OverridingBool
Auto,   Scheme
cs)
parseScheme String
"never"  (OverridingBool
_, Scheme
cs) = (OverridingBool
Never,  Scheme
cs)
parseScheme String
input    (OverridingBool
b, Scheme
cs) =
  ( OverridingBool
b
  , Scheme
    { sHeader :: PprColour
sHeader  = forall a. a -> Maybe a -> a
fromMaybe (Scheme -> PprColour
sHeader Scheme
cs)  (forall a b. Eq a => a -> [(a, b)] -> Maybe b
lookup String
"header" [(String, PprColour)]
table)
    , sMessage :: PprColour
sMessage = forall a. a -> Maybe a -> a
fromMaybe (Scheme -> PprColour
sMessage Scheme
cs) (forall a b. Eq a => a -> [(a, b)] -> Maybe b
lookup String
"message" [(String, PprColour)]
table)
    , sWarning :: PprColour
sWarning = forall a. a -> Maybe a -> a
fromMaybe (Scheme -> PprColour
sWarning Scheme
cs) (forall a b. Eq a => a -> [(a, b)] -> Maybe b
lookup String
"warning" [(String, PprColour)]
table)
    , sError :: PprColour
sError   = forall a. a -> Maybe a -> a
fromMaybe (Scheme -> PprColour
sError Scheme
cs)   (forall a b. Eq a => a -> [(a, b)] -> Maybe b
lookup String
"error"   [(String, PprColour)]
table)
    , sFatal :: PprColour
sFatal   = forall a. a -> Maybe a -> a
fromMaybe (Scheme -> PprColour
sFatal Scheme
cs)   (forall a b. Eq a => a -> [(a, b)] -> Maybe b
lookup String
"fatal"   [(String, PprColour)]
table)
    , sMargin :: PprColour
sMargin  = forall a. a -> Maybe a -> a
fromMaybe (Scheme -> PprColour
sMargin Scheme
cs)  (forall a b. Eq a => a -> [(a, b)] -> Maybe b
lookup String
"margin"  [(String, PprColour)]
table)
    }
  )
  where
    split :: Char -> String -> [String]
    split :: Char -> String -> [String]
split Char
c String
s = case forall a. (a -> Bool) -> [a] -> ([a], [a])
break (forall a. Eq a => a -> a -> Bool
==Char
c) String
s of
        (String
chunk,[])     -> [String
chunk]
        (String
chunk,Char
_:String
rest) -> String
chunk forall a. a -> [a] -> [a]
: Char -> String -> [String]
split Char
c String
rest

    table :: [(String, PprColour)]
table = do
      String
w <- Char -> String -> [String]
split Char
':' String
input
      let (String
k, String
v') = forall a. (a -> Bool) -> [a] -> ([a], [a])
break (forall a. Eq a => a -> a -> Bool
== Char
'=') String
w
      case String
v' of
        Char
'=' : String
v -> forall (m :: * -> *) a. Monad m => a -> m a
return (String
k, String -> PprColour
colCustom String
v)
        String
_ -> []