forked from gentoo-haskell/hackport
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AnsiColor.hs
46 lines (37 loc) · 1018 Bytes
/
AnsiColor.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
{-|
Stability : provisional
Portability : haskell98
Simplistic ANSI color support.
-}
module AnsiColor
( Color(..)
, bold
, inColor
) where
import Data.List
data Color = Black
| Red
| Green
| Yellow
| Blue
| Magenta
| Cyan
| White
| Default
deriving Enum
esc :: [String] -> String
esc [] = ""
esc xs = "\ESC[" ++ (concat . intersperse ";" $ xs) ++ "m"
col :: Color -> Bool -> Color -> [String]
col fg bf bg = show (fromEnum fg + 30) : bf' [show (fromEnum bg + 40)]
where bf' | bf = ("01" :)
| otherwise = id
inColor :: Color -> Bool -> Color -> String -> String
inColor c bf bg txt = esc (col c bf bg) ++ txt ++ esc ["00"]
bold :: String -> String
bold = ansi "1" "22"
-- italic = ansi "3" "23"
-- underline = ansi "4" "24"
-- inverse = ansi "7" "27"
ansi :: String -> String -> String -> String
ansi on off txt = esc [on] ++ txt ++ esc [off]