Advertisement
jille_Jr

CC: inspect.lua

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