2014-12-24 08:37:43 +03:00
|
|
|
module Readline
|
|
|
|
( readline, load_history )
|
|
|
|
where
|
|
|
|
|
|
|
|
-- Pick one of these:
|
|
|
|
-- GPL license
|
|
|
|
import qualified System.Console.Readline as RL
|
|
|
|
-- BSD license
|
|
|
|
--import qualified System.Console.Editline.Readline as RL
|
|
|
|
|
2015-03-12 06:22:35 +03:00
|
|
|
import Control.Monad (when)
|
|
|
|
import System.Directory (getHomeDirectory, doesFileExist)
|
2014-12-24 08:37:43 +03:00
|
|
|
|
2014-12-25 09:17:38 +03:00
|
|
|
import System.IO (hGetLine, hFlush, hIsEOF, stdin, stdout)
|
2015-03-12 06:22:35 +03:00
|
|
|
import System.IO.Error (tryIOError)
|
2014-12-25 09:17:38 +03:00
|
|
|
|
2014-12-24 08:37:43 +03:00
|
|
|
history_file = do
|
|
|
|
home <- getHomeDirectory
|
|
|
|
return $ home ++ "/.mal-history"
|
|
|
|
|
|
|
|
load_history = do
|
|
|
|
hfile <- history_file
|
2015-03-12 06:22:35 +03:00
|
|
|
fileExists <- doesFileExist hfile
|
|
|
|
when fileExists $ do
|
|
|
|
content <- readFile hfile
|
|
|
|
mapM RL.addHistory (lines content)
|
|
|
|
return ()
|
|
|
|
return ()
|
2014-12-24 08:37:43 +03:00
|
|
|
|
|
|
|
readline prompt = do
|
|
|
|
hfile <- history_file
|
|
|
|
maybeLine <- RL.readline prompt
|
|
|
|
case maybeLine of
|
|
|
|
Just line -> do
|
|
|
|
RL.addHistory line
|
2015-03-12 06:22:35 +03:00
|
|
|
res <- tryIOError (appendFile hfile (line ++ "\n"))
|
2014-12-24 08:37:43 +03:00
|
|
|
return maybeLine
|
|
|
|
_ -> return maybeLine
|