2020-01-23 07:16:09 +03:00
|
|
|
{-|
|
|
|
|
Interface Terminal API.
|
|
|
|
-}
|
2020-09-18 20:46:43 +03:00
|
|
|
module Urbit.Vere.Term.API (Ev(..),
|
|
|
|
Client(..),
|
2020-09-25 18:32:17 +03:00
|
|
|
ClientTake(..),
|
2020-09-18 20:46:43 +03:00
|
|
|
trace,
|
|
|
|
slog,
|
|
|
|
spin,
|
|
|
|
stopSpin) where
|
2019-09-18 08:22:19 +03:00
|
|
|
|
2020-01-24 08:28:38 +03:00
|
|
|
import Urbit.Prelude hiding (trace)
|
2019-09-18 08:22:19 +03:00
|
|
|
|
2020-01-24 08:28:38 +03:00
|
|
|
import Urbit.Arvo (Belt, Blit)
|
2019-09-18 08:22:19 +03:00
|
|
|
|
2020-10-27 15:22:33 +03:00
|
|
|
import Control.Monad.Fail (fail)
|
2020-09-25 18:32:17 +03:00
|
|
|
import Urbit.TermSize
|
2019-09-18 08:22:19 +03:00
|
|
|
|
|
|
|
-- External Types --------------------------------------------------------------
|
|
|
|
|
2020-01-23 07:16:09 +03:00
|
|
|
{-|
|
2019-09-18 08:22:19 +03:00
|
|
|
Input Event for terminal driver:
|
|
|
|
|
|
|
|
%blits -- list of blits from arvo.
|
|
|
|
%trace -- stderr line from runtime.
|
2020-09-18 20:46:43 +03:00
|
|
|
%slog -- nock worker logging with priority
|
2019-09-18 08:22:19 +03:00
|
|
|
%blank -- print a blank line
|
|
|
|
%spinr -- Start or stop the spinner
|
|
|
|
-}
|
2020-10-02 18:10:32 +03:00
|
|
|
data Ev = Blits ![Blit]
|
|
|
|
| Trace !Cord
|
2020-10-05 20:26:46 +03:00
|
|
|
| Slog !(Atom, Tank)
|
2019-09-18 08:22:19 +03:00
|
|
|
| Blank
|
2020-10-02 18:10:32 +03:00
|
|
|
| Spinr !(Maybe (Maybe Cord))
|
2019-09-18 09:58:42 +03:00
|
|
|
deriving (Show)
|
2019-09-18 08:22:19 +03:00
|
|
|
|
2020-09-25 18:32:17 +03:00
|
|
|
data ClientTake
|
|
|
|
= ClientTakeBelt Belt
|
|
|
|
| ClientTakeSize TermSize
|
|
|
|
deriving (Show)
|
|
|
|
|
|
|
|
instance ToNoun ClientTake where
|
|
|
|
toNoun = \case
|
|
|
|
ClientTakeBelt b -> toNoun $ (Cord "belt", b)
|
|
|
|
ClientTakeSize (TermSize w h) -> toNoun $ (Cord "size", (w, h))
|
|
|
|
|
|
|
|
instance FromNoun ClientTake where
|
|
|
|
parseNoun n = named "ClientTake" $ do
|
|
|
|
(Cord name, rest) <- parseNoun n
|
|
|
|
case name of
|
|
|
|
"belt" -> do
|
|
|
|
b <- parseNoun rest
|
|
|
|
pure (ClientTakeBelt b)
|
|
|
|
"size" -> do
|
|
|
|
(w, h) <- parseNoun rest
|
|
|
|
pure (ClientTakeSize (TermSize w h))
|
|
|
|
_ -> fail "weird client take"
|
|
|
|
|
|
|
|
|
2019-09-18 08:22:19 +03:00
|
|
|
data Client = Client
|
2020-09-25 18:32:17 +03:00
|
|
|
{ take :: STM (Maybe ClientTake)
|
2019-09-18 12:11:18 +03:00
|
|
|
, give :: [Ev] -> STM ()
|
2019-09-18 08:22:19 +03:00
|
|
|
}
|
|
|
|
|
2019-09-18 09:58:42 +03:00
|
|
|
deriveNoun ''Ev
|
|
|
|
|
2019-09-18 08:22:19 +03:00
|
|
|
|
|
|
|
-- Utilities -------------------------------------------------------------------
|
|
|
|
|
|
|
|
trace :: Client -> Text -> STM ()
|
2019-09-18 12:11:18 +03:00
|
|
|
trace ts = give ts . singleton . Trace . Cord
|
2019-09-18 08:22:19 +03:00
|
|
|
|
2020-09-18 20:46:43 +03:00
|
|
|
slog :: Client -> (Atom, Tank) -> STM ()
|
|
|
|
slog ts = give ts . singleton . Slog
|
|
|
|
|
2019-09-18 08:22:19 +03:00
|
|
|
spin :: Client -> Maybe Text -> STM ()
|
2019-09-18 12:11:18 +03:00
|
|
|
spin ts = give ts . singleton . Spinr . Just . fmap Cord
|
2019-09-18 08:22:19 +03:00
|
|
|
|
|
|
|
stopSpin :: Client -> STM ()
|
2019-09-18 12:11:18 +03:00
|
|
|
stopSpin ts = give ts [Spinr Nothing]
|