terminal-size-0.3.4: Get terminal window height and width

Safe Haskell | Safe-Inferred
Language | Haskell2010

System.Console.Terminal.Size

Description

Get terminal window height and width without ncurses dependency

Based on answer by Andreas Hammar at http://stackoverflow.com/a/12807521/972985

Synopsis

- data Window a = Window {
  - height :: !a
  - width :: !a
  }
- size :: Integral n => IO (Maybe (Window n))
- fdSize :: Integral n => Fd -> IO (Maybe (Window n))
- hSize :: Integral n => Handle -> IO (Maybe (Window n))

# Documentation

data Window a Source #

Terminal window width and height

Constructors

Window |

Fields

- height :: !a
- width :: !a

#### Instances

Instances details

Functor Window Source # |
Instance details Defined in System.Console.Terminal.Common Methods fmap :: (a -> b) -> Window a -> Window b Source # (<$) :: a -> Window b -> Window a Source #
Foldable Window Source # |

Instance details

Defined in System.Console.Terminal.Common

Methods

fold :: Monoid m => Window m -> m Source #

foldMap :: Monoid m => (a -> m) -> Window a -> m Source #

foldMap' :: Monoid m => (a -> m) -> Window a -> m Source #

foldr :: (a -> b -> b) -> b -> Window a -> b Source #

foldr' :: (a -> b -> b) -> b -> Window a -> b Source #

foldl :: (b -> a -> b) -> b -> Window a -> b Source #

foldl' :: (b -> a -> b) -> b -> Window a -> b Source #

foldr1 :: (a -> a -> a) -> Window a -> a Source #

foldl1 :: (a -> a -> a) -> Window a -> a Source #

toList :: Window a -> [a] Source #

null :: Window a -> Bool Source #

length :: Window a -> Int Source #

elem :: Eq a => a -> Window a -> Bool Source #

maximum :: Ord a => Window a -> a Source #

minimum :: Ord a => Window a -> a Source #

sum :: Num a => Window a -> a Source #

product :: Num a => Window a -> a Source #

Traversable Window Source # |
Instance details Defined in System.Console.Terminal.Common Methods traverse :: Applicative f => (a -> f b) -> Window a -> f (Window b) Source # sequenceA :: Applicative f => Window (f a) -> f (Window a) Source # mapM :: Monad m => (a -> m b) -> Window a -> m (Window b) Source # sequence :: Monad m => Window (m a) -> m (Window a) Source #
Generic1 Window Source # |

Instance details

Defined in System.Console.Terminal.Common

Associated Types

type Rep1 Window |

Instance details

Defined in System.Console.Terminal.Common

type Rep1 Window = D1 ('MetaData "Window" "System.Console.Terminal.Common" "terminal-size-0.3.4-5WYBur9DpX11M4bzu1zwp9" 'False) (C1 ('MetaCons "Window" 'PrefixI 'True) (S1 ('MetaSel ('Just "height") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) Par1 :*: S1 ('MetaSel ('Just "width") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) Par1))

Methods

from1 :: Window a -> Rep1 Window a Source #

to1 :: Rep1 Window a -> Window a Source #

Data a => Data (Window a) Source # |

Instance details

Defined in System.Console.Terminal.Common

Methods

gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b) -> (forall g. g -> c g) -> Window a -> c (Window a) Source #

gunfold :: (forall b r. Data b => c (b -> r) -> c r) -> (forall r. r -> c r) -> Constr -> c (Window a) Source #

toConstr :: Window a -> Constr Source #

dataTypeOf :: Window a -> DataType Source #

dataCast1 :: Typeable t => (forall d. Data d => c (t d)) -> Maybe (c (Window a)) Source #

dataCast2 :: Typeable t => (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c (Window a)) Source #

gmapT :: (forall b. Data b => b -> b) -> Window a -> Window a Source #

gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> Window a -> r Source #

gmapQr :: forall r r'. (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> Window a -> r Source #

gmapQ :: (forall d. Data d => d -> u) -> Window a -> [u] Source #

gmapQi :: Int -> (forall d. Data d => d -> u) -> Window a -> u Source #

gmapM :: Monad m => (forall d. Data d => d -> m d) -> Window a -> m (Window a) Source #

gmapMp :: MonadPlus m => (forall d. Data d => d -> m d) -> Window a -> m (Window a) Source #

gmapMo :: MonadPlus m => (forall d. Data d => d -> m d) -> Window a -> m (Window a) Source #

Generic (Window a) Source # |

Instance details

Defined in System.Console.Terminal.Common

Associated Types

type Rep (Window a) |

Instance details

Defined in System.Console.Terminal.Common

type Rep (Window a) = D1 ('MetaData "Window" "System.Console.Terminal.Common" "terminal-size-0.3.4-5WYBur9DpX11M4bzu1zwp9" 'False) (C1 ('MetaCons "Window" 'PrefixI 'True) (S1 ('MetaSel ('Just "height") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 a) :*: S1 ('MetaSel ('Just "width") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 a)))

Methods

from :: Window a -> Rep (Window a) x Source #

to :: Rep (Window a) x -> Window a Source #

Read a => Read (Window a) Source # |
Instance details Defined in System.Console.Terminal.Common Methods readsPrec :: Int -> ReadS (Window a) Source # readList :: ReadS [Window a] Source # readPrec :: ReadPrec (Window a) Source # readListPrec :: ReadPrec [Window a] Source #
Show a => Show (Window a) Source # |
Instance details Defined in System.Console.Terminal.Common Methods showsPrec :: Int -> Window a -> ShowS Source # show :: Window a -> String Source # showList :: [Window a] -> ShowS Source #
Eq a => Eq (Window a) Source # |
Instance details Defined in System.Console.Terminal.Common Methods (==) :: Window a -> Window a -> Bool Source # (/=) :: Window a -> Window a -> Bool Source #
type Rep1 Window Source # |

Instance details

Defined in System.Console.Terminal.Common

type Rep1 Window = D1 ('MetaData "Window" "System.Console.Terminal.Common" "terminal-size-0.3.4-5WYBur9DpX11M4bzu1zwp9" 'False) (C1 ('MetaCons "Window" 'PrefixI 'True) (S1 ('MetaSel ('Just "height") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) Par1 :*: S1 ('MetaSel ('Just "width") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) Par1))

type Rep (Window a) Source # |

Instance details

Defined in System.Console.Terminal.Common

type Rep (Window a) = D1 ('MetaData "Window" "System.Console.Terminal.Common" "terminal-size-0.3.4-5WYBur9DpX11M4bzu1zwp9" 'False) (C1 ('MetaCons "Window" 'PrefixI 'True) (S1 ('MetaSel ('Just "height") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 a) :*: S1 ('MetaSel ('Just "width") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 a)))

size :: Integral n => IO (Maybe (Window n)) Source #

Get terminal window width and height for `stdout`.

    >>> import System.Console.Terminal.Size
    >>> size
    Just (Window {height = 60, width = 112})

fdSize :: Integral n => Fd -> IO (Maybe (Window n)) Source #

Not available on Windows: Get terminal window width and height for a specified file descriptor. If it's not attached to a terminal then `Nothing` is returned.

    >>> import System.Console.Terminal.Size
    >>> import System.Posix
    >>> fdSize stdOutput
    Just (Window {height = 56, width = 85})
    >>> fd <- openFd "foo" ReadWrite (Just stdFileMode) defaultFileFlags
    >>> fdSize fd
    Nothing

hSize :: Integral n => Handle -> IO (Maybe (Window n)) Source #

Same as `fdSize`, but takes `Handle` instead of `Fd` (file descriptor).

Note that on Windows with shells that use the native console API (cmd.exe, PowerShell) this works only for output handles like `stdout` and `stderr`; for input handles like `stdin` it always returns `Nothing`.

    >>> import System.Console.Terminal.Size
    >>> import System.IO
    >>> hSize stdout
    Just (Window {height = 56, width = 85})