mirror of
https://github.com/joshuaclayton/unused.git
synced 2024-09-11 04:45:28 +03:00
Hook into interrupt to trigger other behavior
Why? ==== Unused hides the cursor and potentially does other things to the window that may leave it in an odd state. This introduces a hook to run any state cleanup, including re-enabling the cursor, when a user sends a SIGINT to the program.
This commit is contained in:
parent
5952306873
commit
11d35a6263
@ -5,7 +5,7 @@ import System.IO (hSetBuffering, BufferMode(NoBuffering), stdout)
|
|||||||
import Unused.Parser (parseLines)
|
import Unused.Parser (parseLines)
|
||||||
import Unused.Types (ParseResponse, RemovalLikelihood(..))
|
import Unused.Types (ParseResponse, RemovalLikelihood(..))
|
||||||
import Unused.ResponseFilter (withOneOccurrence, withOneFile, withLikelihoods, ignoringPaths)
|
import Unused.ResponseFilter (withOneOccurrence, withOneFile, withLikelihoods, ignoringPaths)
|
||||||
import Unused.CLI (SearchRunner(..), executeSearch, printParseError, printSearchResults, resetScreen)
|
import Unused.CLI (SearchRunner(..), executeSearch, printParseError, printSearchResults, resetScreen, withInterruptHandler)
|
||||||
|
|
||||||
data Options = Options
|
data Options = Options
|
||||||
{ oSearchRunner :: SearchRunner
|
{ oSearchRunner :: SearchRunner
|
||||||
@ -15,8 +15,9 @@ data Options = Options
|
|||||||
}
|
}
|
||||||
|
|
||||||
main :: IO ()
|
main :: IO ()
|
||||||
main = run =<< execParser
|
main = withInterruptHandler $
|
||||||
(parseOptions `withInfo` "Analyze potentially unused code")
|
run =<< execParser
|
||||||
|
(parseOptions `withInfo` "Analyze potentially unused code")
|
||||||
|
|
||||||
run :: Options -> IO ()
|
run :: Options -> IO ()
|
||||||
run options = do
|
run options = do
|
||||||
|
@ -1,11 +1,31 @@
|
|||||||
module Unused.CLI.Util
|
module Unused.CLI.Util
|
||||||
( resetScreen
|
( resetScreen
|
||||||
|
, withInterruptHandler
|
||||||
, module System.Console.ANSI
|
, module System.Console.ANSI
|
||||||
) where
|
) where
|
||||||
|
|
||||||
import System.Console.ANSI
|
import System.Console.ANSI
|
||||||
|
import Control.Exception (throwTo)
|
||||||
|
import System.Posix.Signals (Handler(Catch), installHandler, keyboardSignal)
|
||||||
|
import Control.Concurrent (ThreadId, myThreadId)
|
||||||
|
import System.Exit (ExitCode(ExitFailure))
|
||||||
|
|
||||||
resetScreen :: IO ()
|
resetScreen :: IO ()
|
||||||
resetScreen = do
|
resetScreen = do
|
||||||
clearScreen
|
clearScreen
|
||||||
setCursorPosition 0 0
|
setCursorPosition 0 0
|
||||||
|
|
||||||
|
withInterruptHandler :: IO () -> IO ()
|
||||||
|
withInterruptHandler body = do
|
||||||
|
tid <- myThreadId
|
||||||
|
_ <- installHandler keyboardSignal (Catch (handleInterrupt tid)) Nothing
|
||||||
|
body
|
||||||
|
|
||||||
|
handleInterrupt :: ThreadId -> IO ()
|
||||||
|
handleInterrupt tid = do
|
||||||
|
resetScreen
|
||||||
|
showCursor
|
||||||
|
throwTo tid $ ExitFailure code
|
||||||
|
where
|
||||||
|
code = signalToInt $ 128 + keyboardSignal
|
||||||
|
signalToInt s = read $ show s :: Int
|
||||||
|
@ -40,6 +40,7 @@ library
|
|||||||
, regex-tdfa
|
, regex-tdfa
|
||||||
, terminal-progress-bar
|
, terminal-progress-bar
|
||||||
, ansi-terminal
|
, ansi-terminal
|
||||||
|
, unix
|
||||||
default-language: Haskell2010
|
default-language: Haskell2010
|
||||||
|
|
||||||
executable unused
|
executable unused
|
||||||
|
Loading…
Reference in New Issue
Block a user