Advertisement
goldfiction

utils

Aug 10th, 2023
757
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.89 KB | None | 0 0
  1. function findItemIndexInTable(table, item)
  2.   for index, value in pairs(table) do
  3.     if value == item then
  4.       return index
  5.     end
  6.   end
  7.  
  8.   return nil
  9. end
  10.  
  11. function drawBargraph(fillPercent, mon, fillColor)
  12.   local orgTerm = term.current()
  13.   local graphCoords = {}
  14.   local width, height = mon.getSize()
  15.   graphCoords["X1"] = width - (width/3)
  16.   graphCoords["Y1"] = height - (height/100) * fillPercent
  17.   graphCoords["X2"] = width - (width/20)
  18.   graphCoords["Y2"] = height
  19.  
  20.   term.redirect(mon)
  21.   mon.clear()
  22.   term.clear()
  23.   paintutils.drawFilledBox(graphCoords["X1"], graphCoords["Y1"], graphCoords["X2"], graphCoords["Y2"], fillColor)
  24.   term.redirect(orgTerm)
  25. end
  26.  
  27. function findAndWrap(devType)
  28.   local devices = peripheral.getNames()
  29.   for id, devName in pairs(devices) do
  30.     if peripheral.getType(devName) == devType then
  31.       return peripheral.wrap(devName)
  32.     end
  33.   end
  34. end
  35.  
  36. function findAndWrapAll(devType, useStartsWith)
  37.   useStartsWith = useStartsWith or false
  38.  
  39.   local deviceWrappers = {}
  40.   local devices = peripheral.getNames()
  41.   for id, devName in pairs(devices) do
  42.     if (useStartsWith == true and devName:sub(1, #devType) == devType) or peripheral.getType(devName) == devType then
  43.       deviceWrappers[(devName .. "_" .. devType)] = peripheral.wrap(devName)
  44.     end
  45.   end
  46.   return deviceWrappers
  47. end
  48.  
  49. function orderTable(t, f)
  50.   local a = {}
  51.   for n in pairs(t) do
  52.     table.insert(a, n)
  53.   end
  54.   table.sort(a, f)
  55.   local i = 0
  56.   local iter = function ()
  57.     i = i + 1
  58.     if a[i] == nil then
  59.       return nil
  60.     else
  61.       return a[i], t[a[i]]
  62.     end
  63.   end
  64.  
  65.   return iter
  66. end
  67.  
  68. function printTable(tableData)
  69.   for key, value in pairs(tableData) do
  70.     print(key .. ":" .. value)
  71.   end
  72. end
  73.  
  74. function formatInt(num, separator)
  75.   if tonumber(num) == nil then
  76.     return -1
  77.   else
  78.     separator = string.sub((separator or ","), 1, 1)
  79.     return tostring(math.floor(num)):reverse():gsub("(%d%d%d)", "%1"..separator):gsub(separator.."(%-?)$", "%1"):reverse()
  80.   end
  81. end
  82.  
  83. function round(num, places)
  84.   local mult = 10^(places or 0)
  85.   return math.floor(num * mult + 0.5) / mult
  86. end
  87.  
  88. function rednetLookupWithRetry(protocol, host)
  89.   local hostId = nil
  90.   local MAX_ATTEMPTS = 20
  91.   local attempts = 0
  92.  
  93.   while hostId == nil and attempts < MAX_ATTEMPTS do
  94.     attempts = attempts + 1
  95.     hostId = rednet.lookup(protocol, host)
  96.     sleep(1)
  97.   end
  98.   return hostId
  99. end
  100.  
  101. --Convert ME Display Bytes to smaller units
  102. function convertMEBytes(bytes)
  103.   local byteLength = string.len(bytes)
  104.   local postfix = "B"
  105.  
  106.   if byteLength > 3 and byteLength <= 6 then
  107.     postfix = "KB"
  108.     bytes = bytes / 1000
  109.   elseif byteLength > 6 and byteLength <= 9 then
  110.     postfix = "MB"
  111.     bytes = bytes / 1000000
  112.   elseif byteLength > 9 and byteLength <= 12 then
  113.     postfix = "GB"
  114.     bytes = bytes / 1000000000
  115.   end
  116.  
  117.   return round(bytes, 1) .. postfix
  118. end
  119.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement