Input: add a new field, restoreInputState

This change adds a new record field to the Input type to allow the end
user to have direct access to the logic needed to restore the terminal's
input state flags. Prior to this change, this state restoration logic
could only be invoked as part of calling 'shutdownInput', but since that
function does other things (like killing threads) it is not advisable to
call it repeatedly (which is necessary in the use case this change is
intended to support).

This change supports a use case where an application initializes
Vty repeatedly, with changes to the input state flags in between
initializations, but where the application wants to ensure that the
*final* input state flag configuration matches the terminal's condition
before Vty started handling input for the first time. This change allows
applications to access the state restoration for the initial Vty handle
by exposing the logic directly in a field of Input.

The input state restoration is still an implicit part of invoking
'shutdownInput' (and thus 'shutdown') so no user-facing changes are
required for applications that do not build their own Input structures.
This commit is contained in:
Jonathan Daugherty 2020-07-09 15:01:45 -07:00
parent e49ea78472
commit 812e873499
2 changed files with 13 additions and 3 deletions

View File

@ -163,11 +163,15 @@ inputForConfig config@Config{ termName = Just termName
atomically $ writeTChan (input^.eventChannel) (EvResize e e) atomically $ writeTChan (input^.eventChannel) (EvResize e e)
_ <- installHandler windowChange pokeIO Nothing _ <- installHandler windowChange pokeIO Nothing
_ <- installHandler continueProcess pokeIO Nothing _ <- installHandler continueProcess pokeIO Nothing
let restore = unsetAttrs
return $ input return $ input
{ shutdownInput = do { shutdownInput = do
shutdownInput input shutdownInput input
_ <- installHandler windowChange Ignore Nothing _ <- installHandler windowChange Ignore Nothing
_ <- installHandler continueProcess Ignore Nothing _ <- installHandler continueProcess Ignore Nothing
unsetAttrs restore
, restoreInputState = restoreInputState input >> restore
} }
inputForConfig config = (<> config) <$> standardIOConfig >>= inputForConfig inputForConfig config = (<> config) <$> standardIOConfig >>= inputForConfig

View File

@ -52,9 +52,14 @@ data Input = Input
-- 'nextEvent' this will not refresh the display if the next event -- 'nextEvent' this will not refresh the display if the next event
-- is an 'EvResize'. -- is an 'EvResize'.
_eventChannel :: TChan Event _eventChannel :: TChan Event
-- | Shuts down the input processing. This should return the -- | Shuts down the input processing. As part of shutting down the
-- terminal input state to before he input initialized. -- input, this should also restore the input state.
, shutdownInput :: IO () , shutdownInput :: IO ()
-- | Restore the terminal's input state to what it was prior
-- to configuring input for Vty. This should be done as part of
-- 'shutdownInput' but is exposed in case you need to access it
-- directly.
, restoreInputState :: IO ()
-- | Changes to this value are reflected after the next event. -- | Changes to this value are reflected after the next event.
, _configRef :: IORef Config , _configRef :: IORef Config
-- | input debug log -- | input debug log
@ -235,6 +240,7 @@ initInput config classifyTable = do
applyConfig fd config applyConfig fd config
stopSync <- newEmptyMVar stopSync <- newEmptyMVar
input <- Input <$> atomically newTChan input <- Input <$> atomically newTChan
<*> pure (return ())
<*> pure (return ()) <*> pure (return ())
<*> newIORef config <*> newIORef config
<*> maybe (return Nothing) <*> maybe (return Nothing)