on master help sign in
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.

commit812e8734999204a5b2b7308c2aaef77a49d5acb7 authorJonathan Daugherty <cygnus@foobox.com> dateThu 9 Jul 2020, 15:01 (6 years ago) parente49ea78
2 files changed+13−3
src/Graphics/Vty/Input.hs+5−1 src/Graphics/Vty/Input/Loop.hs+8−2
src/Graphics/Vty/Input.hs+5−1
@@ -163,11 +163,15 @@ inputForConfig config@Config{ termName = Just termName
163163 atomically $ writeTChan (input^.eventChannel) (EvResize e e)
164164 _ <- installHandler windowChange pokeIO Nothing
165165 _ <- installHandler continueProcess pokeIO Nothing
166+
167+ let restore = unsetAttrs
168+
166169 return $ input
167170 { shutdownInput = do
168171 shutdownInput input
169172 _ <- installHandler windowChange Ignore Nothing
170173 _ <- installHandler continueProcess Ignore Nothing
171− unsetAttrs
174+ restore
175+ , restoreInputState = restoreInputState input >> restore
172176 }
173177inputForConfig config = (<> config) <$> standardIOConfig >>= inputForConfig
src/Graphics/Vty/Input/Loop.hs+8−2
@@ -52,9 +52,14 @@ data Input = Input
5252 -- 'nextEvent' this will not refresh the display if the next event
5353 -- is an 'EvResize'.
5454 _eventChannel :: TChan Event
55− -- | Shuts down the input processing. This should return the
56− -- terminal input state to before he input initialized.
55+ -- | Shuts down the input processing. As part of shutting down the
56+ -- input, this should also restore the input state.
5757 , shutdownInput :: IO ()
58+ -- | Restore the terminal's input state to what it was prior
59+ -- to configuring input for Vty. This should be done as part of
60+ -- 'shutdownInput' but is exposed in case you need to access it
61+ -- directly.
62+ , restoreInputState :: IO ()
5863 -- | Changes to this value are reflected after the next event.
5964 , _configRef :: IORef Config
6065 -- | input debug log
@@ -235,6 +240,7 @@ initInput config classifyTable = do
235240 applyConfig fd config
236241 stopSync <- newEmptyMVar
237242 input <- Input <$> atomically newTChan
243+ <*> pure (return ())
238244 <*> pure (return ())
239245 <*> newIORef config
240246 <*> maybe (return Nothing)