module Shrun.Configuration.Data.StripControl
  ( StripControl (..),
    parseStripControl,
    ConsoleLogStripControl,
    FileLogStripControl,
  )
where

import Shrun.Configuration.Default (Default (def))
import Shrun.Prelude

data StripControlType
  = StripControlConsoleLog
  | StripControlFileLog

-- | Determines how we should treat control characters encountered in
-- logs.
type StripControl :: StripControlType -> Type
data StripControl t
  = -- | \"Intelligently\" strip control characters e.g. colors are fine,
    -- ones that affect the cursor should be removed.
    StripControlSmart
  | -- | Do not strip any control characters.
    StripControlNone
  | -- | Strip all control characters.
    StripControlAll
  deriving stock (StripControl t
StripControl t -> StripControl t -> Bounded (StripControl t)
forall a. a -> a -> Bounded a
forall (t :: StripControlType). StripControl t
$cminBound :: forall (t :: StripControlType). StripControl t
minBound :: StripControl t
$cmaxBound :: forall (t :: StripControlType). StripControl t
maxBound :: StripControl t
Bounded, Int -> StripControl t
StripControl t -> Int
StripControl t -> [StripControl t]
StripControl t -> StripControl t
StripControl t -> StripControl t -> [StripControl t]
StripControl t
-> StripControl t -> StripControl t -> [StripControl t]
(StripControl t -> StripControl t)
-> (StripControl t -> StripControl t)
-> (Int -> StripControl t)
-> (StripControl t -> Int)
-> (StripControl t -> [StripControl t])
-> (StripControl t -> StripControl t -> [StripControl t])
-> (StripControl t -> StripControl t -> [StripControl t])
-> (StripControl t
    -> StripControl t -> StripControl t -> [StripControl t])
-> Enum (StripControl t)
forall a.
(a -> a)
-> (a -> a)
-> (Int -> a)
-> (a -> Int)
-> (a -> [a])
-> (a -> a -> [a])
-> (a -> a -> [a])
-> (a -> a -> a -> [a])
-> Enum a
forall (t :: StripControlType). Int -> StripControl t
forall (t :: StripControlType). StripControl t -> Int
forall (t :: StripControlType). StripControl t -> [StripControl t]
forall (t :: StripControlType). StripControl t -> StripControl t
forall (t :: StripControlType).
StripControl t -> StripControl t -> [StripControl t]
forall (t :: StripControlType).
StripControl t
-> StripControl t -> StripControl t -> [StripControl t]
$csucc :: forall (t :: StripControlType). StripControl t -> StripControl t
succ :: StripControl t -> StripControl t
$cpred :: forall (t :: StripControlType). StripControl t -> StripControl t
pred :: StripControl t -> StripControl t
$ctoEnum :: forall (t :: StripControlType). Int -> StripControl t
toEnum :: Int -> StripControl t
$cfromEnum :: forall (t :: StripControlType). StripControl t -> Int
fromEnum :: StripControl t -> Int
$cenumFrom :: forall (t :: StripControlType). StripControl t -> [StripControl t]
enumFrom :: StripControl t -> [StripControl t]
$cenumFromThen :: forall (t :: StripControlType).
StripControl t -> StripControl t -> [StripControl t]
enumFromThen :: StripControl t -> StripControl t -> [StripControl t]
$cenumFromTo :: forall (t :: StripControlType).
StripControl t -> StripControl t -> [StripControl t]
enumFromTo :: StripControl t -> StripControl t -> [StripControl t]
$cenumFromThenTo :: forall (t :: StripControlType).
StripControl t
-> StripControl t -> StripControl t -> [StripControl t]
enumFromThenTo :: StripControl t
-> StripControl t -> StripControl t -> [StripControl t]
Enum, StripControl t -> StripControl t -> Bool
(StripControl t -> StripControl t -> Bool)
-> (StripControl t -> StripControl t -> Bool)
-> Eq (StripControl t)
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
forall (t :: StripControlType).
StripControl t -> StripControl t -> Bool
$c== :: forall (t :: StripControlType).
StripControl t -> StripControl t -> Bool
== :: StripControl t -> StripControl t -> Bool
$c/= :: forall (t :: StripControlType).
StripControl t -> StripControl t -> Bool
/= :: StripControl t -> StripControl t -> Bool
Eq, Eq (StripControl t)
Eq (StripControl t) =>
(StripControl t -> StripControl t -> Ordering)
-> (StripControl t -> StripControl t -> Bool)
-> (StripControl t -> StripControl t -> Bool)
-> (StripControl t -> StripControl t -> Bool)
-> (StripControl t -> StripControl t -> Bool)
-> (StripControl t -> StripControl t -> StripControl t)
-> (StripControl t -> StripControl t -> StripControl t)
-> Ord (StripControl t)
StripControl t -> StripControl t -> Bool
StripControl t -> StripControl t -> Ordering
StripControl t -> StripControl t -> StripControl t
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
forall (t :: StripControlType). Eq (StripControl t)
forall (t :: StripControlType).
StripControl t -> StripControl t -> Bool
forall (t :: StripControlType).
StripControl t -> StripControl t -> Ordering
forall (t :: StripControlType).
StripControl t -> StripControl t -> StripControl t
$ccompare :: forall (t :: StripControlType).
StripControl t -> StripControl t -> Ordering
compare :: StripControl t -> StripControl t -> Ordering
$c< :: forall (t :: StripControlType).
StripControl t -> StripControl t -> Bool
< :: StripControl t -> StripControl t -> Bool
$c<= :: forall (t :: StripControlType).
StripControl t -> StripControl t -> Bool
<= :: StripControl t -> StripControl t -> Bool
$c> :: forall (t :: StripControlType).
StripControl t -> StripControl t -> Bool
> :: StripControl t -> StripControl t -> Bool
$c>= :: forall (t :: StripControlType).
StripControl t -> StripControl t -> Bool
>= :: StripControl t -> StripControl t -> Bool
$cmax :: forall (t :: StripControlType).
StripControl t -> StripControl t -> StripControl t
max :: StripControl t -> StripControl t -> StripControl t
$cmin :: forall (t :: StripControlType).
StripControl t -> StripControl t -> StripControl t
min :: StripControl t -> StripControl t -> StripControl t
Ord, Int -> StripControl t -> ShowS
[StripControl t] -> ShowS
StripControl t -> [Char]
(Int -> StripControl t -> ShowS)
-> (StripControl t -> [Char])
-> ([StripControl t] -> ShowS)
-> Show (StripControl t)
forall a.
(Int -> a -> ShowS) -> (a -> [Char]) -> ([a] -> ShowS) -> Show a
forall (t :: StripControlType). Int -> StripControl t -> ShowS
forall (t :: StripControlType). [StripControl t] -> ShowS
forall (t :: StripControlType). StripControl t -> [Char]
$cshowsPrec :: forall (t :: StripControlType). Int -> StripControl t -> ShowS
showsPrec :: Int -> StripControl t -> ShowS
$cshow :: forall (t :: StripControlType). StripControl t -> [Char]
show :: StripControl t -> [Char]
$cshowList :: forall (t :: StripControlType). [StripControl t] -> ShowS
showList :: [StripControl t] -> ShowS
Show)

instance DecodeTOML (StripControl t) where
  tomlDecoder :: Decoder (StripControl t)
tomlDecoder = Decoder Text -> Decoder (StripControl t)
forall (m :: Type -> Type) (t :: StripControlType).
MonadFail m =>
m Text -> m (StripControl t)
parseStripControl Decoder Text
forall a. DecodeTOML a => Decoder a
tomlDecoder

type ConsoleLogStripControl = StripControl StripControlConsoleLog

type FileLogStripControl = StripControl StripControlFileLog

parseStripControl :: (MonadFail m) => m Text -> m (StripControl t)
parseStripControl :: forall (m :: Type -> Type) (t :: StripControlType).
MonadFail m =>
m Text -> m (StripControl t)
parseStripControl m Text
getTxt =
  m Text
getTxt m Text -> (Text -> m (StripControl t)) -> m (StripControl t)
forall a b. m a -> (a -> m b) -> m b
forall (m :: Type -> Type) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
    Text
"all" -> StripControl t -> m (StripControl t)
forall a. a -> m a
forall (f :: Type -> Type) a. Applicative f => a -> f a
pure StripControl t
forall (t :: StripControlType). StripControl t
StripControlAll
    Text
"none" -> StripControl t -> m (StripControl t)
forall a. a -> m a
forall (f :: Type -> Type) a. Applicative f => a -> f a
pure StripControl t
forall (t :: StripControlType). StripControl t
StripControlNone
    Text
"smart" -> StripControl t -> m (StripControl t)
forall a. a -> m a
forall (f :: Type -> Type) a. Applicative f => a -> f a
pure StripControl t
forall (t :: StripControlType). StripControl t
StripControlSmart
    Text
bad ->
      [Char] -> m (StripControl t)
forall a. [Char] -> m a
forall (m :: Type -> Type) a. MonadFail m => [Char] -> m a
fail
        ([Char] -> m (StripControl t)) -> [Char] -> m (StripControl t)
forall a b. (a -> b) -> a -> b
$ [[Char]] -> [Char]
forall a. Monoid a => [a] -> a
mconcat
          [ [Char]
"Wanted one of (all|none|smart), received: ",
            Text -> [Char]
unpack Text
bad
          ]

instance Default ConsoleLogStripControl where
  def :: ConsoleLogStripControl
def = ConsoleLogStripControl
forall (t :: StripControlType). StripControl t
StripControlSmart

instance Default FileLogStripControl where
  def :: FileLogStripControl
def = FileLogStripControl
forall (t :: StripControlType). StripControl t
StripControlAll