1
0
mirror of https://github.com/kepler155c/opus synced 2024-11-14 12:44:50 +00:00
opus/sys/apis/history.lua
2018-01-24 17:39:38 -05:00

51 lines
939 B
Lua

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