alesandreo

lib/ale/turtle/history.lua

Aug 11th, 2021 (edited)
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.10 KB | None | 0 0
  1.  
  2. -- https://pastebin.com/TFLLgAK5
  3.  
  4. History = {
  5.   undo_log = {},
  6.   redo_log = {},
  7.   rewinding = false,
  8.   redoing = false
  9. }
  10.  
  11. History.__index = History
  12.  
  13. function History:new()
  14.   local o = {}
  15.   setmetatable(o, self)
  16.   return o
  17. end
  18.  
  19. function History:addToLog(action)
  20.   if self.rewinding then
  21.     return table.insert(self.redo_log, action)
  22.   end
  23.   return table.insert(self.undo_log, action)
  24. end
  25.  
  26. function History:clearRedoLog()
  27.   self.redo_log = {}
  28. end
  29.  
  30. function History:redo()
  31.   local action
  32.   action = table.remove(self.redo_log)
  33.   if not action then
  34.     error("Redo Log returned a nil action.")
  35.   end
  36.   return action
  37. end
  38.  
  39. function History:undo()
  40.   local action
  41.   action = table.remove(self.undo_log)
  42.   if not action then
  43.     error("Undo Log returned a nil action.")
  44.   end
  45.   return action
  46. end
  47.  
  48. function History:erase_undo(n)
  49.   n = n or 1
  50.   for i=1, n, 1 do
  51.     print("Erasing: "..table.remove(self.undo_log))
  52.   end
  53.   return true
  54. end
  55.  
  56. function History:erase_redo(n)
  57.   n = n or 1
  58.   for i=1, n, 1 do
  59.     print("Erasing: "..table.remove(self.redo_log))
  60.   end
  61.   return true
  62. end
Advertisement
Add Comment
Please, Sign In to add comment