module Language.Haskell.HsColour.ANSI
( highlightOnG,highlightOn
, highlightOff
, highlightG,highlight
, cleareol, clearbol, clearline, clearDown, clearUp, cls
, goto
, cursorUp, cursorDown, cursorLeft, cursorRight
, savePosition, restorePosition
, Highlight(..)
, Colour(..)
, colourCycle
, enableScrollRegion, scrollUp, scrollDown
, lineWrap
, TerminalType(..)
) where
import Language.Haskell.HsColour.ColourHighlight
import Language.Haskell.HsColour.Output(TerminalType(..))
import Data.List (intersperse,isPrefixOf)
import Data.Char (isDigit)
type Pos = (Int,Int)
at :: Pos -> String -> String
goto :: Int -> Int -> String
home :: String
cls :: String
at (x,y) s = goto x y ++ s
goto x y = '\ESC':'[':(show y ++(';':show x ++ "H"))
home = goto 1 1
cursorUp = "\ESC[A"
cursorDown = "\ESC[B"
cursorRight = "\ESC[C"
cursorLeft = "\ESC[D"
cleareol = "\ESC[K"
clearbol = "\ESC[1K"
clearline = "\ESC[2K"
clearDown = "\ESC[J"
clearUp = "\ESC[1J"
cls = "\ESC[2J"
savePosition = "\ESC7"
restorePosition = "\ESC8"
instance Enum Highlight where
fromEnum Normal = 0
fromEnum Bold = 1
fromEnum Dim = 2
fromEnum Underscore = 4
fromEnum Blink = 5
fromEnum ReverseVideo = 7
fromEnum Concealed = 8
fromEnum (Foreground (Rgb _ _ _)) = error "Internal error: fromEnum (Foreground (Rgb _ _ _))"
fromEnum (Background (Rgb _ _ _)) = error "Internal error: fromEnum (Background (Rgb _ _ _))"
fromEnum (Foreground c) = 30 + fromEnum c
fromEnum (Background c) = 40 + fromEnum c
fromEnum Italic = 2
highlight :: [Highlight] -> String -> String
highlight = highlightG Ansi16Colour
highlightOn :: [Highlight] -> String
highlightOn = highlightOnG Ansi16Colour
highlightG :: TerminalType -> [Highlight] -> String -> String
highlightG tt attrs s = highlightOnG tt attrs ++ s ++ highlightOff
highlightOnG :: TerminalType -> [Highlight] -> String
highlightOnG tt [] = highlightOnG tt [Normal]
highlightOnG tt attrs = "\ESC["
++ concat (intersperse ";" (concatMap (renderAttrG tt) attrs))
++"m"
highlightOff :: [Char]
highlightOff = "\ESC[0m"
renderAttrG :: TerminalType -> Highlight -> [String]
renderAttrG XTerm256Compatible (Foreground (Rgb r g b)) =
[ "38", "5", show ( rgb24bit_to_xterm256 r g b ) ]
renderAttrG XTerm256Compatible (Background (Rgb r g b)) =
[ "48", "5", show ( rgb24bit_to_xterm256 r g b ) ]
renderAttrG _ a =
[ show (fromEnum (hlProjectToBasicColour8 a)) ]
colourCycle :: [Colour]
colourCycle = cycle [Red,Blue,Magenta,Green,Cyan]
enableScrollRegion :: Int -> Int -> String
enableScrollRegion start end = "\ESC["++show start++';':show end++"r"
scrollDown :: String
scrollDown = "\ESCD"
scrollUp :: String
scrollUp = "\ESCM"
lineWrap :: Bool -> [Char]
lineWrap True = "\ESC[7h"
lineWrap False = "\ESC[7l"