summaryrefslogtreecommitdiff
path: root/src/System/IO/Terminal/IO.hs
blob: fb0f1c2c050703aefab2277f6f99b243cac98203 (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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
{-|
Module      : System.IO.Terminal.IO
Copyright   : (c) UU, 2019
License     : MIT
Maintainer  : Tom Smeding
Stability   : experimental
Portability : POSIX, macOS, Windows

Extra terminal management utility functions. This module basically extends
the @ansi-terminal@ package.
-}
module System.IO.Terminal.IO
    (queryTermSize
    -- ,withWinchHandler
    ,toAlternateScreen
    ,fromAlternateScreen)
    where

-- import Foreign.C.Types
import qualified System.Console.Terminal.Size as TS
-- import System.Exit
import System.IO
-- import System.Posix.Signals


-- sigWINCH :: CInt
-- sigWINCH = 28


-- | Request the current terminal size from the terminal. Probably not very
-- fast.
queryTermSize :: IO (Int, Int)
queryTermSize = TS.size >>= \case
    Just win -> return (TS.width win, TS.height win)
    Nothing  -> error "Cannot get terminal size"

-- withWinchHandler :: IO () -> IO a -> IO a
-- withWinchHandler h act = do
--     prevh <- installHandler sigWINCH (Catch h) Nothing
--     case prevh of
--         Default -> return ()
--         Ignore -> return ()
--         _ -> die "ERROR: A signal handler was already installed for the WINCH signal!"

--     res <- act

--     _ <- installHandler sigWINCH prevh Nothing
--     return res

-- | Switch to the \"alternate screen\", if the terminal supports it.
toAlternateScreen :: IO ()
toAlternateScreen = putStr "\x1B[?1049h" >> hFlush stdout

-- | Switch from the \"alternate screen\" back to the normal buffer, if the
-- terminal supports it.
fromAlternateScreen :: IO ()
fromAlternateScreen = putStr "\x1B[?1049l" >> hFlush stdout