Merge pull request #217 from dubek/lua-interop
[jackhill/mal.git] / lua / readline.lua
1 local LN = require('linenoise')
2
3 local M = {}
4
5 local history_loaded = false
6 local history_file = os.getenv("HOME") .. "/.mal-history"
7
8 M.raw = false
9
10 function M.readline(prompt)
11 if not history_loaded then
12 history_loaded = true
13 xpcall(function()
14 for line in io.lines(history_file) do
15 LN.historyadd(line)
16 end
17 end, function(exc)
18 return true -- ignore the error
19 end)
20 end
21
22 if M.raw then
23 io.write(prompt); io.flush();
24 line = io.read()
25 else
26 line = LN.linenoise(prompt)
27 end
28 if line then
29 LN.historyadd(line)
30 xpcall(function()
31 local f = io.open(history_file, "a")
32 f:write(line.."\n")
33 f:close()
34 end, function(exc)
35 return true -- ignore the error
36 end)
37 end
38 return line
39 end
40
41 return M