Cac*_*tus 6 terminal haskell resource-cleanup
使用的terminal包,我的代码基本上是这样的:
main :: IO ()
main = withTerminal $ runTerminalT $ do
eraseInDisplay EraseAll
hideCursor
setAutoWrap False
forever $ do
calculate
updateScreen
Run Code Online (Sandbox Code Playgroud)
如果用户使用 退出该程序Ctrl-C,则终端设置(在这种具体情况下,隐藏光标)将保留。我正在寻找一种轻量级的方法来恢复运行程序之前的终端设置。通过轻量级,我的意思是我可以环绕整个runTerminalTshebang,而不必手动调用checkInterrupt代码的各个部分等。
我还没有进行测试来确定这是否有效,但我想您只需安装一个信号处理程序来进行清理,然后像默认处理程序一样退出。
import System.Exit
import System.Posix.Signals
main = do
installHandler sigINT Nothing . Catch $ do
resetTerminalOrWhatever
exitWith (ExitFailure 1)
withTerminal $ {- ... -}
Run Code Online (Sandbox Code Playgroud)
信号模块可从unix包装中获取。
如果您很偏执,您可以检查旧处理程序是什么并尝试将其合并到您的处理程序中:
main = do
mfix $ \oldHandler ->
installHandler sigINT Nothing . CatchInfo $ \si -> case oldHandler of
Default -> reset >> exit
Ignore -> reset >> exit
Catch act -> reset >> act
CatchOnce act -> reset >> act >> exit
CatchInfo f -> reset >> f si
CatchInfoOnce f -> reset >> f si >> exit
withTerminal $ {- ... -}
Run Code Online (Sandbox Code Playgroud)
...或类似的东西。