1
0
mirror of https://github.com/kepler155c/opus synced 2024-11-05 00:16:16 +00:00
opus/sys/apis/history.lua
kepler155c@gmail.com fc243a9c12 Initial commit
2016-12-11 14:24:52 -05:00

48 lines
883 B
Lua

local Util = require('util')
local History = { }
function History.load(filename, limit)
local entries = Util.readLines(filename) or { }
local pos = #entries + 1
return {
entries = entries,
add = function(line)
local last = entries[pos] or entries[pos - 1]
if not last or line ~= last then
table.insert(entries, line)
if limit then
while #entries > limit do
table.remove(entries, 1)
end
end
Util.writeLines(filename, entries)
pos = #entries + 1
end
end,
setPosition = function(p)
pos = p
end,
back = function()
if pos > 1 then
pos = pos - 1
return entries[pos]
end
end,
forward = function()
if pos <= #entries then
pos = pos + 1
return entries[pos]
end
end,
}
end
return History