shrub/pkg/hs/urbit-termsize/lib/Urbit/TermSize.hs
Elliot Glaysher 20a6c0331c king: set initial terminal size and react to resizes
This changes startup so we get the size of the current terminal
to send to Urbit on startup. We then subscribe to terminal size
change notifications and send those to your Urbit via the terminal
muxing system.

In the case where there are multiple terminal connections to your
Urbit, set the terminal size to the minimum of the widths.
2020-09-25 12:40:24 -04:00

43 lines
974 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
ts <- termSize
cb ts
pure ts