1
0
mirror of https://github.com/20kdc/OC-KittenOS.git synced 2024-11-17 07:58:06 +11:00
OC-KittenOS/code/apps/app-claw.lua

347 lines
7.9 KiB
Lua
Raw Normal View History

-- This is released into the public domain.
-- No warranty is provided, implied or otherwise.
-- app-claw: Package manager.
2018-03-28 00:40:05 +11:00
-- libs & such
local event = require("event")(neo)
local neoux, err = require("neoux")
if not neoux then error(err) end
neoux = neoux(event, neo)
local claw = require("app-claw-core")()
local clawcsi = require("app-claw-csi")
2018-03-28 00:40:05 +11:00
local source = "http://20kdc.duckdns.org/neo/DEV/"
2018-03-28 00:40:05 +11:00
local disks = neo.requireAccess("c.filesystem", "searching disks for packages")
local primaryDisk = disks.primary
local primaryINet = neo.requestAccess("c.internet")
if primaryINet then primaryINet = primaryINet.list()() end
--
2018-03-28 00:40:05 +11:00
local function yielder()
-- slightly dangerous, but what can we do?
pcall(event.sleepTo, os.uptime() + 0.05)
2018-03-28 00:40:05 +11:00
end
2018-03-28 00:40:05 +11:00
local function download(url, cb)
if not primaryINet then return nil, "no internet" end
2018-03-28 00:40:05 +11:00
local req, err = primaryINet.request(source .. url)
if not req then
2018-03-28 00:40:05 +11:00
cb(nil)
return nil, "dlR/" .. tostring(err)
end
-- OpenComputers#535
req.finishConnect()
while true do
local n, n2 = req.read(neo.readBufSize)
2018-03-28 00:40:05 +11:00
local o, r = cb(n)
if not o then
req.close()
return nil, r
end
if not n then
req.close()
if n2 then
return nil, n2
else
break
end
else
if n == "" then
2018-03-28 00:40:05 +11:00
yielder()
end
end
end
2018-03-28 00:40:05 +11:00
return true
end
2018-03-28 00:40:05 +11:00
local function fsSrc(disk)
return function (url, cb)
local h, e = disk.open(url, "rb")
if not h then cb(nil) return nil, tostring(e) end
local c = ""
while c do
c = disk.read(h, neo.readBufSize)
local o, r = cb(c)
if not o then return nil, r end
end
2018-03-28 00:40:05 +11:00
disk.close(h)
return true
end
end
2018-03-28 00:40:05 +11:00
local function fsDst(disk)
return {function (url)
local h, e = disk.open(url, "wb")
if not h then return nil, tostring(e) end
return function (d)
local ok, r = true
if d then
ok, r = disk.write(h, d)
else
disk.close(h)
end
if not ok then return nil, tostring(r) end
return true
end
2018-03-28 00:40:05 +11:00
end, disk.makeDirectory, disk.exists, disk.isDirectory, disk.remove, disk.rename}
end
local function checked(...)
local res, res2, err = pcall(...)
if not res then
neoux.startDialog(tostring(res2), "error!", true)
elseif not res2 then
neoux.startDialog(tostring(err), "failed!", true)
else
2018-03-28 00:40:05 +11:00
return res2
end
end
2018-03-28 00:40:05 +11:00
-- Beginning Of The App (well, the actual one)
local genCurrent, genPrimary, genPackage, primaryWindow
local running = true
2018-03-28 00:40:05 +11:00
-- primary
local primarySearchTx = ""
local primaryPage = 1
local primaryList = {}
local primaryNextMinus = false
2018-03-28 00:40:05 +11:00
-- package
local packageLock = nil
local packageId = "FIXME"
2018-03-28 00:40:05 +11:00
local function describe(pkg)
local weHave = claw.getInfo(pkg, "local")
local theyHave = claw.getInfo(pkg, "local")
local someoneHas = claw.getInfo(pkg, nil, true)
if weHave then
if theyHave.v > weHave.v then
return pkg .. " [v" .. weHave.v .. "!]"
end
2018-03-28 00:40:05 +11:00
if someoneHas.v < weHave.v then
return pkg .. " (v" .. weHave.v .. ") R<"
end
2018-03-28 00:40:05 +11:00
return pkg .. " (v" .. weHave.v .. ")"
end
2018-03-28 00:40:05 +11:00
return pkg
end
2018-03-28 00:40:05 +11:00
local function primaryWindowRegenCore()
local gen, gens = genCurrent()
return 25, 12, "claw", neoux.tcwindow(25, 12, gen, function (w)
2018-03-28 00:40:05 +11:00
w.close()
running = false
end, 0xFF8F00, 0, gens)
end
2018-03-28 00:40:05 +11:00
local function primaryWindowRegen()
primaryWindow.reset(primaryWindowRegenCore())
end
2018-03-28 00:40:05 +11:00
-- Use all non-primary filesystems
for pass = 1, 3 do
for v in disks.list() do
local nam = nil
if v == primaryDisk then
nam = (pass == 1) and "local"
elseif v == disks.temporary then
nam = (pass == 2) and "ramfs"
elseif pass == 3 then
nam = v.address
end
2018-03-28 00:40:05 +11:00
if nam then
local ok, r = clawcsi(claw, nam, fsSrc(v), (not v.isReadOnly()) and fsDst(v))
2018-03-28 00:40:05 +11:00
if not ok and nam == "local" then
claw.unlock()
error(r)
end
end
end
end
-- No longer needed
disks = nil
2018-03-28 00:40:05 +11:00
if primaryINet then
checked(clawcsi, claw, "inet", download)
end
clawcsi = nil
2018-03-28 00:40:05 +11:00
primaryList = claw.getList()
-- Sections
function genPrimary()
local minus = (primaryNextMinus and 3) or nil
primaryNextMinus = false
2018-03-28 00:40:05 +11:00
local pgs = 10
local pages = math.ceil(#primaryList / pgs)
local elems = {
2018-03-28 00:40:05 +11:00
neoux.tcbutton(23, 1, "+", function (w)
if primaryPage < pages then
primaryPage = primaryPage + 1
primaryWindowRegen()
end
end),
2018-03-28 00:40:05 +11:00
neoux.tcrawview(4, 1, {neoux.pad(primaryPage .. " / " .. pages, 19, true, true)}),
neoux.tcbutton(1, 1, "-", function (w)
2018-03-28 00:40:05 +11:00
if primaryPage > 1 then
primaryNextMinus = true
2018-03-28 00:40:05 +11:00
primaryPage = primaryPage - 1
primaryWindowRegen()
end
end)
}
2018-03-28 00:40:05 +11:00
local base = (primaryPage - 1) * pgs
for i = 1, pgs do
local ent = primaryList[base + i]
if ent then
2018-03-28 00:40:05 +11:00
local enttx = describe(ent)
table.insert(elems, neoux.tcbutton(1, i + 1, unicode.safeTextFormat(enttx), function (w)
-- FREE UP MEMORY NOW
elems = {}
w.reset(25, 12, "claw", function (ev)
if ev == "close" then
w.close()
running = false
end
end)
2018-03-28 00:40:05 +11:00
packageId = ent
genCurrent = genPackage
primaryWindowRegen()
end))
end
end
2018-03-28 00:40:05 +11:00
table.insert(elems, neoux.tcfield(1, 12, 16, function (s)
if s then primarySearchTx = s end
return primarySearchTx
end))
2018-03-28 00:40:05 +11:00
table.insert(elems, neoux.tcbutton(17, 12, "Search!", function (w)
local n = {}
2018-03-28 00:40:05 +11:00
for _, v in ipairs(claw.getList()) do
for i = 1, #v do
2018-03-28 00:40:05 +11:00
if v:sub(i, i + #primarySearchTx - 1) == primarySearchTx then
table.insert(n, v)
break
end
end
end
2018-03-28 00:40:05 +11:00
primaryPage = 1
primaryList = n
primaryWindowRegen()
end))
return elems, minus
end
2018-03-28 00:40:05 +11:00
--
local function packageGetBB(src, lclI, srcI, srcW)
local buttons = {}
if srcI and srcW then
table.insert(buttons, {
"Del",
function ()
if packageLock then return end
packageLock = ""
checked(claw.remove, src, packageId, true)
packageLock = nil
primaryWindowRegen()
end
})
end
if srcI and ((not lclI) or (lclI.v < srcI.v)) then
table.insert(buttons, {
"Get",
function ()
if packageLock then return end
packageLock = "installing from " .. src
primaryWindowRegen()
checked(claw.installTo, "local", packageId, src, true, yielder)
packageLock = nil
primaryWindowRegen()
end
})
end
if srcW and lclI and not srcI then
table.insert(buttons, {
"All",
function ()
if packageLock then return end
packageLock = "storing w/ dependencies at " .. src
primaryWindowRegen()
checked(claw.installTo, src, packageId, "local", true, yielder)
packageLock = nil
primaryWindowRegen()
end
})
table.insert(buttons, {
"Put",
function ()
if packageLock then return end
packageLock = "storing at " .. src
primaryWindowRegen()
checked(claw.installTo, src, packageId, "local", false, yielder)
packageLock = nil
primaryWindowRegen()
end
})
end
return buttons
end
function genPackage()
if packageLock then
return {neoux.tcrawview(1, 1, neoux.fmtText(unicode.safeTextFormat(packageId .. "\n" .. packageLock), 25))}
end
-- concept:
-- mtd <back>
-- Multi-Track Drifting
--
-- local v20 <del> <run>
-- inet v21 <pull>
-- dir v22 <pull> <push>
-- crockett <push>
local info = claw.getInfo(packageId)
local infoL = claw.getInfo(packageId, "local")
local elems = {
neoux.tcrawview(1, 1, neoux.fmtText(unicode.safeTextFormat(packageId .. "\n" .. info.desc .. "\nv" .. info.v .. " deps " .. table.concat(info.deps, ", ")), 25)),
neoux.tcbutton(20, 1, "Back", function ()
if packageLock then return end
genCurrent = genPrimary
primaryWindowRegen()
end)
}
for k, v in ipairs(claw.sourceList) do
2018-03-28 00:40:05 +11:00
local lI = claw.getInfo(packageId, v[1])
local row = 12 + k - #(claw.sourceList)
2018-03-28 00:40:05 +11:00
local pfx = " "
if lI then
pfx = "v" .. string.format("%04i", lI.v) .. " "
end
table.insert(elems, neoux.tcrawview(1, row, {neoux.pad(pfx .. v[1], 14, false, true)}))
local col = 26
for _, bv in ipairs(packageGetBB(v[1], infoL, lI, v[2])) do
local b = neoux.tcbutton(col, row, bv[1], bv[2])
col = col - b.w
b.x = col
table.insert(elems, b)
end
end
return elems
end
--
genCurrent = genPrimary
primaryWindow = neoux.create(primaryWindowRegenCore())
while running do
event.pull()
end
2018-03-28 00:40:05 +11:00
claw.unlock()