mirror of
https://github.com/urbit/shrub.git
synced 2024-12-21 09:51:36 +03:00
20a6c0331c
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.
43 lines
974 B
Haskell
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
|