diff --git a/.gitignore b/.gitignore index e660fd9..849ddff 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1 @@ -bin/ +dist/ diff --git a/bc.cabal b/bc.cabal index f22b9d6..d20cfb6 100644 --- a/bc.cabal +++ b/bc.cabal @@ -6,7 +6,6 @@ license: MIT license-file: LICENSE author: Veit Heller maintainer: veit@veitheller.de -copyright: category: Language build-type: Simple extra-source-files: ChangeLog.md, README.md @@ -14,7 +13,7 @@ cabal-version: >=1.10 executable bc main-is: Main.hs - other-modules: - build-depends: base >=4.9 && <4.10 + hs-source-dirs: src/ + build-depends: base >=4.9 && <4.10, haskeline, unix hs-source-dirs: src default-language: Haskell2010 diff --git a/src/BC/Config.hs b/src/BC/Config.hs index e3524f6..af64943 100644 --- a/src/BC/Config.hs +++ b/src/BC/Config.hs @@ -1,5 +1,7 @@ -module Bc.Prompt where +module BC.Config where versionStr = "0.1.0.0" -prompt = "> " +promptStr = "> " + +returnStr = "=> " diff --git a/src/BC/Prompt.hs b/src/BC/Prompt.hs index 7d3af51..fd9ea51 100644 --- a/src/BC/Prompt.hs +++ b/src/BC/Prompt.hs @@ -1,24 +1,45 @@ module BC.Prompt (startPrompt) where +import System.Console.Haskeline +import System.Posix.Signals + import BC.Config printHeader :: IO () printHeader = do - putStrLn "bc (better calculator) version " ++ versionStr + putStrLn $ "bc (better calculator) version " ++ versionStr putStrLn "Copyright 2017 Veit Heller" putStrLn "This is free software with ABSOLUTELY NO WARRANTY.\n" +output :: [Char] -> InputT IO () +output out = outputStrLn $ returnStr ++ out + + prompt :: IO () -prompt = runInputT settings $ poll prompt +prompt = runInputT defaultSettings $ poll promptStr where poll p = do - input <- getInputLine p - case input of - Nothing -> return "" - Just str -> return str + input <- getInputLine p + case input of + Nothing -> do + output "Bye!" + return () + Just "quit" -> do + output "Bye!" + return () + Just str -> do + output str + poll p + + +installHandlers :: IO () +installHandlers = do + installHandler keyboardSignal (Catch (putStrLn "\n(interrupt) type quit to exit")) Nothing + return () startPrompt :: IO () startPrompt = do printHeader + installHandlers prompt