module Graphics.Vty.Output.XTermColor ( reserveTerminal )
where
import Graphics.Vty.Output.Interface
import qualified Graphics.Vty.Output.TerminfoBased as TerminfoBased
import Blaze.ByteString.Builder (writeToByteString)
import Blaze.ByteString.Builder.Word (writeWord8)
import Control.Applicative
import Control.Monad.Trans
import Data.Foldable (foldMap)
import System.IO
reserveTerminal :: ( Applicative m, MonadIO m ) => String -> Handle -> m Output
reserveTerminal variant outHandle = liftIO $ do
let flushedPut str = do
hPutStr outHandle str
hFlush outHandle
let variant' = if variant == "xterm-color" then "xterm" else variant
flushedPut setUtf8CharSet
t <- TerminfoBased.reserveTerminal variant' outHandle
let t' = t
{ terminalID = terminalID t ++ " (xterm-color)"
, releaseTerminal = do
liftIO $ flushedPut setDefaultCharSet
releaseTerminal t
, mkDisplayContext = \tActual r -> do
dc <- mkDisplayContext t tActual r
return $ dc { inlineHack = xtermInlineHack t' }
}
return t'
setUtf8CharSet, setDefaultCharSet :: String
setUtf8CharSet = "\ESC%G"
setDefaultCharSet = "\ESC%@"
xtermInlineHack :: Output -> IO ()
xtermInlineHack t = do
let writeReset = foldMap (writeWord8.toEnum.fromEnum) "\ESC[K"
outputByteBuffer t $ writeToByteString writeReset