module Jikka.Common.Format.Color
  ( ColorFlag (..),
    withColor,
    withBold,
    hGetColorFlag,
    Color (..),
  )
where

import System.Console.ANSI
import System.IO (Handle)

data ColorFlag
  = EnableColor
  | DisableColor
  deriving (ColorFlag -> ColorFlag -> Bool
(ColorFlag -> ColorFlag -> Bool)
-> (ColorFlag -> ColorFlag -> Bool) -> Eq ColorFlag
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: ColorFlag -> ColorFlag -> Bool
$c/= :: ColorFlag -> ColorFlag -> Bool
== :: ColorFlag -> ColorFlag -> Bool
$c== :: ColorFlag -> ColorFlag -> Bool
Eq, Eq ColorFlag
Eq ColorFlag
-> (ColorFlag -> ColorFlag -> Ordering)
-> (ColorFlag -> ColorFlag -> Bool)
-> (ColorFlag -> ColorFlag -> Bool)
-> (ColorFlag -> ColorFlag -> Bool)
-> (ColorFlag -> ColorFlag -> Bool)
-> (ColorFlag -> ColorFlag -> ColorFlag)
-> (ColorFlag -> ColorFlag -> ColorFlag)
-> Ord ColorFlag
ColorFlag -> ColorFlag -> Bool
ColorFlag -> ColorFlag -> Ordering
ColorFlag -> ColorFlag -> ColorFlag
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
min :: ColorFlag -> ColorFlag -> ColorFlag
$cmin :: ColorFlag -> ColorFlag -> ColorFlag
max :: ColorFlag -> ColorFlag -> ColorFlag
$cmax :: ColorFlag -> ColorFlag -> ColorFlag
>= :: ColorFlag -> ColorFlag -> Bool
$c>= :: ColorFlag -> ColorFlag -> Bool
> :: ColorFlag -> ColorFlag -> Bool
$c> :: ColorFlag -> ColorFlag -> Bool
<= :: ColorFlag -> ColorFlag -> Bool
$c<= :: ColorFlag -> ColorFlag -> Bool
< :: ColorFlag -> ColorFlag -> Bool
$c< :: ColorFlag -> ColorFlag -> Bool
compare :: ColorFlag -> ColorFlag -> Ordering
$ccompare :: ColorFlag -> ColorFlag -> Ordering
$cp1Ord :: Eq ColorFlag
Ord, Int -> ColorFlag -> ShowS
[ColorFlag] -> ShowS
ColorFlag -> String
(Int -> ColorFlag -> ShowS)
-> (ColorFlag -> String)
-> ([ColorFlag] -> ShowS)
-> Show ColorFlag
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [ColorFlag] -> ShowS
$cshowList :: [ColorFlag] -> ShowS
show :: ColorFlag -> String
$cshow :: ColorFlag -> String
showsPrec :: Int -> ColorFlag -> ShowS
$cshowsPrec :: Int -> ColorFlag -> ShowS
Show, ReadPrec [ColorFlag]
ReadPrec ColorFlag
Int -> ReadS ColorFlag
ReadS [ColorFlag]
(Int -> ReadS ColorFlag)
-> ReadS [ColorFlag]
-> ReadPrec ColorFlag
-> ReadPrec [ColorFlag]
-> Read ColorFlag
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [ColorFlag]
$creadListPrec :: ReadPrec [ColorFlag]
readPrec :: ReadPrec ColorFlag
$creadPrec :: ReadPrec ColorFlag
readList :: ReadS [ColorFlag]
$creadList :: ReadS [ColorFlag]
readsPrec :: Int -> ReadS ColorFlag
$creadsPrec :: Int -> ReadS ColorFlag
Read)

withColor :: ColorFlag -> Color -> String -> String
withColor :: ColorFlag -> Color -> ShowS
withColor ColorFlag
DisableColor Color
_ String
s = String
s
withColor ColorFlag
EnableColor Color
color String
s = [SGR] -> String
setSGRCode [ConsoleLayer -> ColorIntensity -> Color -> SGR
SetColor ConsoleLayer
Foreground ColorIntensity
Vivid Color
color] String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
s String -> ShowS
forall a. [a] -> [a] -> [a]
++ [SGR] -> String
setSGRCode [ConsoleLayer -> ColorIntensity -> Color -> SGR
SetColor ConsoleLayer
Foreground ColorIntensity
Dull Color
White]

withBold :: ColorFlag -> String -> String
withBold :: ColorFlag -> ShowS
withBold ColorFlag
DisableColor String
s = String
s
withBold ColorFlag
EnableColor String
s = [SGR] -> String
setSGRCode [ConsoleIntensity -> SGR
SetConsoleIntensity ConsoleIntensity
BoldIntensity] String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
s String -> ShowS
forall a. [a] -> [a] -> [a]
++ [SGR] -> String
setSGRCode [ConsoleIntensity -> SGR
SetConsoleIntensity ConsoleIntensity
NormalIntensity]

hGetColorFlag :: Handle -> IO ColorFlag
hGetColorFlag :: Handle -> IO ColorFlag
hGetColorFlag Handle
handle = do
  Bool
supported <- Handle -> IO Bool
hSupportsANSI Handle
handle
  ColorFlag -> IO ColorFlag
forall (m :: * -> *) a. Monad m => a -> m a
return (if Bool
supported then ColorFlag
EnableColor else ColorFlag
DisableColor)