Ada: merge to latest baseline
[jackhill/mal.git] / haskell / Readline.hs
1 module Readline
2 ( readline, load_history )
3 where
4
5 -- Pick one of these:
6 -- GPL license
7 import qualified System.Console.Readline as RL
8 -- BSD license
9 --import qualified System.Console.Editline.Readline as RL
10
11 import Control.Monad (when)
12 import System.Directory (getHomeDirectory, doesFileExist)
13
14 import System.IO (hGetLine, hFlush, hIsEOF, stdin, stdout)
15 import System.IO.Error (tryIOError)
16
17 history_file = do
18 home <- getHomeDirectory
19 return $ home ++ "/.mal-history"
20
21 load_history = do
22 hfile <- history_file
23 fileExists <- doesFileExist hfile
24 when fileExists $ do
25 content <- readFile hfile
26 mapM RL.addHistory (lines content)
27 return ()
28 return ()
29
30 readline prompt = do
31 hfile <- history_file
32 maybeLine <- RL.readline prompt
33 case maybeLine of
34 Just line -> do
35 RL.addHistory line
36 res <- tryIOError (appendFile hfile (line ++ "\n"))
37 return maybeLine
38 _ -> return maybeLine