Last active
April 15, 2016 14:20
-
-
Save geekhunger/9301757 to your computer and use it in GitHub Desktop.
Easy Backups in Codea.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--# 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