Skip to content

Commit 22e65f0

Browse files
Hopeful fix for spinning on terminal crash.
1 parent b0c97c8 commit 22e65f0

5 files changed

Lines changed: 23 additions & 9 deletions

File tree

Core.hs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -271,10 +271,8 @@ mpgInput field = runForever $ do
271271
run :: IO ()
272272
run = runForever $ sequence_ . keymap =<< getKeys
273273
where
274-
getKeys = unsafeInterleaveIO $ do
275-
c <- UI.getKey
276-
cs <- getKeys
277-
pure (c:cs) -- A lazy list of curses keys
274+
-- A lazy list of curses keys
275+
getKeys = unsafeInterleaveIO $ (:) <$> UI.getKey <*> getKeys
278276

279277
------------------------------------------------------------------------
280278

Keymap.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
module Keymap where
3434

3535
import Prelude ()
36-
import Base hiding (all, delete)
36+
import Base hiding (all, delete, (!?))
3737

3838
import Core
3939
import Config (package)

UI.hs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ import System.Posix.Signals (raiseSignal, sigTSTP, installHandler, Handler(.
5959

6060
import Foreign.C.String
6161
import Foreign.C.Types
62+
import Foreign.C.Error (Errno(..), getErrno)
6263

6364
import qualified Data.ByteString.Char8 as P
6465
import qualified Data.ByteString as B
@@ -132,20 +133,35 @@ suspend = raiseSignal sigTSTP
132133
screenSize :: IO (Int, Int)
133134
screenSize = Curses.scrSize
134135

136+
--
137+
-- | Rewrite of Curses.getCh to avoid looping on terminal crash
138+
-- | (also no unget support since I don't need it)
139+
--
140+
getCh :: IO Curses.Key
141+
getCh = do
142+
threadWaitRead 0
143+
v <- Curses.getch
144+
case v of
145+
-1 -> do
146+
Errno e <- getErrno
147+
putStrLn $ "Error " ++ show e ++ "; terminal has gone away? Hard-exiting now."
148+
exitFailure
149+
k -> pure $ Curses.decodeKey k
150+
135151
--
136152
-- | Read a key. UIs need to define a method for getting events.
137153
-- We only need to refresh if we don't have no SIGWINCH support.
138154
--
139155
getKey :: IO Char
140156
getKey = do
141-
k <- Curses.getCh
157+
k <- getCh
142158
if k == Curses.KeyResize
143159
then do
144160
when (isNothing Curses.cursesSigWinch) do
145161
runDraw $ redraw <> resizeui
146162
getKey
147163
else pure $ unkey k
148-
164+
149165
-- | Resize the window
150166
-- From "Writing Programs with NCURSES", by Eric S. Raymond and Zeyd M. Ben-Halim
151167
--

hmp3-ng.cabal

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
cabal-version: 2.2
22

33
name: hmp3-ng
4-
version: 2.17.0
4+
version: 2.17.1
55
synopsis: A 2019 fork of an ncurses mp3 player written in Haskell
66
description: An mp3 player with a curses frontend. Playlists are populated by
77
passing file and directory names on the command line. 'h' displays

stack.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
resolver: lts-23.9
1+
resolver: lts-23.28
22
system-ghc: true
33
compiler-check: newer-minor
44

0 commit comments

Comments
 (0)