mirror of
https://github.com/kepler155c/opus
synced 2025-12-19 22:58:06 +00:00
Initial commit
This commit is contained in:
47
sys/apis/history.lua
Normal file
47
sys/apis/history.lua
Normal file
@@ -0,0 +1,47 @@
|
||||
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
|
||||
Reference in New Issue
Block a user