Commit | Line | Data |
---|---|---|
fa9a9758 JM |
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 | ||
c9de2e82 JM |
11 | import Control.Monad (when) |
12 | import System.Directory (getHomeDirectory, doesFileExist) | |
fa9a9758 | 13 | |
5400d4bf | 14 | import System.IO (hGetLine, hFlush, hIsEOF, stdin, stdout) |
c9de2e82 | 15 | import System.IO.Error (tryIOError) |
5400d4bf | 16 | |
fa9a9758 JM |
17 | history_file = do |
18 | home <- getHomeDirectory | |
19 | return $ home ++ "/.mal-history" | |
20 | ||
21 | load_history = do | |
22 | hfile <- history_file | |
c9de2e82 JM |
23 | fileExists <- doesFileExist hfile |
24 | when fileExists $ do | |
25 | content <- readFile hfile | |
26 | mapM RL.addHistory (lines content) | |
27 | return () | |
28 | return () | |
fa9a9758 JM |
29 | |
30 | readline prompt = do | |
31 | hfile <- history_file | |
32 | maybeLine <- RL.readline prompt | |
33 | case maybeLine of | |
34 | Just line -> do | |
fa9a9758 | 35 | RL.addHistory line |
c9de2e82 | 36 | res <- tryIOError (appendFile hfile (line ++ "\n")) |
fa9a9758 JM |
37 | return maybeLine |
38 | _ -> return maybeLine |