OC-DSS2/OpenOS/client/usr/bin/search4.lua

128 lines
3.1 KiB
Lua
Raw Normal View History

2021-09-03 10:59:21 +10:00
local event = require "event"
local sides = require "sides"
local inv = require "inv"
local mtmenu = require "mtmenu"
local stat = ""
2021-09-03 10:59:21 +10:00
function inv.search(searchterm)
local rt
repeat
rt = inv.matchAll({label=searchterm or ""},true)
if not rt then
mtmenu.popup("Server appears to be down.\nPress any key to try again.","Error")
2021-09-03 10:59:21 +10:00
event.pull("key_down")
end
until rt
return rt, #rt, #rt
end
local searchterm = ""
--local output = inv.getAliases()[os.getenv("HOSTNAME")]
local output
local ci,si = 1,1
local run = true
local timers = {}
repeat
local w,aliases = pcall(inv.getAliases)
if not w then
mtmenu.popup("Server appears to be down.\nPress any key to try again.","Error")
2021-09-03 10:59:21 +10:00
event.pull("key_down")
else
output=aliases[os.getenv("HOSTNAME")]
end
until w
local phases = {
[0] = "⣿⣿",
"⢾⣿",
"",
"",
"⣏⣹",
"",
"",
"⣿⡷"
}
local function search()
io.write("\n\27[2KSearch: ")
searchterm = io.read()
end
local function setOutput()
io.write("\n\27[2KOutput side:")
local ins = io.read()
output = tonumber(ins) or sides[ins]
end
local function extract(item)
io.write(string.format("\nExtracting %s; Count [64] or x to exit? ",item.label))
local count = io.read()
if count == "x" then return false end
count=tonumber(count) or 64
item.size = nil
local w, c = inv.extract(item,count,output[1],output[2])
mtmenu.setStatus(string.format("%s: %i items extracted", w and "Success" or "Error", c or 0))
2021-09-03 10:59:21 +10:00
end
local function info(item)
local s = string.format("\n%s (%s)\nCount: %d\n",item.label,item.name,item.size)
if item.aspects then
s=s.."Aspects: "
for k,v in pairs(item.aspects) do
s=string.format("%s\n - %d x %s",s,v,k)
end
end
s=s.."\n\nPress any key."
mtmenu.popup(s,item.label or item.name)
2021-09-03 10:59:21 +10:00
event.pull("key_down")
end
local function help()
mtmenu.popup("\n[Tab] Search\n[Enter] Extract Items\n[i] Item Information\n[r] Refresh\n[o] Set output\n\nPress any key.","Key bindings")
2021-09-03 10:59:21 +10:00
event.pull("key_down")
end
local function formatEntries(t,used,size,searchterm)
local rt = {}
local mx, my = mtmenu.getScreenSize()
rt.header = " Name" .. (" "):rep(mx-11) .. "Count"
2023-11-23 09:50:44 +11:00
rt.status = string.format(" %s (%i/%i) %s %s", phases[math.floor((os.time()-43200)/86400)%8], used, size, searchterm, (" "):rep(mx)):sub(1,mx):sub(1,mx-5) .. "[H]elp"
for k,v in ipairs(t) do
-- (YYxYY+NN)
local count = string.format("(%ix%i+%i)",v.size//v.maxSize, v.maxSize, v.size%v.maxSize)
local fmt = string.format("%%-%is %%s", mx-#count-1)
rt[k] = string.format(fmt, v.label, count)
2021-09-03 10:59:21 +10:00
end
return rt
2021-09-03 10:59:21 +10:00
end
while run do
local sr, used, size = inv.search(searchterm)
local ci, ch, co, st
2021-09-03 10:59:21 +10:00
while true do
ci, ch, co, st = mtmenu.menu(formatEntries(sr,used,size,searchterm), st)
if co == 28 then -- enter
2021-09-03 10:59:21 +10:00
extract(sr[ci])
break
elseif co == 15 then -- tab
search()
break
elseif ch == "i" then
info(sr[ci])
elseif ch == "h" then
help()
elseif ch == "r" then
mtmenu.popup("Refreshing index...")
2021-09-03 10:59:21 +10:00
break
elseif ch == "o" then
setOutput()
elseif ch == "q" then
run = false
break
end
end
end
for k,v in pairs(timers) do
event.cancel(v)
end