module Text.Paint
( Color(..)
, Flag(..)
, Paint(..)
, paint
) where
import qualified Data.Text as T
data Color
= Black
| Red
| Green
| Yellow
| Blue
| Magenta
| Cyan
| LightGray
| DarkGray
| LightRed
| LightGreen
| LightYellow
| LightBlue
| LightMagenta
| LightCyan
| White
| Default
deriving (Eq, Ord, Enum, Show)
data Flag
= Bold
| Underline
| Blink
deriving (Eq, Ord, Show)
data Paint = Paint Color Color [Flag]
flagCode :: Flag
-> Int
flagCode Bold = 1
flagCode Underline = 4
flagCode Blink = 5
fgCode :: Color
-> Int
fgCode Black = 30
fgCode Red = 31
fgCode Green = 32
fgCode Yellow = 33
fgCode Blue = 34
fgCode Magenta = 35
fgCode Cyan = 36
fgCode LightGray = 37
fgCode Default = 39
fgCode DarkGray = 90
fgCode LightRed = 91
fgCode LightGreen = 92
fgCode LightYellow = 93
fgCode LightBlue = 94
fgCode LightMagenta = 95
fgCode LightCyan = 96
fgCode White = 97
bgCode :: Color
-> Int
bgCode Black = 40
bgCode Red = 41
bgCode Green = 42
bgCode Yellow = 43
bgCode Blue = 44
bgCode Magenta = 45
bgCode Cyan = 46
bgCode LightGray = 47
bgCode Default = 49
bgCode DarkGray = 100
bgCode LightRed = 101
bgCode LightGreen = 102
bgCode LightYellow = 103
bgCode LightBlue = 104
bgCode LightMagenta = 105
bgCode LightCyan = 106
bgCode White = 107
translate :: Paint
-> T.Text
translate (Paint fg bg flags) = T.concat [T.pack "\x1b[", codes, T.pack "m"]
where
codes = T.intercalate (T.singleton ';') $ map (T.pack . show) codes'
codes' = map flagCode flags ++ [fgCode fg] ++ [bgCode bg]
paint :: Paint
-> T.Text
-> T.Text
paint pnt text = T.concat [translate pnt, text, T.pack "\x1b[0m"]