summaryrefslogtreecommitdiffstats
path: root/src/Much/Screen.hs
blob: b93847f7c83aec13d0b75bb62c6bdd6efdaa737b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
{-# 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)