From b31965fe8a09e3d934bc1b3bc7e40fae9875f53b Mon Sep 17 00:00:00 2001 From: hellerve Date: Sat, 20 May 2017 10:56:19 -0400 Subject: [PATCH] prompt: fixed history file handling --- bc.cabal | 2 +- src/BC/Config.hs | 2 ++ src/BC/Prompt.hs | 26 +++++++++++--------------- 3 files changed, 14 insertions(+), 16 deletions(-) diff --git a/bc.cabal b/bc.cabal index 783ba33..b8b346b 100644 --- a/bc.cabal +++ b/bc.cabal @@ -14,6 +14,6 @@ cabal-version: >=1.10 executable bc main-is: Main.hs hs-source-dirs: src/ - build-depends: base >=4.9 && <4.10, directory, hashmap, numbers, parsec, unix + build-depends: base >=4.9 && <4.10, directory, hashmap, numbers, parsec, strict, unix hs-source-dirs: src default-language: Haskell2010 diff --git a/src/BC/Config.hs b/src/BC/Config.hs index 36d9086..ab2c05b 100644 --- a/src/BC/Config.hs +++ b/src/BC/Config.hs @@ -7,3 +7,5 @@ promptStr = "> " returnStr = "=> " historyFile = ".bc_history" + +historyLen = 1000::Int diff --git a/src/BC/Prompt.hs b/src/BC/Prompt.hs index 918208f..a43d80b 100644 --- a/src/BC/Prompt.hs +++ b/src/BC/Prompt.hs @@ -7,6 +7,8 @@ import System.Directory (doesFileExist, getHomeDirectory) import System.IO import System.Posix.Signals +import qualified System.IO.Strict as S + import BC.Config import BC.Eval import BC.Parse @@ -24,19 +26,16 @@ newPrompt = do exists <- doesFileExist f if exists then do - contents <- readFile f - return $ PState (-1) (split contents) + contents <- S.readFile f + let s = lines contents + length s `seq` return $ PState (-1) s else return $ PState (-1) [] - where split [] = [] - split s = takeWhile (/= '\n') s : split (dropWhile (/= '\n') s) savePrompt :: Prompt -> IO () savePrompt (PState _ strs) = do home <- getHomeDirectory - f <- openFile (home ++ "/" ++ historyFile) WriteMode - hPrint f (intercalate "\n" strs) - hClose f + writeFile (home ++ "/" ++ historyFile) (intercalate "\n" $ reverse strs) printHeader :: IO () @@ -126,7 +125,8 @@ readline state = read' "" 0 c <- getChar case c of '\EOT' -> return Nothing - '\n' -> return (Just acc) + '\n' -> do putStrLn "" + return (Just acc) '\DEL' -> if null acc || pos == 0 then read' acc pos pstate @@ -152,14 +152,9 @@ prompt :: State -> Prompt -> IO Prompt prompt state pstate@(PState _ history) = do input <- readline state pstate case input of - Nothing -> do - putStrLn "\nBye!" - return pstate - Just "quit" -> do - putStrLn "\nBye!" - return pstate + Nothing -> return pstate + Just "quit" -> return pstate Just str -> do - putStrLn "" newstate <- output state str let newpstate = PState (-1) (str:history) prompt newstate newpstate @@ -180,3 +175,4 @@ startPrompt = do p <- newPrompt state <- prompt newState p savePrompt state + putStrLn "Bye!"