Skip to content

Commit cf8bf0e

Browse files
Control-C to kill decoder.
1 parent cb49b3c commit cf8bf0e

2 files changed

Lines changed: 19 additions & 7 deletions

File tree

Keymap.hs

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,17 @@ import Base
1717
import Core
1818
import Elements (package)
1919
import Keyboard (unkey, charToKey, Key(..), historyKeys)
20-
import State (getsHS, modifyHS_, KeysHelp, Modal(..), HState(..), SearchType(..))
20+
import State (getsHS, modifyHS_, KeysHelp, Modal(..), HState(..), SearchType(..), mpgRef, Mpg(..))
2121
import Style (plainSeg)
2222
import Text (dropLastUTF8)
2323
import UI qualified (getKey, resetui)
2424

25+
import Control.Monad.Trans.Maybe
2526
import Data.ByteString.Char8 qualified as P
2627
import Data.ByteString.UTF8 qualified as UTF8
2728
import Data.Map.Strict qualified as M
29+
import System.Process (getPid)
30+
import System.Posix.Signals (signalProcess, sigINT)
2831

2932

3033
------------------------------------------------------------------------
@@ -61,18 +64,25 @@ mainMode = KeyMap \c -> getsHS (.modal) >>= \case
6164
toggleFocus
6265
hist <- getsHS (.searchHist)
6366
searchMode c $ Zipper "" hist []
64-
| c `elem` ['q', '\^C'] ->
65-
forcePause *> setsModal (const $ Just ExitModal) $> mainMode
66-
| c `elem` ['H', ';'] ->
67-
showHist $> mainMode
6867
| c >= '1' && c <= '9' ->
6968
jumpRel (fromIntegral (fromEnum c - 48) / 10) $> mainMode
7069
| True -> sequence_ (M.lookup c keyMap) $> mainMode
7170

7271

72+
-- Helpers
73+
7374
historyKeyMap :: M.Map Char Int
7475
historyKeyMap = M.fromList $ zip (toList historyKeys) [0..]
7576

77+
askExit :: IO ()
78+
askExit = setsModal $ const $ Just ExitModal
79+
80+
controlC :: IO ()
81+
controlC = do
82+
mpid <- runMaybeT do
83+
mpg <- MaybeT $ readIORef mpgRef
84+
MaybeT $ getPid mpg.mpgPH
85+
maybe askExit (signalProcess sigINT) mpid
7686

7787
------------------------------------------------------------------------
7888
-- Search mode
@@ -147,14 +157,14 @@ keyTable =
147157
, ("Repeat last regex search backwards", ['N'], repeatSearch False)
148158
, ("Mark for deletion in .hmp3-delete", ['D'], blacklist)
149159
, ("Restart song", [unkey KeyBackspace], seekStart)
150-
, ("Toggle the song history", ['H', ';'], placeholder)
160+
, ("Toggle the song history", ['H', ';'], showHist)
151161
, ("Search for file matching regex", ['/'], placeholder)
152162
, ("Search backwards for file", ['?'], placeholder)
153163
, ("Search for directory matching regex", ['\\'], placeholder)
154164
, ("Search backwards for directory", ['|'], placeholder)
155165
, ("Change size of folder and file columns", ['[', ']'], placeholder)
156166
, ("Load config file", ['l'], loadConfig)
157-
, ("Quit " <> UTF8.fromString package, ['q'], placeholder)
167+
, ("Quit " <> UTF8.fromString package, ['q'], forcePause *> askExit)
158168
]
159169
where placeholder = pure () -- handled separately
160170

@@ -167,6 +177,7 @@ quietKeys =
167177
, (unkey KeySRight, seek 60)
168178
, ('[', adjFolderCol (-1))
169179
, (']', adjFolderCol 1)
180+
, ('\^C', controlC)
170181
]
171182

172183
-- Compiled dispatch table for normal-mode single-key commands.

hmp3-ng.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ library
7979
process,
8080
random,
8181
regex-posix,
82+
transformers,
8283
unix >=2.8,
8384
utf8-string,
8485

0 commit comments

Comments
 (0)