prompt: fixed history file handling
This commit is contained in:
2
bc.cabal
2
bc.cabal
@@ -14,6 +14,6 @@ cabal-version: >=1.10
|
|||||||
executable bc
|
executable bc
|
||||||
main-is: Main.hs
|
main-is: Main.hs
|
||||||
hs-source-dirs: src/
|
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
|
hs-source-dirs: src
|
||||||
default-language: Haskell2010
|
default-language: Haskell2010
|
||||||
|
@@ -7,3 +7,5 @@ promptStr = "> "
|
|||||||
returnStr = "=> "
|
returnStr = "=> "
|
||||||
|
|
||||||
historyFile = ".bc_history"
|
historyFile = ".bc_history"
|
||||||
|
|
||||||
|
historyLen = 1000::Int
|
||||||
|
@@ -7,6 +7,8 @@ import System.Directory (doesFileExist, getHomeDirectory)
|
|||||||
import System.IO
|
import System.IO
|
||||||
import System.Posix.Signals
|
import System.Posix.Signals
|
||||||
|
|
||||||
|
import qualified System.IO.Strict as S
|
||||||
|
|
||||||
import BC.Config
|
import BC.Config
|
||||||
import BC.Eval
|
import BC.Eval
|
||||||
import BC.Parse
|
import BC.Parse
|
||||||
@@ -24,19 +26,16 @@ newPrompt = do
|
|||||||
exists <- doesFileExist f
|
exists <- doesFileExist f
|
||||||
if exists
|
if exists
|
||||||
then do
|
then do
|
||||||
contents <- readFile f
|
contents <- S.readFile f
|
||||||
return $ PState (-1) (split contents)
|
let s = lines contents
|
||||||
|
length s `seq` return $ PState (-1) s
|
||||||
else return $ PState (-1) []
|
else return $ PState (-1) []
|
||||||
where split [] = []
|
|
||||||
split s = takeWhile (/= '\n') s : split (dropWhile (/= '\n') s)
|
|
||||||
|
|
||||||
|
|
||||||
savePrompt :: Prompt -> IO ()
|
savePrompt :: Prompt -> IO ()
|
||||||
savePrompt (PState _ strs) = do
|
savePrompt (PState _ strs) = do
|
||||||
home <- getHomeDirectory
|
home <- getHomeDirectory
|
||||||
f <- openFile (home ++ "/" ++ historyFile) WriteMode
|
writeFile (home ++ "/" ++ historyFile) (intercalate "\n" $ reverse strs)
|
||||||
hPrint f (intercalate "\n" strs)
|
|
||||||
hClose f
|
|
||||||
|
|
||||||
|
|
||||||
printHeader :: IO ()
|
printHeader :: IO ()
|
||||||
@@ -126,7 +125,8 @@ readline state = read' "" 0
|
|||||||
c <- getChar
|
c <- getChar
|
||||||
case c of
|
case c of
|
||||||
'\EOT' -> return Nothing
|
'\EOT' -> return Nothing
|
||||||
'\n' -> return (Just acc)
|
'\n' -> do putStrLn ""
|
||||||
|
return (Just acc)
|
||||||
'\DEL' ->
|
'\DEL' ->
|
||||||
if null acc || pos == 0
|
if null acc || pos == 0
|
||||||
then read' acc pos pstate
|
then read' acc pos pstate
|
||||||
@@ -152,14 +152,9 @@ prompt :: State -> Prompt -> IO Prompt
|
|||||||
prompt state pstate@(PState _ history) = do
|
prompt state pstate@(PState _ history) = do
|
||||||
input <- readline state pstate
|
input <- readline state pstate
|
||||||
case input of
|
case input of
|
||||||
Nothing -> do
|
Nothing -> return pstate
|
||||||
putStrLn "\nBye!"
|
Just "quit" -> return pstate
|
||||||
return pstate
|
|
||||||
Just "quit" -> do
|
|
||||||
putStrLn "\nBye!"
|
|
||||||
return pstate
|
|
||||||
Just str -> do
|
Just str -> do
|
||||||
putStrLn ""
|
|
||||||
newstate <- output state str
|
newstate <- output state str
|
||||||
let newpstate = PState (-1) (str:history)
|
let newpstate = PState (-1) (str:history)
|
||||||
prompt newstate newpstate
|
prompt newstate newpstate
|
||||||
@@ -180,3 +175,4 @@ startPrompt = do
|
|||||||
p <- newPrompt
|
p <- newPrompt
|
||||||
state <- prompt newState p
|
state <- prompt newState p
|
||||||
savePrompt state
|
savePrompt state
|
||||||
|
putStrLn "Bye!"
|
||||||
|
Reference in New Issue
Block a user