Advertisement
i6_quadcore

utils

Feb 22nd, 2025
279
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.40 KB | None | 0 0
  1. indexToColor = {
  2.     colors.blue,
  3.     colors.green,
  4.     colors.red,
  5.     colors.orange,
  6.     colors.magenta,
  7.     colors.lightBlue,
  8.     colors.yellow,
  9.     colors.lime,
  10.     colors.cyan,
  11.     colors.purple
  12. }
  13.  
  14. colorToIndex = {}
  15. for k, v in pairs(indexToColor) do colorToIndex[v] = k end
  16.  
  17. colorCount = table.getn(indexToColor)
  18.  
  19. local toastText = nil
  20. local statusText = nil
  21.  
  22. function removeToast()
  23.     if (toastText ~= nil) then toastText:remove() end
  24.     toastText = nil
  25. end
  26.  
  27. function removeStatus()
  28.     if (statusText ~= nil) then statusText:remove() end
  29.     statusText = nil
  30. end
  31.  
  32. function label(frame, text, x, y)
  33.     local newLabel = frame:addLabel():setText(text):setSize(#text,1):setPosition(x, y)
  34.     return newLabel
  35. end
  36.  
  37. function toast(frame, text, color)
  38.     if (toastText ~= nil) then removeToast() end
  39.     toastText = label(frame, text, 3, 18):setForeground(color)
  40.     toastText:onClick(removeToast)
  41.     -- frame:addTimer():onCall(removeToast):setTime(3):start()
  42. end
  43.  
  44. function status(frame, text, color)
  45.     if (statusText ~= nil) then removeStatus() end
  46.     statusText = label(frame, text, string.format("parent.w - %d + 1", #text), 1):setForeground(color)
  47.     -- frame:addTimer():onCall(removeStatus):setTime(3):start()
  48. end
  49.  
  50. function saveTable(table, path)
  51.     local file = fs.open(path, "w")
  52.     file.write(textutils.serialize(table))
  53.     file.close()
  54. end
  55.  
  56. function loadTable(path)
  57.     local file = fs.open(path, "r")
  58.     if (file == nil) then return {} end
  59.  
  60.     local data = file.readAll()
  61.     file.close()
  62.     local result = textutils.unserialize(data)
  63.     if result == nil then return {} end
  64.  
  65.     return result
  66. end
  67.  
  68. function saveCookbook(cookbook)
  69.     saveTable(cookbook, "cookbook.dat")
  70. end
  71.  
  72. function tally(t)
  73.     local freq = {}
  74.     for _, v in pairs(t) do
  75.         freq[v] = (freq[v] or 0) + 1
  76.     end
  77.     local freqPairs = {}
  78.     for key, value in pairs(freq) do
  79.         table.insert(freqPairs, {key, value})
  80.     end
  81.     table.sort(freqPairs, function(a, b) return a[2] > b[2] end)
  82.    
  83.     return freqPairs
  84. end
  85.  
  86. -- SO 640642 -- Tyler
  87. function deepCopy(obj, seen)
  88.     if type(obj) ~= 'table' then return obj end
  89.     if seen and seen[obj] then return seen[obj] end
  90.     local s = seen or {}
  91.     local res = setmetatable({}, getmetatable(obj))
  92.     s[obj] = res
  93.     for k, v in pairs(obj) do res[deepCopy(k, s)] = deepCopy(v, s) end
  94.     return res
  95. end
  96.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement