#include "Common-Safe-Haskell.hs"
module System.Console.ANSI.Codes
(
module System.Console.ANSI.Types
, cursorUpCode, cursorDownCode, cursorForwardCode, cursorBackwardCode
, cursorUpLineCode, cursorDownLineCode
, setCursorColumnCode, setCursorPositionCode
, saveCursorCode, restoreCursorCode, reportCursorPositionCode
, clearFromCursorToScreenEndCode, clearFromCursorToScreenBeginningCode
, clearScreenCode, clearFromCursorToLineEndCode
, clearFromCursorToLineBeginningCode, clearLineCode
, scrollPageUpCode, scrollPageDownCode
, setSGRCode
, hideCursorCode, showCursorCode
, hyperlinkCode, hyperlinkWithIdCode, hyperlinkWithParamsCode
, setTitleCode
, colorToCode, csi, sgrToCode
) where
import Data.List (intercalate)
import Data.Colour.SRGB (toSRGB24, RGB (..))
import System.Console.ANSI.Types
csi :: [Int]
-> String
-> String
csi :: [Int] -> String -> String
csi [Int]
args String
code = String
"\ESC[" forall a. [a] -> [a] -> [a]
++ forall a. [a] -> [[a]] -> [a]
intercalate String
";" (forall a b. (a -> b) -> [a] -> [b]
map forall a. Show a => a -> String
show [Int]
args) forall a. [a] -> [a] -> [a]
++ String
code
colorToCode :: Color -> Int
colorToCode :: Color -> Int
colorToCode Color
color = case Color
color of
Color
Black -> Int
0
Color
Red -> Int
1
Color
Green -> Int
2
Color
Yellow -> Int
3
Color
Blue -> Int
4
Color
Magenta -> Int
5
Color
Cyan -> Int
6
Color
White -> Int
7
sgrToCode :: SGR
-> [Int]
sgrToCode :: SGR -> [Int]
sgrToCode SGR
sgr = case SGR
sgr of
SGR
Reset -> [Int
0]
SetConsoleIntensity ConsoleIntensity
intensity -> case ConsoleIntensity
intensity of
ConsoleIntensity
BoldIntensity -> [Int
1]
ConsoleIntensity
FaintIntensity -> [Int
2]
ConsoleIntensity
NormalIntensity -> [Int
22]
SetItalicized Bool
True -> [Int
3]
SetItalicized Bool
False -> [Int
23]
SetUnderlining Underlining
underlining -> case Underlining
underlining of
Underlining
SingleUnderline -> [Int
4]
Underlining
DoubleUnderline -> [Int
21]
Underlining
NoUnderline -> [Int
24]
SetBlinkSpeed BlinkSpeed
blink_speed -> case BlinkSpeed
blink_speed of
BlinkSpeed
SlowBlink -> [Int
5]
BlinkSpeed
RapidBlink -> [Int
6]
BlinkSpeed
NoBlink -> [Int
25]
SetVisible Bool
False -> [Int
8]
SetVisible Bool
True -> [Int
28]
SetSwapForegroundBackground Bool
True -> [Int
7]
SetSwapForegroundBackground Bool
False -> [Int
27]
SetColor ConsoleLayer
Foreground ColorIntensity
Dull Color
color -> [Int
30 forall a. Num a => a -> a -> a
+ Color -> Int
colorToCode Color
color]
SetColor ConsoleLayer
Foreground ColorIntensity
Vivid Color
color -> [Int
90 forall a. Num a => a -> a -> a
+ Color -> Int
colorToCode Color
color]
SetColor ConsoleLayer
Background ColorIntensity
Dull Color
color -> [Int
40 forall a. Num a => a -> a -> a
+ Color -> Int
colorToCode Color
color]
SetColor ConsoleLayer
Background ColorIntensity
Vivid Color
color -> [Int
100 forall a. Num a => a -> a -> a
+ Color -> Int
colorToCode Color
color]
SetPaletteColor ConsoleLayer
Foreground Word8
index -> [Int
38, Int
5, forall a b. (Integral a, Num b) => a -> b
fromIntegral Word8
index]
SetPaletteColor ConsoleLayer
Background Word8
index -> [Int
48, Int
5, forall a b. (Integral a, Num b) => a -> b
fromIntegral Word8
index]
SetRGBColor ConsoleLayer
Foreground Colour Float
color -> [Int
38, Int
2] forall a. [a] -> [a] -> [a]
++ forall {b} {b}. (Num b, RealFrac b, Floating b) => Colour b -> [b]
toRGB Colour Float
color
SetRGBColor ConsoleLayer
Background Colour Float
color -> [Int
48, Int
2] forall a. [a] -> [a] -> [a]
++ forall {b} {b}. (Num b, RealFrac b, Floating b) => Colour b -> [b]
toRGB Colour Float
color
SetDefaultColor ConsoleLayer
Foreground -> [Int
39]
SetDefaultColor ConsoleLayer
Background -> [Int
49]
where
toRGB :: Colour b -> [b]
toRGB Colour b
color = let RGB Word8
r Word8
g Word8
b = forall b. (RealFrac b, Floating b) => Colour b -> RGB Word8
toSRGB24 Colour b
color
in forall a b. (a -> b) -> [a] -> [b]
map forall a b. (Integral a, Num b) => a -> b
fromIntegral [Word8
r, Word8
g, Word8
b]
cursorUpCode, cursorDownCode, cursorForwardCode, cursorBackwardCode
:: Int
-> String
cursorUpCode :: Int -> String
cursorUpCode Int
n = [Int] -> String -> String
csi [Int
n] String
"A"
cursorDownCode :: Int -> String
cursorDownCode Int
n = [Int] -> String -> String
csi [Int
n] String
"B"
cursorForwardCode :: Int -> String
cursorForwardCode Int
n = [Int] -> String -> String
csi [Int
n] String
"C"
cursorBackwardCode :: Int -> String
cursorBackwardCode Int
n = [Int] -> String -> String
csi [Int
n] String
"D"
cursorDownLineCode, cursorUpLineCode :: Int
-> String
cursorDownLineCode :: Int -> String
cursorDownLineCode Int
n = [Int] -> String -> String
csi [Int
n] String
"E"
cursorUpLineCode :: Int -> String
cursorUpLineCode Int
n = [Int] -> String -> String
csi [Int
n] String
"F"
setCursorColumnCode :: Int
-> String
setCursorColumnCode :: Int -> String
setCursorColumnCode Int
n = [Int] -> String -> String
csi [Int
n forall a. Num a => a -> a -> a
+ Int
1] String
"G"
setCursorPositionCode :: Int
-> Int
-> String
setCursorPositionCode :: Int -> Int -> String
setCursorPositionCode Int
n Int
m = [Int] -> String -> String
csi [Int
n forall a. Num a => a -> a -> a
+ Int
1, Int
m forall a. Num a => a -> a -> a
+ Int
1] String
"H"
saveCursorCode, restoreCursorCode :: String
saveCursorCode :: String
saveCursorCode = String
"\ESC7"
restoreCursorCode :: String
restoreCursorCode = String
"\ESC8"
reportCursorPositionCode :: String
reportCursorPositionCode :: String
reportCursorPositionCode = [Int] -> String -> String
csi [] String
"6n"
clearFromCursorToScreenEndCode, clearFromCursorToScreenBeginningCode,
clearScreenCode :: String
clearFromCursorToLineEndCode, clearFromCursorToLineBeginningCode,
clearLineCode :: String
clearFromCursorToScreenEndCode :: String
clearFromCursorToScreenEndCode = [Int] -> String -> String
csi [Int
0] String
"J"
clearFromCursorToScreenBeginningCode :: String
clearFromCursorToScreenBeginningCode = [Int] -> String -> String
csi [Int
1] String
"J"
clearScreenCode :: String
clearScreenCode = [Int] -> String -> String
csi [Int
2] String
"J"
clearFromCursorToLineEndCode :: String
clearFromCursorToLineEndCode = [Int] -> String -> String
csi [Int
0] String
"K"
clearFromCursorToLineBeginningCode :: String
clearFromCursorToLineBeginningCode = [Int] -> String -> String
csi [Int
1] String
"K"
clearLineCode :: String
clearLineCode = [Int] -> String -> String
csi [Int
2] String
"K"
scrollPageUpCode, scrollPageDownCode :: Int
-> String
scrollPageUpCode :: Int -> String
scrollPageUpCode Int
n = [Int] -> String -> String
csi [Int
n] String
"S"
scrollPageDownCode :: Int -> String
scrollPageDownCode Int
n = [Int] -> String -> String
csi [Int
n] String
"T"
setSGRCode :: [SGR]
-> String
setSGRCode :: [SGR] -> String
setSGRCode [SGR]
sgrs = [Int] -> String -> String
csi (forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap SGR -> [Int]
sgrToCode [SGR]
sgrs) String
"m"
hideCursorCode, showCursorCode :: String
hideCursorCode :: String
hideCursorCode = [Int] -> String -> String
csi [] String
"?25l"
showCursorCode :: String
showCursorCode = [Int] -> String -> String
csi [] String
"?25h"
hyperlinkWithParamsCode
:: [(String, String)]
-> String
-> String
-> String
hyperlinkWithParamsCode :: [(String, String)] -> String -> String -> String
hyperlinkWithParamsCode [(String, String)]
ps String
uri String
link =
String
"\ESC]8;" forall a. [a] -> [a] -> [a]
++ String
ps' forall a. [a] -> [a] -> [a]
++ String
";" forall a. [a] -> [a] -> [a]
++ String
uri forall a. [a] -> [a] -> [a]
++ String
"\ESC\\" forall a. [a] -> [a] -> [a]
++ String
link forall a. [a] -> [a] -> [a]
++ String
"\ESC]8;;\ESC\\"
where
ps' :: String
ps' = forall a. [a] -> [[a]] -> [a]
intercalate String
":" forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map (\(String
k, String
v) -> String
k forall a. [a] -> [a] -> [a]
++ String
"=" forall a. [a] -> [a] -> [a]
++ String
v) [(String, String)]
ps
hyperlinkCode
:: String
-> String
-> String
hyperlinkCode :: String -> String -> String
hyperlinkCode = [(String, String)] -> String -> String -> String
hyperlinkWithParamsCode []
hyperlinkWithIdCode
:: String
-> String
-> String
-> String
hyperlinkWithIdCode :: String -> String -> String -> String
hyperlinkWithIdCode String
linkId = [(String, String)] -> String -> String -> String
hyperlinkWithParamsCode [(String
"id", String
linkId)]
setTitleCode :: String
-> String
setTitleCode :: String -> String
setTitleCode String
title = String
"\ESC]0;" forall a. [a] -> [a] -> [a]
++ forall a. (a -> Bool) -> [a] -> [a]
filter (forall a. Eq a => a -> a -> Bool
/= Char
'\007') String
title forall a. [a] -> [a] -> [a]
++ String
"\007"