DISABLE FDs (REMOVE ME).
[jackhill/mal.git] / haskell / Readline.hs
1 module Readline
2 ( addHistory, 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 import System.IO.Error (tryIOError)
14
15 history_file :: IO String
16 history_file = do
17 home <- getHomeDirectory
18 return $ home ++ "/.mal-history"
19
20 load_history :: IO ()
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
28 readline :: String -> IO (Maybe String)
29 readline = RL.readline
30
31 addHistory :: String -> IO ()
32 addHistory line = do
33 hfile <- history_file
34 _ <- tryIOError (appendFile hfile (line ++ "\n"))
35 RL.addHistory line