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

184 lines
4.5 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 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
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")
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
popup("Server appears to be down.\nPress any key to try again.","Error")
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
print(inv.extract(item,count,output[1],output[2]))
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."
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")
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
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))
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
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
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
popup("Refreshing index...")
-- inv.inputItems()
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