1
0
mirror of https://github.com/kepler155c/opus synced 2024-11-14 20:54:50 +00:00
opus/sys/apis/history.lua

51 lines
983 B
Lua
Raw Normal View History

2016-12-11 19:24:52 +00:00
local Util = require('util')
2017-10-03 04:50:54 +00:00
local History = { }
local History_mt = { __index = History }
2016-12-11 19:24:52 +00:00
function History.load(filename, limit)
2017-10-03 04:50:54 +00:00
local self = setmetatable({
limit = limit,
filename = filename,
}, History_mt)
2016-12-11 19:24:52 +00:00
2017-10-03 04:50:54 +00:00
self.entries = Util.readLines(filename) or { }
self.pos = #self.entries + 1
2016-12-11 19:24:52 +00:00
2017-10-03 04:50:54 +00:00
return self
end
2016-12-11 19:24:52 +00:00
2017-10-03 04:50:54 +00:00
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)
2016-12-11 19:24:52 +00:00
end
2017-10-03 04:50:54 +00:00
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
2016-12-11 19:24:52 +00:00
end
return History