1
0
mirror of https://github.com/kepler155c/opus synced 2024-11-17 06:04:50 +00:00
opus/sys/apis/history.lua

48 lines
883 B
Lua
Raw Normal View History

2016-12-11 19:24:52 +00:00
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