mirror of
https://github.com/urbit/shrub.git
synced 2024-12-22 18:31:44 +03:00
41 lines
950 B
Haskell
41 lines
950 B
Haskell
|
{-# LANGUAGE DeriveAnyClass #-}
|
||
|
{-# LANGUAGE LambdaCase #-}
|
||
|
{-# LANGUAGE RecordWildCards #-}
|
||
|
|
||
|
module Urbit.TermSize
|
||
|
( TermSize(..)
|
||
|
, termSize
|
||
|
, liveTermSize
|
||
|
)
|
||
|
where
|
||
|
|
||
|
import Prelude
|
||
|
|
||
|
import Data.Functor ((<&>))
|
||
|
import System.Console.Terminal.Size (Window(..), size)
|
||
|
|
||
|
import qualified System.Posix.Signals as Sys
|
||
|
import qualified System.Posix.Signals.Exts as Sys
|
||
|
|
||
|
|
||
|
-- Types -----------------------------------------------------------------------
|
||
|
|
||
|
data TermSize = TermSize
|
||
|
{ tsWide :: !Word
|
||
|
, tsTall :: !Word
|
||
|
}
|
||
|
deriving (Eq, Ord, Show)
|
||
|
|
||
|
|
||
|
-- Utilities -------------------------------------------------------------------
|
||
|
|
||
|
termSize :: IO TermSize
|
||
|
termSize = size <&> \case
|
||
|
Nothing -> TermSize 80 24
|
||
|
Just (Window {..}) -> TermSize width height
|
||
|
|
||
|
liveTermSize :: (TermSize -> IO ()) -> IO TermSize
|
||
|
liveTermSize cb = do
|
||
|
Sys.installHandler Sys.sigWINCH (Sys.Catch (termSize >>= cb)) Nothing
|
||
|
termSize
|