new version of search4, and modified server libraries to be simpler

This commit is contained in:
Izaya 2023-11-23 06:45:01 +10:00
parent 8a3dd58423
commit ad3396f025
2 changed files with 28 additions and 85 deletions

View File

@ -1,34 +1,15 @@
local event = require "event" local event = require "event"
local sides = require "sides" local sides = require "sides"
local inv = require "inv" local inv = require "inv"
local mtmenu = require "mtmenu"
local function popup(str,title) local stat = ""
if title then
title=string.format("[%s]",title or "")
else
title = ""
end
local width, height, content = 0, 0, {}
for line in str:gmatch("[^\n]*") do
height = height + 1
width = math.max(width,line:len())
content[#content+1] = line
end
if width < 1 or height < 1 then return false end
local startx,starty = (40-(width//2))-2, (12-(height//2))-2
io.write(string.format("\27[%d;%dH╒═%s%s╕",starty,startx,title,(""):rep((width+1)-title:len())))
for k,v in pairs(content) do
io.write(string.format("\27[%d;%dH│ %s%s │",starty+k,startx,v,(" "):rep(width-v:len())))
end
io.write(string.format("\27[%d;%dH┕%s┙",starty+1+#content,startx,(""):rep(width+2)))
end
function inv.search(searchterm) function inv.search(searchterm)
local rt local rt
repeat repeat
rt = inv.matchAll({label=searchterm or ""},true) rt = inv.matchAll({label=searchterm or ""},true)
if not rt then if not rt then
popup("Server appears to be down.\nPress any key to try again.","Error") mtmenu.popup("Server appears to be down.\nPress any key to try again.","Error")
event.pull("key_down") event.pull("key_down")
end end
until rt until rt
@ -45,7 +26,7 @@ local timers = {}
repeat repeat
local w,aliases = pcall(inv.getAliases) local w,aliases = pcall(inv.getAliases)
if not w then if not w then
popup("Server appears to be down.\nPress any key to try again.","Error") mtmenu.popup("Server appears to be down.\nPress any key to try again.","Error")
event.pull("key_down") event.pull("key_down")
else else
output=aliases[os.getenv("HOSTNAME")] output=aliases[os.getenv("HOSTNAME")]
@ -79,7 +60,8 @@ local function extract(item)
if count == "x" then return false end if count == "x" then return false end
count=tonumber(count) or 64 count=tonumber(count) or 64
item.size = nil item.size = nil
print(inv.extract(item,count,output[1],output[2])) 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))
end end
local function info(item) local function info(item)
@ -91,73 +73,35 @@ local function info(item)
end end
end end
s=s.."\n\nPress any key." s=s.."\n\nPress any key."
popup(s,item.label or item.name) mtmenu.popup(s,item.label or item.name)
event.pull("key_down") event.pull("key_down")
end end
local function help() local function help()
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") 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")
event.pull("key_down") event.pull("key_down")
end end
local function draw(sr, ci, used, size) local function formatEntries(t,used,size,searchterm)
io.write("\27[2J\27[H") local rt = {}
for i = si, si+22 do local mx, my = mtmenu.getScreenSize()
if sr[i] then rt.header = " Name" .. (" "):rep(mx-11) .. "Count"
item = sr[i] rt.status = string.format(" %s (%i/%i) %s %s", phases[math.floor((os.time()-43200)/86400)%8], used, size, searchterm):sub(1,mx):sub(1,mx-7) .. "[H]elp"
pt = "\27[0m" for k,v in ipairs(t) do
if ci == i then -- (YYxYY+NN)
pt = "\27[7m" local count = string.format("(%ix%i+%i)",v.size//v.maxSize, v.maxSize, v.size%v.maxSize)
end local fmt = string.format("%%-%is %%s", mx-#count-1)
print(string.format("%s%-59s %20s", pt, item.label, string.format("(%d - %dx%d + %d)", item.size, math.floor(item.size/item.maxSize),item.maxSize,item.size%item.maxSize))) rt[k] = string.format(fmt, v.label, count)
end
end end
io.write("\27[0m\27[25;62HPress [H] for help.") return rt
local mcol = 3
local countdown = 8-(math.floor((os.time()-43200)/86400)%8)
if countdown == 8 then
mcol = 1
elseif countdown >= 4 then
mcol = 2
end
-- io.write(string.format("\27[0m\27[1;77H\27[3%im%i %s\27[0m", mcol, countdown, phases[math.floor((os.time()-43200)/86400)%8]))
io.write(string.format("\27[0m\27[24;1H\27[3%im%1i %2s\27[0m %-70s",mcol, countdown, phases[math.floor((os.time()-43200)/86400)%8], searchterm))
-- io.write(string.format("\27[24;1H(%d/%d) %s",used,size-1,searchterm))
end end
--[[
timers[#timers+1] = event.timer(120,function()
io.write(string.format("\27[1;77H%i %s\27[24;1H", 8-(math.floor((os.time()-43200)/86400)%8), phases[math.floor((os.time()-43200)/86400)%8]))
end,math.huge)
]]--
while run do while run do
local sr, used, size = inv.search(searchterm) local sr, used, size = inv.search(searchterm)
ci = 1 local ci, ch, co, st
while true do while true do
if ci > #sr then ci, ch, co, st = mtmenu.menu(formatEntries(sr,used,size,searchterm), st)
ci = #sr if co == 28 then -- enter
elseif ci < 1 then
ci = 1
end
if ci > math.min(si + 20,#sr) then
si = si + 5
elseif ci < si + 2 then
si = si - 5
end
if si < 1 then
si = 1
elseif si > #sr then
si = #sr - 23
end
draw(sr,ci,used,size)
local _,_,ch,co = event.pull(60,"key_down")
ch=string.char(ch or 0)
if co == 208 then -- down
ci = ci + 1
elseif co == 200 then -- up
ci = ci - 1
elseif co == 28 then -- enter
extract(sr[ci]) extract(sr[ci])
break break
elseif co == 15 then -- tab elseif co == 15 then -- tab
@ -168,8 +112,7 @@ while run do
elseif ch == "h" then elseif ch == "h" then
help() help()
elseif ch == "r" then elseif ch == "r" then
popup("Refreshing index...") mtmenu.popup("Refreshing index...")
-- inv.inputItems()
break break
elseif ch == "o" then elseif ch == "o" then
setOutput() setOutput()

View File

@ -27,7 +27,7 @@ function inv.match(specifiers)
for item in transposers[1].getAllStacks(transposers[1].drawerside) do for item in transposers[1].getAllStacks(transposers[1].drawerside) do
local match = true local match = true
for k,v in pairs(specifiers) do for k,v in pairs(specifiers) do
if item[k] ~= v then if type(v) ~= "table" and item[k] ~= v then
match = false match = false
break break
end end
@ -60,11 +60,11 @@ function inv.matchAll(specifiers,fuzzy)
end end
end end
if match then if match then
rt[counter] = item rt[#rt+1] = item
end end
counter = counter + 1 counter = counter + 1
end end
return rt return rt, #rt, counter
end end
function inv.extract(specifiers,count,addr,side) function inv.extract(specifiers,count,addr,side)