urbit-king: Library for getting current terminal size (SIGWINCH).

This commit is contained in:
Benjamin Summers 2020-03-23 13:15:13 -07:00
parent 7415a2f66a
commit a52aca28a6
7 changed files with 105 additions and 1 deletions

View File

@ -7,6 +7,7 @@ packages:
- urbit-atom
- urbit-azimuth
- urbit-king
- urbit-termsize
extra-deps:
- flat-0.3.4@sha256:002a0e0ae656ea8cc02a772d0bcb6ea7dbd7f2e79070959cc748ad1e7138eb38

View File

@ -505,7 +505,7 @@ term (tsize, Client{..}) shutdownSTM king enqueueEv =
where
T.TSize wi hi = tsize
initialEvents = [(initialBlew wi hi), initialHail]
initialEvents = [initialBlew wi hi, initialHail]
runTerm :: RAcquire e (EffCb e TermEf)
runTerm = do

3
pkg/hs/urbit-termsize/.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
.stack-work/
urbit-termsize.cabal
*~

View File

@ -0,0 +1,21 @@
The MIT License (MIT)
Copyright (c) 2016 urbit
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

View File

@ -0,0 +1,13 @@
module Main where
import Prelude
import Urbit.TermSize (liveTermSize)
import System.IO (getLine)
main :: IO ()
main = do
init <- liveTermSize (putStrLn . ("New Size: " <>) . show)
putStrLn ("Initial Size: " <> show init)
_ <- getLine
pure ()

View File

@ -0,0 +1,40 @@
{-# 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

View File

@ -0,0 +1,26 @@
name: urbit-termsize
version: 0.1.0
license: MIT
license-file: LICENSE
dependencies:
- base
- terminal-size
- unix
ghc-options:
- -fwarn-incomplete-patterns
- -fwarn-unused-binds
- -fwarn-unused-imports
- -O2
library:
source-dirs: lib
executables:
live-termsize:
main: Main.hs
source-dirs: app
dependencies:
- urbit-termsize
ghc-options: "-threaded -rtsopts -with-rtsopts=-N"