Skip to content

Instantly share code, notes, and snippets.

@geekhunger
Last active April 15, 2016 14:20
Show Gist options
  • Save geekhunger/9301757 to your computer and use it in GitHub Desktop.
Save geekhunger/9301757 to your computer and use it in GitHub Desktop.
Easy Backups in Codea.
--# Hook
local s_setup
debug.sethook(function(event)
if setup and setup ~= s_setup then
local o_setup = setup
setup = function()
debug.sethook()
if hook then hook() end
o_setup()
end
s_setup = setup
end
end, "r")
function string.insert(str, insert, pos)
pos = pos or #str+1
return str:sub(1, pos) ..
insert ..
str:sub(pos+1, #str)
end
local function delay(time, callback, loop)
tween(time, {}, {}, nil, function()
if loop ~= nil then
if type(loop) == "number" then loop = loop - 1 end
if loop == true or loop >= 1 then delay(time, callback, loop) end
end
callback()
end)
end
local function extract(str)
local list = {}
for num in tostring(str):gmatch("[^,]+") do
table.insert(list, num)
end
return list
end
local function sourceToTabs(str)
local tabs, name = {}
str:gsub("[^\n]*\n", function(row)
if row:find("^%-%-#") then
name = row:sub(5, #row-1)
tabs[name] = ''
else
tabs[name] = tabs[name]..row
end
end)
return tabs
end
function hook()
local BackupKeys = extract(readLocalData("BackupKeys", ""))
local LatestBackup = readLocalData("LatestBackup", #BackupKeys)
local Selected
local autorun = false
-- CLI
backup = {
save = function()
local key, value = os.date("%d-%m-%Y %H:%M:%S"), ""
for _, name in pairs(listProjectTabs()) do
value = value .. "--# " .. name .. "\n" .. readProjectTab(name) .. "\n"
end
table.insert(BackupKeys, key)
LatestBackup = #BackupKeys
saveLocalData(key, value)
saveLocalData("BackupKeys", table.concat(BackupKeys, ","))
saveLocalData("LatestBackup", LatestBackup)
print("๐Ÿ†• Backup")
end,
load = function(n)
if not n or n <= 0 or n > #BackupKeys then
print("โ˜๏ธ No valid Backup number specified! Use backup.list() to see available Backups.")
return
end
-- Recreate tabs from Backup
local tabs = sourceToTabs(readLocalData(BackupKeys[n]))
-- Delete current tabs !!!
for num, name in ipairs(listProjectTabs()) do
if name ~= "Main" then saveProjectTab(name, nil) end
end
-- Restore Backup !!!
for name, content in pairs(tabs) do
saveProjectTab(name, content:sub(1, #content-1))
end
LatestBackup = n
saveLocalData("LatestBackup", LatestBackup)
print("๐Ÿ‘ Backup #"..n.." restored!")
end,
copy = function(n)
if not n or n <= 0 or n > #BackupKeys then
print("โ˜๏ธ No valid Backup number specified! Use backup.list() to see available Backups.")
return
end
pasteboard.copy(readLocalData(BackupKeys[n]))
print("โœ‚๏ธ Copied source to pasteboard.")
end,
delete = function(n)
if not n then
for key in ipairs(BackupKeys) do
saveLocalData(key, nil)
end
BackupKeys = {}
LatestBackup = 0
saveLocalData("BackupKeys", nil)
saveLocalData("LatestBackup", nil)
print("๐Ÿšฎ All Backups deleted!")
else
saveLocalData(BackupKeys[n], nil)
table.remove(BackupKeys, n)
if n <= LatestBackup then
LatestBackup = LatestBackup - 1
end
saveLocalData("BackupKeys", table.concat(BackupKeys, ","))
saveLocalData("LatestBackup", LatestBackup)
print("โŒ Backup #"..n)
if #BackupKeys > 0 then
parameter.action("โŒ All Backups", function() backup.delete() end)
end
end
delay(2, function() restart() end)
end,
list = function()
if #BackupKeys > 0 then
local list = "#: Backup"
for n, key in pairs(BackupKeys) do
list = list .. "\n" .. n .. ": " .. key .. (n == LatestBackup and " ๐Ÿ‘ˆ" or "")
end
print(list)
else
print("0 Backups")
end
end,
gist = function(n)
if not n then
print("โ˜๏ธ No valid Backup number specified! Use backup.list() to see available Backups.")
return
end
-- Create template for gist
local tabs = sourceToTabs(readLocalData(BackupKeys[n]))
local template = '{"public": true, "files": {'
for name, content in pairs(tabs) do
template = template .. '"'..name..'.lua": {"content": "'..content..'"},'
end
template = template:sub(1, #template-1)..'}}'
print(template:gsub([["*]], [[\"]]))
---[[
-- HTTP Request
local function onFailure(error)
print(error)
end
local function onSuccess(data, status, header)
if not data then
onFailure("Request failed: status "..status)
else
print(data)
end
end
http.request("https://api.github.com/gists", onSuccess, onFailure, {
useragent = "",
method = "POST",
data = template
})
--]]
end,
help = function()
print([[Backup slider usage.
Save/Store a Backup by moving the slider to the most right position.
Delete the current Backup by sliding immediately to 0. To delete a specific Backup, dile the desired number and slide then to 0.
Load/Restore by sliding to the desired Backup number. You can see all available Backups by calling backup.list()]])
print([[CLI commands. Replace # with a Backup number.
backup.save()
backup.load(#)
backup.copy(#)
backup.delete()
backup.delete(#)
backup.list()
backup.help()]])
end,
}
-- GUI
local function onDial()
tween.resetAll()
if not autorun then -- prevent callback autorun!
autorun = true
return
end
delay(1, function()
if Backups > 0 and Backups < (#BackupKeys + 1) then
Selected = Backups
print("๐Ÿ“Œ Backup #"..Selected.." selected.")
end
if Backups > #BackupKeys then
backup.save()
else
if Backups == 0 then
backup.delete(Selected or LatestBackup)
else
delay(1, function() backup.load(Selected or LatestBackup) end)
end
end
end)
end
parameter.integer("Backups", 0, #BackupKeys + 1, LatestBackup, onDial)
print("โ• You may use backup.help()")
end
--# Main
function setup()
print("hook() was injected into setup()โ€ฆ")
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment