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 sides = require "sides"
local inv = require "inv"
local function popup(str,title)
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
local mtmenu = require "mtmenu"
local stat = ""
function inv.search(searchterm)
local rt
repeat
rt = inv.matchAll({label=searchterm or ""},true)
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")
end
until rt
@ -45,7 +26,7 @@ local timers = {}
repeat
local w,aliases = pcall(inv.getAliases)
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")
else
output=aliases[os.getenv("HOSTNAME")]
@ -79,7 +60,8 @@ local function extract(item)
if count == "x" then return false end
count=tonumber(count) or 64
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
local function info(item)
@ -91,73 +73,35 @@ local function info(item)
end
end
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")
end
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")
end
local function draw(sr, ci, used, size)
io.write("\27[2J\27[H")
for i = si, si+22 do
if sr[i] then
item = sr[i]
pt = "\27[0m"
if ci == i then
pt = "\27[7m"
end
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)))
end
local function formatEntries(t,used,size,searchterm)
local rt = {}
local mx, my = mtmenu.getScreenSize()
rt.header = " Name" .. (" "):rep(mx-11) .. "Count"
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"
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)
end
io.write("\27[0m\27[25;62HPress [H] for help.")
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))
return rt
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
local sr, used, size = inv.search(searchterm)
ci = 1
local ci, ch, co, st
while true do
if ci > #sr then
ci = #sr
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
ci, ch, co, st = mtmenu.menu(formatEntries(sr,used,size,searchterm), st)
if co == 28 then -- enter
extract(sr[ci])
break
elseif co == 15 then -- tab
@ -168,8 +112,7 @@ while run do
elseif ch == "h" then
help()
elseif ch == "r" then
popup("Refreshing index...")
-- inv.inputItems()
mtmenu.popup("Refreshing index...")
break
elseif ch == "o" then
setOutput()
@ -181,4 +124,4 @@ while run do
end
for k,v in pairs(timers) do
event.cancel(v)
end
end

View File

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