{-# LANGUAGE RecordWildCards #-} module Much.Screen (Screen(..), setScreen, withScreen) where import Control.Exception import Data.List import System.IO data Screen = Screen { inputEcho :: Bool , inputBufferMode :: BufferMode , outputBufferMode :: BufferMode , decsetPm :: [Int] , decrstPm :: [Int] } setScreen :: Handle -> Handle -> Screen -> IO Screen setScreen i o Screen{..} = get <* set where get = Screen <$> hGetEcho i <*> hGetBuffering i <*> hGetBuffering o <*> pure decrstPm <*> pure decsetPm set = do hSetEcho i inputEcho hSetBuffering i inputBufferMode hSetBuffering o outputBufferMode hPutStr o $ "\ESC[?" ++ intercalate ";" (map show decsetPm) ++ "h" hPutStr o $ "\ESC[?" ++ intercalate ";" (map show decrstPm) ++ "l" hFlush o withScreen :: Handle -> Handle -> Screen -> (Screen -> IO a) -> IO a withScreen i o s = bracket (setScreen i o s) (setScreen i o)