Debian Codegraph @

src/System/Console/Terminal/Size.hs

raw
1 {-# LANGUAGE CPP #-}
2 {- |
3 Get terminal window height and width without ncurses dependency
4
5 Based on answer by Andreas Hammar at <http://stackoverflow.com/a/12807521/972985>
6 -}
7 module System.Console.Terminal.Size
8 ( Window(..)
9 , size
10 #if !defined(mingw32_HOST_OS)
11 , fdSize
12 #endif
13 , hSize
14 ) where
15
16 import System.Console.Terminal.Common
17 #if defined(mingw32_HOST_OS)
18 import qualified System.Console.Terminal.Windows as Host
19 #else
20 import qualified System.Console.Terminal.Posix as Host
21 import System.Posix.Types(Fd)
22 #endif
23 import System.IO(Handle)
24
25
26 -- | Get terminal window width and height for @stdout@.
27 --
28 -- >>> import System.Console.Terminal.Size
29 -- >>> size
30 -- Just (Window {height = 60, width = 112})
31 size :: Integral n => IO (Maybe (Window n))
32 size = Host.size
33
34 #if !defined(mingw32_HOST_OS)
35 -- | /Not available on Windows:/
36 -- Get terminal window width and height for a specified file descriptor. If
37 -- it's not attached to a terminal then 'Nothing' is returned.
38 --
39 -- >>> import System.Console.Terminal.Size
40 -- >>> import System.Posix
41 -- >>> fdSize stdOutput
42 -- Just (Window {height = 56, width = 85})
43 -- >>> fd <- openFd "foo" ReadWrite (Just stdFileMode) defaultFileFlags
44 -- >>> fdSize fd
45 -- Nothing
46 fdSize :: Integral n => Fd -> IO (Maybe (Window n))
47 fdSize = Host.fdSize
48 #endif
49
50 -- | Same as 'fdSize', but takes 'Handle' instead of 'Fd' (file descriptor).
51 --
52 -- Note that on Windows with shells that use the native console API (cmd.exe,
53 -- PowerShell) this works only for output handles like 'stdout' and 'stderr';
54 -- for input handles like 'stdin' it always returns 'Nothing'.
55 --
56 -- >>> import System.Console.Terminal.Size
57 -- >>> import System.IO
58 -- >>> hSize stdout
59 -- Just (Window {height = 56, width = 85})
60 hSize :: Integral n => Handle -> IO (Maybe (Window n))
61 hSize = Host.hSize