---@alias itemInspected table local linesPrinted = 0 local termWidth, termHeight = term.getSize() ---@type table local inspectSides = { up = turtle.inspectUp, u = turtle.inspectUp, down = turtle.inspectDown, dn = turtle.inspectDown, d = turtle.inspectDown, [""] = turtle.inspect, forward = turtle.inspect, fd = turtle.inspect, f = turtle.inspect, } ---@param t table local function pairsByKeys(t) local allKeys = {} for k in pairs(t) do if type(k) == "string" then table.insert(allKeys, k) end end table.sort(allKeys) local i = 0 return function() i = i + 1 local key = allKeys[i] if key == nil then return nil end return key, t[key] end end ---@param t table ---@param indent number|nil local function printPairs(t, indent) indent = indent or 0 for k, v in pairsByKeys(t) do if linesPrinted >= termHeight - 2 then term.setTextColor(colors.gray) write("(Press a key to keep printing)") os.pullEvent("key") term.clearLine() local _, y = term.getCursorPos() term.setCursorPos(1, y) end if indent > 0 then term.setTextColor(colors.gray) term.write(string.rep(" ", indent - 1)) term.write("- ") end term.setTextColor(colors.lightGray) linesPrinted = linesPrinted + write(tostring(k) .. ": ") if type(v) == "string" then term.setTextColor(colors.lime) linesPrinted = linesPrinted + print(v) elseif type(v) == "boolean" then term.setTextColor(colors.purple) linesPrinted = linesPrinted + print(v) elseif type(v) == "number" then term.setTextColor(colors.purple) linesPrinted = linesPrinted + print(v) elseif type(v) == "table" then linesPrinted = linesPrinted + print() printPairs(v, indent + 1) else linesPrinted = linesPrinted + print(v) end end end ---@param inspectFunc fun():boolean,itemInspected|nil local function inspectBlock(inspectFunc) local any, item = inspectFunc() if not any or not item then term.setTextColor(colors.red) print("Cannot inspect air") else printPairs(item) end end ---@param slot number local function inspectSlot(slot) local item = turtle.getItemDetail(slot) if item == nil then term.setTextColor(colors.red) print("Cannot inspect air") else printPairs(item) end end local args = { ... } local function usage(msg) msg = msg and (msg .. "\n") or "" error(msg .. 'usage: inspect [slot:integer|side:string]', 0) end local target = args[1] or "" local targetSlot = tonumber(target) if targetSlot then if targetSlot > 16 or targetSlot < 1 then usage("slot must be in the range of 1-16 (inclusive)") end inspectSlot(targetSlot) else local inspectFunc = inspectSides[target] if inspectFunc then inspectBlock(inspectFunc) else local valid = {} for key in pairs(inspectSides) do if key ~= "" then table.insert(valid, key) end end usage('unknown side: "' .. target .. '"\nvalid sides: ' .. table.concat(valid, ", ")) end end