Labyrinthia

utils

Jan 18th, 2021 (edited)
617
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.45 KB | None | 0 0
  1. -- Little thing with some functions I like to use in my stuff.
  2. function printCenter(text)
  3.     local w, _ = term.getSize()
  4.     local _, y = term.getCursorPos()
  5.     local x = math.max(math.floor((w / 2) - (#text / 2)), 0)
  6.     term.setCursorPos(x, y)
  7.     write(text)
  8. end
  9.  
  10. function printFarRight(text)
  11.     local w, _ = term.getSize()
  12.     local _, h = term.getCursorPos()
  13.     term.setCursorPos(w - #text, h)
  14.     print(text)
  15. end
  16.  
  17. function printSpecificX(text, x)
  18.     local _, h = term.getCursorPos()
  19.     term.setCursorPos(x, h)
  20.     write(text)
  21. end
  22.  
  23. function has_key(array, key)
  24.     return array[key] ~=  nil
  25. end
  26.  
  27. function getTableSize(array)
  28.     local count = 0
  29.     for _, __ in pairs(array) do
  30.         count = count + 1
  31.     end
  32.     return count
  33. end
  34.  
  35.  
  36. function deepEquals(t1, t2)
  37.     local ty1 = type(t1)
  38.     local ty2 = type(t2)
  39.  
  40.     if ty1 ~= ty2 then
  41.         return false
  42.     end
  43.  
  44.     -- non-table types can be directly compared
  45.     if ty1 ~= "table" and ty2 ~= "table" then
  46.         return t1 == t2
  47.     end
  48.  
  49.     if getTableSize(t1) ~= getTableSize(t2) then
  50.         return false
  51.     end
  52.  
  53.     for k1, v1 in pairs(t1) do
  54.         local v2 = t2[k1]
  55.         if v2 == nil or not deepEquals(v1, v2) then
  56.             return false
  57.         end
  58.     end
  59.  
  60.     for k2, v2 in pairs(t2) do
  61.         local v1 = t1[k2]
  62.         if v1 == nil or not deepEquals(v1, v2) then
  63.             return false
  64.         end
  65.     end
  66.    
  67.     return true
  68. end
  69.  
Add Comment
Please, Sign In to add comment