From 1cf687103f1edb6ff53f9807e6d7b2bf12c09d1c Mon Sep 17 00:00:00 2001 From: qvalentin Date: Sun, 27 Jun 2021 12:03:52 +0200 Subject: [PATCH] handle exit cond, wip --- app/Main.hs | 20 +++++++++++++++++--- wnpClient.cabal | 1 + 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/app/Main.hs b/app/Main.hs index da1ffc2..e4b77a8 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -9,17 +9,25 @@ import Network.HTTP.Client import Network.HTTP.Simple import Network.URI import System.Directory +import System.Exit import System.FilePath import System.IO +import System.Signal main :: IO () main = do setLocaleEncoding utf8 createDirectoryIfMissing False =<< workingDir join $ (writeFile <$> fileName) <*> pure "" + installHandler sigINT sigIntHandler + installHandler sigTERM sigIntHandler forever $ do - line <- getLine - dispatch line + end <- isEOF + if end + then sigIntHandler sigTERM + else do + line <- getLine + dispatch line dispatch :: String -> IO () dispatch ('T' : 'I' : 'T' : 'L' : 'E' : ':' : title) = writeToFile title 0 @@ -64,4 +72,10 @@ coverName :: IO FilePath coverName = ( "cover.jpg") <$> workingDir workingDir :: IO FilePath -workingDir = ( ".wnpClient") <$> getHomeDirectory \ No newline at end of file +workingDir = ( ".wnpClient") <$> getHomeDirectory + +sigIntHandler :: System.Signal.Handler +sigIntHandler _ = do + join $ (writeFile <$> fileName) <*> pure "" + hPutStrLn stderr ("Handle exit") + exitSuccess \ No newline at end of file diff --git a/wnpClient.cabal b/wnpClient.cabal index b84a22b..fa0e565 100644 --- a/wnpClient.cabal +++ b/wnpClient.cabal @@ -52,6 +52,7 @@ executable wnpClient , http-conduit , bytestring , filepath + , signal default-language: Haskell2010 test-suite wnpClient-test