Advertisement
jille_Jr

CC: inspect.lua

Nov 15th, 2020 (edited)
635
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.55 KB | None | 0 0
  1. ---@alias itemInspected table<string, any>
  2.  
  3. local linesPrinted = 0
  4. local termWidth, termHeight = term.getSize()
  5.  
  6. ---@type table<string,fun():boolean,itemInspected|nil>
  7. local inspectSides = {
  8.     up = turtle.inspectUp,
  9.     u = turtle.inspectUp,
  10.     down = turtle.inspectDown,
  11.     dn = turtle.inspectDown,
  12.     d = turtle.inspectDown,
  13.     [""] = turtle.inspect,
  14.     forward = turtle.inspect,
  15.     fd = turtle.inspect,
  16.     f = turtle.inspect,
  17. }
  18.  
  19. ---@param t table<string,any>
  20. local function pairsByKeys(t)
  21.     local allKeys = {}
  22.     for k in pairs(t) do
  23.         if type(k) == "string" then
  24.             table.insert(allKeys, k)
  25.         end
  26.     end
  27.     table.sort(allKeys)
  28.     local i = 0
  29.     return function()
  30.         i = i + 1
  31.         local key = allKeys[i]
  32.         if key == nil then
  33.             return nil
  34.         end
  35.         return key, t[key]
  36.     end
  37. end
  38.  
  39. ---@param t table<string,any>
  40. ---@param indent number|nil
  41. local function printPairs(t, indent)
  42.     indent = indent or 0
  43.     for k, v in pairsByKeys(t) do
  44.         if linesPrinted >= termHeight - 2 then
  45.             term.setTextColor(colors.gray)
  46.             write("(Press a key to keep printing)")
  47.             os.pullEvent("key")
  48.             term.clearLine()
  49.             local _, y = term.getCursorPos()
  50.             term.setCursorPos(1, y)
  51.         end
  52.  
  53.         if indent > 0 then
  54.             term.setTextColor(colors.gray)
  55.             term.write(string.rep("  ", indent - 1))
  56.             term.write("- ")
  57.         end
  58.         term.setTextColor(colors.lightGray)
  59.         linesPrinted = linesPrinted + write(tostring(k) .. ": ")
  60.         if type(v) == "string" then
  61.             term.setTextColor(colors.lime)
  62.             linesPrinted = linesPrinted + print(v)
  63.         elseif type(v) == "boolean" then
  64.             term.setTextColor(colors.purple)
  65.             linesPrinted = linesPrinted + print(v)
  66.         elseif type(v) == "number" then
  67.             term.setTextColor(colors.purple)
  68.             linesPrinted = linesPrinted + print(v)
  69.         elseif type(v) == "table" then
  70.             linesPrinted = linesPrinted + print()
  71.             printPairs(v, indent + 1)
  72.         else
  73.             linesPrinted = linesPrinted + print(v)
  74.         end
  75.     end
  76. end
  77.  
  78. ---@param inspectFunc fun():boolean,itemInspected|nil
  79. local function inspectBlock(inspectFunc)
  80.     local any, item = inspectFunc()
  81.     if not any or not item then
  82.         term.setTextColor(colors.red)
  83.         print("Cannot inspect air")
  84.     else
  85.         printPairs(item)
  86.     end
  87. end
  88.  
  89. ---@param slot number
  90. local function inspectSlot(slot)
  91.     local item = turtle.getItemDetail(slot)
  92.     if item == nil then
  93.         term.setTextColor(colors.red)
  94.         print("Cannot inspect air")
  95.     else
  96.         printPairs(item)
  97.     end
  98. end
  99.  
  100. local args = { ... }
  101.  
  102. local function usage(msg)
  103.     msg = msg and (msg .. "\n") or ""
  104.     error(msg .. 'usage: inspect [slot:integer|side:string]', 0)
  105. end
  106.  
  107. local target = args[1] or ""
  108. local targetSlot = tonumber(target)
  109.  
  110. if targetSlot then
  111.     if targetSlot > 16 or targetSlot < 1 then
  112.         usage("slot must be in the range of 1-16 (inclusive)")
  113.     end
  114.     inspectSlot(targetSlot)
  115. else
  116.     local inspectFunc = inspectSides[target]
  117.  
  118.     if inspectFunc then
  119.         inspectBlock(inspectFunc)
  120.     else
  121.         local valid = {}
  122.         for key in pairs(inspectSides) do
  123.             if key ~= "" then
  124.                 table.insert(valid, key)
  125.             end
  126.         end
  127.         usage('unknown side: "' .. target .. '"\nvalid sides: ' .. table.concat(valid, ", "))
  128.     end
  129. end
  130.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement