aboutsummaryrefslogtreecommitdiff
path: root/lua/readline.lua
blob: a75f4ff98692c4fbabcf48e60edda69d72752d82 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
local LN = require('linenoise')

local M = {}

local history_loaded = false
local history_file = os.getenv("HOME") .. "/.mal-history"

function M.readline(prompt)
    if not history_loaded then
        history_loaded = true
        for line in io.lines(history_file) do
            LN.historyadd(line)
        end
    end

    line = LN.linenoise(prompt)
    if line then
        LN.historyadd(line)
        local f = io.open(history_file, "a")
        f:write(line.."\n")
        f:close()
    end
    return line
end

return M