module Language.Haskell.HsColour.ANSI
( highlightOn
, highlightOff
, highlight
, cleareol, clearbol, clearline, clearDown, clearUp, cls
, goto
, cursorUp, cursorDown, cursorLeft, cursorRight
, savePosition, restorePosition
, Highlight(..)
, Colour(..)
, colourCycle
, enableScrollRegion, scrollUp, scrollDown
, lineWrap
) where
import Language.Haskell.HsColour.ColourHighlight
import List (intersperse,isPrefixOf)
import 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 c) = 30 + fromEnum c
fromEnum (Background c) = 40 + fromEnum c
fromEnum Italic = 2
highlight :: [Highlight] -> String -> String
highlight attrs s = highlightOn attrs ++ s ++ highlightOff
highlightOn [] = highlightOn [Normal]
highlightOn attrs = "\ESC["
++ concat (intersperse ";" (map (show.fromEnum) attrs))
++"m"
highlightOff = "\ESC[0m"
colourCycle :: [Colour]
colourCycle = cycle [Red,Blue,Magenta,Green,Cyan]
enableScrollRegion :: Int -> Int -> String
enableScrollRegion start end = "\ESC["++show start++';':show end++"r"
scrollDown = "\ESCD"
scrollUp = "\ESCM"
lineWrap True = "\ESC[7h"
lineWrap False = "\ESC[7l"