Commit | Line | Data |
---|---|---|
9d42904e JM |
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 | ||
3e0b36dc JM |
8 | M.raw = false |
9 | ||
9d42904e JM |
10 | function M.readline(prompt) |
11 | if not history_loaded then | |
12 | history_loaded = true | |
c9de2e82 JM |
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) | |
9d42904e JM |
20 | end |
21 | ||
3e0b36dc JM |
22 | if M.raw then |
23 | io.write(prompt); io.flush(); | |
24 | line = io.read() | |
25 | else | |
26 | line = LN.linenoise(prompt) | |
27 | end | |
9d42904e JM |
28 | if line then |
29 | LN.historyadd(line) | |
c9de2e82 JM |
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) | |
9d42904e JM |
37 | end |
38 | return line | |
39 | end | |
40 | ||
41 | return M |