OC-DSS/OpenOS/usr/bin/items.lua

141 lines
3.5 KiB
Lua

local inv = require "inv"
local event = require "event"
local sides = require "sides"
local searchterm = ""
local output = nil
local ci,si = 1,1
local run = true
local function search()
io.write("\n\27[2KSearch: ")
searchterm = io.read()
end
local function setOutput()
io.write("\n\27[2KOutput side:")
local in = io.read()
output = tonumber(in) or sides[in]
end
local function extract(res)
local slot,item = res[1], res[2]
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
inv.extractItem(slot,count)
end
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 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][2]
pt = " "
if ci == i then
pt = ">"
end
-- print(string.format("%s %s (%s - %d)",pt,item.label, item.name, item.size):sub(1,79))
print(string.format("%s %s (%d - %dx%d + %d)",pt,item.label, item.size, math.floor(item.size/item.maxSize),item.maxSize,item.size%item.maxSize):sub(1,79))
end
end
io.write("\27[25;62HPress [H] for help.")
-- io.write(string.format("\27[24;1H(%d/%d) [Tab] Search [Enter] Extract [i] Information [r] Refresh",used,size))
io.write(string.format("\27[24;1H(%d/%d) %s",used,size-1,searchterm))
end
while run do
local sr, used, size = inv.search(searchterm)
ci = 1
while true do
--[[
if ci > math.min(#sr,23) then
ci = math.min(#sr,23)
elseif ci < 1 then
ci = 1
end
]]--
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("key_down")
ch=string.char(ch)
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][2])
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