module Z.IO.StdStream.Ansi
(
bold, italicize, underline,
color, color', palette, palette', rgb, rgb',
cursorUp, cursorDown, cursorForward, cursorBackward,
cursorDownLine, cursorUpLine ,
setCursorColumn, setCursorPosition, saveCursor, restoreCursor,
clearFromCursorToScreenEnd, clearFromCursorToScreenBeginning, clearScreen,
clearFromCursorToLineEnd, clearFromCursorToLineBeginning, clearLine,
scrollPageUp, scrollPageDown,
hideCursor, showCursor,
setTitle,
reset,
boldIntensity, faintIntensity, resetIntensity,
italicized, noItalicized,
singleUnderline, doubleUnderline, noUnderline,
slowBlink, rapidBlink, blinkOff,
conceal, reveal,
invert, invertOff,
setForeground, setBrightForeground, setBackground, setBrightBackground,
setPaletteForeground, setPaletteBackground,
setRGBForeground, setRGBBackground,
setDefaultForeground, setDefaultBackground,
AnsiColor(..), PaletteColor, RGBColor,
csi, sgr, colorToCode
) where
import qualified Z.Data.Builder as B
import qualified Z.Data.Text as T
import qualified Z.Data.Text.ShowT as T
import Data.Word
import GHC.Generics
csi :: [Int]
-> B.Builder ()
-> B.Builder ()
csi :: [Int] -> Builder () -> Builder ()
csi [Int]
args Builder ()
code = do
Char -> Builder ()
B.char8 Char
'\ESC'
Char -> Builder ()
B.char8 Char
'['
Builder () -> (Int -> Builder ()) -> [Int] -> Builder ()
forall a. Builder () -> (a -> Builder ()) -> [a] -> Builder ()
B.intercalateList (Char -> Builder ()
B.char8 Char
';') Int -> Builder ()
forall a. (Integral a, Bounded a) => a -> Builder ()
B.int [Int]
args
Builder ()
code
cursorUp, cursorDown, cursorForward, cursorBackward
:: Int
-> B.Builder ()
cursorDownLine, cursorUpLine :: Int
-> B.Builder ()
cursorUp :: Int -> Builder ()
cursorUp Int
n = [Int] -> Builder () -> Builder ()
csi [Int
n] (Char -> Builder ()
B.char8 Char
'A')
cursorDown :: Int -> Builder ()
cursorDown Int
n = [Int] -> Builder () -> Builder ()
csi [Int
n] (Char -> Builder ()
B.char8 Char
'B')
cursorForward :: Int -> Builder ()
cursorForward Int
n = [Int] -> Builder () -> Builder ()
csi [Int
n] (Char -> Builder ()
B.char8 Char
'C')
cursorBackward :: Int -> Builder ()
cursorBackward Int
n = [Int] -> Builder () -> Builder ()
csi [Int
n] (Char -> Builder ()
B.char8 Char
'D')
cursorDownLine :: Int -> Builder ()
cursorDownLine Int
n = [Int] -> Builder () -> Builder ()
csi [Int
n] (Char -> Builder ()
B.char8 Char
'E')
cursorUpLine :: Int -> Builder ()
cursorUpLine Int
n = [Int] -> Builder () -> Builder ()
csi [Int
n] (Char -> Builder ()
B.char8 Char
'F')
setCursorColumn :: Int
-> B.Builder ()
setCursorColumn :: Int -> Builder ()
setCursorColumn Int
n = [Int] -> Builder () -> Builder ()
csi [Int
n] (Char -> Builder ()
B.char8 Char
'G')
setCursorPosition :: Int
-> Int
-> B.Builder ()
setCursorPosition :: Int -> Int -> Builder ()
setCursorPosition Int
n Int
m = [Int] -> Builder () -> Builder ()
csi [Int
n, Int
m] (Char -> Builder ()
B.char8 Char
'G')
saveCursor, restoreCursor :: B.Builder ()
saveCursor :: Builder ()
saveCursor = Char -> Builder ()
B.char8 Char
'\ESC' Builder () -> Builder () -> Builder ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Char -> Builder ()
B.char8 Char
'7'
restoreCursor :: Builder ()
restoreCursor = Char -> Builder ()
B.char8 Char
'\ESC' Builder () -> Builder () -> Builder ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Char -> Builder ()
B.char8 Char
'8'
clearFromCursorToScreenEnd, clearFromCursorToScreenBeginning, clearScreen :: B.Builder ()
clearFromCursorToLineEnd, clearFromCursorToLineBeginning, clearLine :: B.Builder ()
clearFromCursorToScreenEnd :: Builder ()
clearFromCursorToScreenEnd = [Int] -> Builder () -> Builder ()
csi [Int
0] (Char -> Builder ()
B.char8 Char
'J')
clearFromCursorToScreenBeginning :: Builder ()
clearFromCursorToScreenBeginning = [Int] -> Builder () -> Builder ()
csi [Int
1] (Char -> Builder ()
B.char8 Char
'J')
clearScreen :: Builder ()
clearScreen = [Int] -> Builder () -> Builder ()
csi [Int
2] (Char -> Builder ()
B.char8 Char
'J')
clearFromCursorToLineEnd :: Builder ()
clearFromCursorToLineEnd = [Int] -> Builder () -> Builder ()
csi [Int
0] (Char -> Builder ()
B.char8 Char
'K')
clearFromCursorToLineBeginning :: Builder ()
clearFromCursorToLineBeginning = [Int] -> Builder () -> Builder ()
csi [Int
1] (Char -> Builder ()
B.char8 Char
'K')
clearLine :: Builder ()
clearLine = [Int] -> Builder () -> Builder ()
csi [Int
2] (Char -> Builder ()
B.char8 Char
'K')
scrollPageUp, scrollPageDown :: Int
-> B.Builder()
scrollPageUp :: Int -> Builder ()
scrollPageUp Int
n = [Int] -> Builder () -> Builder ()
csi [Int
n] (Char -> Builder ()
B.char8 Char
'S')
scrollPageDown :: Int -> Builder ()
scrollPageDown Int
n = [Int] -> Builder () -> Builder ()
csi [Int
n] (Char -> Builder ()
B.char8 Char
'T')
hideCursor, showCursor :: B.Builder ()
hideCursor :: Builder ()
hideCursor = [Int] -> Builder () -> Builder ()
csi [] Builder ()
"?25l"
showCursor :: Builder ()
showCursor = [Int] -> Builder () -> Builder ()
csi [] Builder ()
"?25h"
setTitle :: T.Text
-> B.Builder ()
setTitle :: Text -> Builder ()
setTitle Text
title = do
Builder ()
"\ESC]0;"
Text -> Builder ()
B.text ((Char -> Bool) -> Text -> Text
T.filter (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
/= Char
'\007') Text
title)
Char -> Builder ()
B.char8 Char
'\007'
sgr :: [Word8]
-> B.Builder ()
sgr :: [Word8] -> Builder ()
sgr [Word8]
args = do
Char -> Builder ()
B.char8 Char
'\ESC'
Char -> Builder ()
B.char8 Char
'['
Builder () -> (Word8 -> Builder ()) -> [Word8] -> Builder ()
forall a. Builder () -> (a -> Builder ()) -> [a] -> Builder ()
B.intercalateList (Char -> Builder ()
B.char8 Char
';') Word8 -> Builder ()
forall a. (Integral a, Bounded a) => a -> Builder ()
B.int [Word8]
args
Char -> Builder ()
B.char8 Char
'm'
reset :: B.Builder ()
reset :: Builder ()
reset = [Word8] -> Builder ()
sgr [Word8
0]
boldIntensity, faintIntensity, resetIntensity :: B.Builder ()
boldIntensity :: Builder ()
boldIntensity = [Word8] -> Builder ()
sgr [Word8
1]
faintIntensity :: Builder ()
faintIntensity = [Word8] -> Builder ()
sgr [Word8
2]
resetIntensity :: Builder ()
resetIntensity = [Word8] -> Builder ()
sgr [Word8
22]
bold :: B.Builder () -> B.Builder ()
bold :: Builder () -> Builder ()
bold Builder ()
t = Builder ()
boldIntensity Builder () -> Builder () -> Builder ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Builder ()
t Builder () -> Builder () -> Builder ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Builder ()
resetIntensity
italicized, noItalicized :: B.Builder ()
italicized :: Builder ()
italicized = [Word8] -> Builder ()
sgr [Word8
3]
noItalicized :: Builder ()
noItalicized = [Word8] -> Builder ()
sgr [Word8
23]
italicize :: B.Builder () -> B.Builder ()
italicize :: Builder () -> Builder ()
italicize Builder ()
t = Builder ()
italicized Builder () -> Builder () -> Builder ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Builder ()
t Builder () -> Builder () -> Builder ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Builder ()
noItalicized
singleUnderline, doubleUnderline, noUnderline :: B.Builder ()
singleUnderline :: Builder ()
singleUnderline = [Word8] -> Builder ()
sgr [Word8
4]
doubleUnderline :: Builder ()
doubleUnderline = [Word8] -> Builder ()
sgr [Word8
21]
noUnderline :: Builder ()
noUnderline = [Word8] -> Builder ()
sgr [Word8
24]
underline :: B.Builder () -> B.Builder ()
underline :: Builder () -> Builder ()
underline Builder ()
t = Builder ()
singleUnderline Builder () -> Builder () -> Builder ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Builder ()
t Builder () -> Builder () -> Builder ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Builder ()
singleUnderline
slowBlink, rapidBlink, blinkOff :: B.Builder ()
slowBlink :: Builder ()
slowBlink = [Word8] -> Builder ()
sgr [Word8
5]
rapidBlink :: Builder ()
rapidBlink = [Word8] -> Builder ()
sgr [Word8
6]
blinkOff :: Builder ()
blinkOff = [Word8] -> Builder ()
sgr [Word8
25]
conceal, reveal :: B.Builder ()
conceal :: Builder ()
conceal = [Word8] -> Builder ()
sgr [Word8
8]
reveal :: Builder ()
reveal = [Word8] -> Builder ()
sgr [Word8
28]
invert, invertOff :: B.Builder ()
invert :: Builder ()
invert = [Word8] -> Builder ()
sgr [Word8
7]
invertOff :: Builder ()
invertOff = [Word8] -> Builder ()
sgr [Word8
27]
color :: AnsiColor -> B.Builder () -> B.Builder ()
color :: AnsiColor -> Builder () -> Builder ()
color AnsiColor
c Builder ()
t = do
AnsiColor -> Builder ()
setForeground AnsiColor
c
Builder ()
t
Builder ()
setDefaultForeground
color' :: AnsiColor -> AnsiColor -> B.Builder () -> B.Builder ()
color' :: AnsiColor -> AnsiColor -> Builder () -> Builder ()
color' AnsiColor
c1 AnsiColor
c2 Builder ()
t = do
AnsiColor -> Builder ()
setForeground AnsiColor
c1
AnsiColor -> Builder ()
setBackground AnsiColor
c2
Builder ()
t
Builder ()
setDefaultForeground
Builder ()
setDefaultBackground
palette :: PaletteColor -> B.Builder () -> B.Builder ()
palette :: Word8 -> Builder () -> Builder ()
palette Word8
c Builder ()
t = do
Word8 -> Builder ()
setPaletteForeground Word8
c
Builder ()
t
Builder ()
setDefaultForeground
palette' :: PaletteColor -> PaletteColor -> B.Builder () -> B.Builder ()
palette' :: Word8 -> Word8 -> Builder () -> Builder ()
palette' Word8
c1 Word8
c2 Builder ()
t = do
Word8 -> Builder ()
setPaletteForeground Word8
c1
Word8 -> Builder ()
setPaletteBackground Word8
c2
Builder ()
t
Builder ()
setDefaultForeground
Builder ()
setDefaultBackground
rgb :: RGBColor -> B.Builder () -> B.Builder ()
rgb :: RGBColor -> Builder () -> Builder ()
rgb RGBColor
c Builder ()
t = do
RGBColor -> Builder ()
setRGBForeground RGBColor
c
Builder ()
t
Builder ()
setDefaultForeground
rgb' :: RGBColor -> RGBColor -> B.Builder () -> B.Builder ()
rgb' :: RGBColor -> RGBColor -> Builder () -> Builder ()
rgb' RGBColor
c1 RGBColor
c2 Builder ()
t = do
RGBColor -> Builder ()
setRGBForeground RGBColor
c1
RGBColor -> Builder ()
setRGBBackground RGBColor
c2
Builder ()
t
Builder ()
setDefaultForeground
Builder ()
setDefaultBackground
setForeground, setBrightForeground, setBackground, setBrightBackground :: AnsiColor -> B.Builder ()
setForeground :: AnsiColor -> Builder ()
setForeground AnsiColor
c = [Word8] -> Builder ()
sgr [Word8
30 Word8 -> Word8 -> Word8
forall a. Num a => a -> a -> a
+ AnsiColor -> Word8
colorToCode AnsiColor
c]
setBrightForeground :: AnsiColor -> Builder ()
setBrightForeground AnsiColor
c = [Word8] -> Builder ()
sgr [Word8
90 Word8 -> Word8 -> Word8
forall a. Num a => a -> a -> a
+ AnsiColor -> Word8
colorToCode AnsiColor
c]
setBackground :: AnsiColor -> Builder ()
setBackground AnsiColor
c = [Word8] -> Builder ()
sgr [Word8
40 Word8 -> Word8 -> Word8
forall a. Num a => a -> a -> a
+ AnsiColor -> Word8
colorToCode AnsiColor
c]
setBrightBackground :: AnsiColor -> Builder ()
setBrightBackground AnsiColor
c = [Word8] -> Builder ()
sgr [Word8
100 Word8 -> Word8 -> Word8
forall a. Num a => a -> a -> a
+ AnsiColor -> Word8
colorToCode AnsiColor
c]
setPaletteForeground, setPaletteBackground :: PaletteColor -> B.Builder ()
setPaletteForeground :: Word8 -> Builder ()
setPaletteForeground Word8
index = [Word8] -> Builder ()
sgr [Word8
38, Word8
5, Word8
index]
setPaletteBackground :: Word8 -> Builder ()
setPaletteBackground Word8
index = [Word8] -> Builder ()
sgr [Word8
48, Word8
5, Word8
index]
setRGBForeground, setRGBBackground :: RGBColor -> B.Builder ()
setRGBForeground :: RGBColor -> Builder ()
setRGBForeground (Word8
r,Word8
g,Word8
b) = [Word8] -> Builder ()
sgr [Word8
38, Word8
2, Word8
r, Word8
g, Word8
b]
setRGBBackground :: RGBColor -> Builder ()
setRGBBackground (Word8
r,Word8
g,Word8
b) = [Word8] -> Builder ()
sgr [Word8
48, Word8
2, Word8
r, Word8
g, Word8
b]
setDefaultForeground, setDefaultBackground :: B.Builder ()
setDefaultForeground :: Builder ()
setDefaultForeground = [Word8] -> Builder ()
sgr [Word8
39]
setDefaultBackground :: Builder ()
setDefaultBackground = [Word8] -> Builder ()
sgr [Word8
49]
data AnsiColor = Black
| Red
| Green
| Yellow
| Blue
| Magenta
| Cyan
| White
deriving (AnsiColor -> AnsiColor -> Bool
(AnsiColor -> AnsiColor -> Bool)
-> (AnsiColor -> AnsiColor -> Bool) -> Eq AnsiColor
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: AnsiColor -> AnsiColor -> Bool
$c/= :: AnsiColor -> AnsiColor -> Bool
== :: AnsiColor -> AnsiColor -> Bool
$c== :: AnsiColor -> AnsiColor -> Bool
Eq, Eq AnsiColor
Eq AnsiColor
-> (AnsiColor -> AnsiColor -> Ordering)
-> (AnsiColor -> AnsiColor -> Bool)
-> (AnsiColor -> AnsiColor -> Bool)
-> (AnsiColor -> AnsiColor -> Bool)
-> (AnsiColor -> AnsiColor -> Bool)
-> (AnsiColor -> AnsiColor -> AnsiColor)
-> (AnsiColor -> AnsiColor -> AnsiColor)
-> Ord AnsiColor
AnsiColor -> AnsiColor -> Bool
AnsiColor -> AnsiColor -> Ordering
AnsiColor -> AnsiColor -> AnsiColor
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 :: AnsiColor -> AnsiColor -> AnsiColor
$cmin :: AnsiColor -> AnsiColor -> AnsiColor
max :: AnsiColor -> AnsiColor -> AnsiColor
$cmax :: AnsiColor -> AnsiColor -> AnsiColor
>= :: AnsiColor -> AnsiColor -> Bool
$c>= :: AnsiColor -> AnsiColor -> Bool
> :: AnsiColor -> AnsiColor -> Bool
$c> :: AnsiColor -> AnsiColor -> Bool
<= :: AnsiColor -> AnsiColor -> Bool
$c<= :: AnsiColor -> AnsiColor -> Bool
< :: AnsiColor -> AnsiColor -> Bool
$c< :: AnsiColor -> AnsiColor -> Bool
compare :: AnsiColor -> AnsiColor -> Ordering
$ccompare :: AnsiColor -> AnsiColor -> Ordering
$cp1Ord :: Eq AnsiColor
Ord, AnsiColor
AnsiColor -> AnsiColor -> Bounded AnsiColor
forall a. a -> a -> Bounded a
maxBound :: AnsiColor
$cmaxBound :: AnsiColor
minBound :: AnsiColor
$cminBound :: AnsiColor
Bounded, Int -> AnsiColor
AnsiColor -> Int
AnsiColor -> [AnsiColor]
AnsiColor -> AnsiColor
AnsiColor -> AnsiColor -> [AnsiColor]
AnsiColor -> AnsiColor -> AnsiColor -> [AnsiColor]
(AnsiColor -> AnsiColor)
-> (AnsiColor -> AnsiColor)
-> (Int -> AnsiColor)
-> (AnsiColor -> Int)
-> (AnsiColor -> [AnsiColor])
-> (AnsiColor -> AnsiColor -> [AnsiColor])
-> (AnsiColor -> AnsiColor -> [AnsiColor])
-> (AnsiColor -> AnsiColor -> AnsiColor -> [AnsiColor])
-> Enum AnsiColor
forall a.
(a -> a)
-> (a -> a)
-> (Int -> a)
-> (a -> Int)
-> (a -> [a])
-> (a -> a -> [a])
-> (a -> a -> [a])
-> (a -> a -> a -> [a])
-> Enum a
enumFromThenTo :: AnsiColor -> AnsiColor -> AnsiColor -> [AnsiColor]
$cenumFromThenTo :: AnsiColor -> AnsiColor -> AnsiColor -> [AnsiColor]
enumFromTo :: AnsiColor -> AnsiColor -> [AnsiColor]
$cenumFromTo :: AnsiColor -> AnsiColor -> [AnsiColor]
enumFromThen :: AnsiColor -> AnsiColor -> [AnsiColor]
$cenumFromThen :: AnsiColor -> AnsiColor -> [AnsiColor]
enumFrom :: AnsiColor -> [AnsiColor]
$cenumFrom :: AnsiColor -> [AnsiColor]
fromEnum :: AnsiColor -> Int
$cfromEnum :: AnsiColor -> Int
toEnum :: Int -> AnsiColor
$ctoEnum :: Int -> AnsiColor
pred :: AnsiColor -> AnsiColor
$cpred :: AnsiColor -> AnsiColor
succ :: AnsiColor -> AnsiColor
$csucc :: AnsiColor -> AnsiColor
Enum, Int -> AnsiColor -> ShowS
[AnsiColor] -> ShowS
AnsiColor -> String
(Int -> AnsiColor -> ShowS)
-> (AnsiColor -> String)
-> ([AnsiColor] -> ShowS)
-> Show AnsiColor
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [AnsiColor] -> ShowS
$cshowList :: [AnsiColor] -> ShowS
show :: AnsiColor -> String
$cshow :: AnsiColor -> String
showsPrec :: Int -> AnsiColor -> ShowS
$cshowsPrec :: Int -> AnsiColor -> ShowS
Show, ReadPrec [AnsiColor]
ReadPrec AnsiColor
Int -> ReadS AnsiColor
ReadS [AnsiColor]
(Int -> ReadS AnsiColor)
-> ReadS [AnsiColor]
-> ReadPrec AnsiColor
-> ReadPrec [AnsiColor]
-> Read AnsiColor
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [AnsiColor]
$creadListPrec :: ReadPrec [AnsiColor]
readPrec :: ReadPrec AnsiColor
$creadPrec :: ReadPrec AnsiColor
readList :: ReadS [AnsiColor]
$creadList :: ReadS [AnsiColor]
readsPrec :: Int -> ReadS AnsiColor
$creadsPrec :: Int -> ReadS AnsiColor
Read, (forall x. AnsiColor -> Rep AnsiColor x)
-> (forall x. Rep AnsiColor x -> AnsiColor) -> Generic AnsiColor
forall x. Rep AnsiColor x -> AnsiColor
forall x. AnsiColor -> Rep AnsiColor x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep AnsiColor x -> AnsiColor
$cfrom :: forall x. AnsiColor -> Rep AnsiColor x
Generic)
deriving anyclass Int -> AnsiColor -> Builder ()
(Int -> AnsiColor -> Builder ()) -> ShowT AnsiColor
forall a. (Int -> a -> Builder ()) -> ShowT a
toUTF8BuilderP :: Int -> AnsiColor -> Builder ()
$ctoUTF8BuilderP :: Int -> AnsiColor -> Builder ()
T.ShowT
colorToCode :: AnsiColor -> Word8
colorToCode :: AnsiColor -> Word8
colorToCode AnsiColor
c = case AnsiColor
c of
AnsiColor
Black -> Word8
0
AnsiColor
Red -> Word8
1
AnsiColor
Green -> Word8
2
AnsiColor
Yellow -> Word8
3
AnsiColor
Blue -> Word8
4
AnsiColor
Magenta -> Word8
5
AnsiColor
Cyan -> Word8
6
AnsiColor
White -> Word8
7
type PaletteColor = Word8
type RGBColor = (Word8, Word8, Word8)