Advertisement
Morthaden

morth-utils

Oct 7th, 2018 (edited)
2,063
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.63 KB | None | 0 0
  1. BASE_CHAR_WIDTH = 5
  2. BASE_CHAR_HEIGHT = 8
  3. BASE_EXCL_WIDTH = 2
  4.  
  5. -- Used to store 'default' peripherals to fall back on
  6. local PERIP_CACHE = {}
  7.  
  8. string.starts = function(String,Start)
  9.    return string.sub(String,1,string.len(Start))==Start
  10. end
  11.  
  12. table.reduce = function (list, fn, init) -- Technically a table as with everything else
  13.     local acc = init
  14.     for k, v in ipairs(list) do
  15.         if 1 == k and not init then
  16.             acc = v
  17.         else
  18.             acc = fn(acc, v)
  19.         end
  20.     end
  21.     return acc
  22. end
  23.  
  24. table.sum = function(list)
  25.     if #list == 0 then
  26.         return 0
  27.     end
  28.     if #list == 1 then
  29.         return list[1]
  30.     end
  31.     return table.reduce(list,
  32.         function(a,b)
  33.             return a+b
  34.         end
  35.     )
  36. end
  37.  
  38. function comma_value(amount)
  39.   local formatted = amount
  40.   while true do  
  41.     formatted, k = string.gsub(formatted, "^(-?%d+)(%d%d%d)", '%1,%2')
  42.     if (k==0) then
  43.       break
  44.     end
  45.   end
  46.   return formatted
  47. end
  48.  
  49. function tablelength(T)
  50.   local count = 0
  51.   for _ in pairs(T) do count = count + 1 end
  52.   return count
  53. end
  54.  
  55. table.size = function(tbl)
  56.     local count = 0
  57.     for _ in pairs(tbl) do count = count + 1 end
  58.     return count
  59. end
  60.  
  61. function round(val, decimal)
  62.   if (decimal) then
  63.     return math.floor( (val * 10^decimal) + 0.5) / (10^decimal)
  64.   else
  65.     return math.floor(val+0.5)
  66.   end
  67. end
  68.  
  69. function format_num(amount, decimal, prefix, neg_prefix)
  70.   local str_amount,  formatted, famount, remain
  71.  
  72.   decimal = decimal or 2  -- default 2 decimal places
  73.   neg_prefix = neg_prefix or "-" -- default negative sign
  74.  
  75.   famount = math.abs(round(amount,decimal))
  76.   famount = math.floor(famount)
  77.  
  78.   remain = round(math.abs(amount) - famount, decimal)
  79.  
  80.         -- comma to separate the thousands
  81.   formatted = comma_value(famount)
  82.  
  83.         -- attach the decimal portion
  84.   if (decimal > 0) then
  85.     remain = string.sub(tostring(remain),3)
  86.     formatted = formatted .. "." .. remain ..
  87.                 string.rep("0", decimal - string.len(remain))
  88.   end
  89.  
  90.         -- attach prefix string e.g '$'
  91.   formatted = (prefix or "") .. formatted
  92.  
  93.         -- if value is negative then format accordingly
  94.   if (amount<0) then
  95.     if (neg_prefix=="()") then
  96.       formatted = "("..formatted ..")"
  97.     else
  98.       formatted = neg_prefix .. formatted
  99.     end
  100.   end
  101.  
  102.   return formatted
  103. end
  104.  
  105. function debugPrint(str)
  106.   if isDebugging then
  107.     print(str)
  108.   end
  109. end
  110.  
  111. function findStr(mp, str)
  112.   for i,v in ipairs(mp) do
  113.     if string.match(v, str) then
  114.       return v
  115.     end
  116.   end
  117.   return nil
  118. end
  119.  
  120. function tableSize(tbl)
  121.   local n = 0
  122.   for i,v in pairs(tbl) do
  123.     n = n+1
  124.   end
  125.   return n
  126. end
  127.  
  128. function clearTable(tbl)
  129.   count = tableSize(tbl)
  130.   for i=0, count do
  131.     tbl[i]=nil
  132.   end
  133. end
  134.  
  135. function clear()
  136.   term.clear()
  137.   term.setCursorPos(1,1)
  138. end
  139.  
  140. function string:split()
  141.   local fields = {}
  142.   local pattern = string.format("([^%s]+)")
  143.   self:gsub(pattern, function(c) fields[#fields+1] = c end)
  144.   return fields
  145. end
  146.  
  147. function getStrungTable(tbl, pre, post)
  148.   if pre == nil then
  149.     pre = ""
  150.   end
  151.   if post == nil then
  152.     post = ""
  153.   end
  154.   local strs = {}
  155.   local n = 1
  156.   for k,v in pairs(tbl) do
  157.     strs[n] = tostring(pre)..tostring(k).."\t"..tostring(v)..tostring(post)
  158.     n=n+1
  159.   end
  160.   return strs
  161. end
  162.  
  163. local function getStrungFuncTable(tbl, pre, post)
  164.   if pre == nil then
  165.     pre = ""
  166.   end
  167.   if post == nil then
  168.     post = ""
  169.   end
  170.   local strs = {}
  171.   local n = 1
  172.   for k,v in pairs(tbl) do
  173.     debugPrint(pre..k..post)
  174.     strs[n] = pre..k..post
  175.     n=n+1
  176.   end
  177.   return strs
  178. end
  179.  
  180. function string:splitArr()
  181.   local fields = {}
  182.   local pattern = string.format("([^%s]+)")
  183.   self:gsub(pattern, function(c) fields[#fields+1] = c end)
  184.   return fields
  185. end
  186.  
  187. function split(str, pat)
  188.    local t = {}  -- NOTE: use {n = 0} in Lua-5.0
  189.    local fpat = "(.-)" .. pat
  190.    local last_end = 1
  191.    local s, e, cap = str:find(fpat, 1)
  192.    while s do
  193.       if s ~= 1 or cap ~= "" then
  194.          table.insert(t,cap)
  195.       end
  196.       last_end = e+1
  197.       s, e, cap = str:find(fpat, last_end)
  198.    end
  199.    if last_end <= #str then
  200.       cap = str:sub(last_end)
  201.       table.insert(t, cap)
  202.    end
  203.    return t
  204. end
  205.  
  206. table.slice = function(tbl, first, last, step)
  207.   local sliced = {}
  208.  
  209.   for i = first or 1, last or #tbl, step or 1 do
  210.     sliced[#sliced+1] = tbl[i]
  211.   end
  212.  
  213.   return sliced
  214. end
  215.  
  216. function printArgs(args)
  217.   local argstring = ""
  218.   for i,a in pairs(args) do
  219.     argstring = argstring .. a .. " "
  220.   end
  221.   print("Args: "..argstring)
  222. end
  223.  
  224. function printMap(map)
  225.   print("Debugging map:")
  226.   for i,a in pairs(map) do
  227.     print(i)
  228.   end
  229. end
  230.  
  231. function debugMap(map, pretext)
  232.   for i,a in pairs(map) do
  233.     print(tostring(i) .. "      " .. tostring(a))
  234.   end
  235. end
  236.  
  237. function longestString(list)
  238.   local mx = 0
  239.   local str = ""
  240.   debugPrint("<<<< longestString")
  241.   for i,v in ipairs(list) do
  242.     debugPrint(v..": "..#v)
  243.     if #v > mx then
  244.       mx = #v
  245.       str = v
  246.     end
  247.   end
  248.   return str
  249. end
  250.  
  251. function getPeripByName(str)
  252.   return peripheral.wrap(findStr(peripheral.getNames(), str))
  253. end
  254.  
  255. function setPeripForCache(name, perip)
  256.     PERIP_CACHE[name] = perip
  257.     return PERIP_CACHE[name]
  258. end
  259.  
  260. function peripFromCache(name)
  261.     if PERIP_CACHE[name] == nil then
  262.         PERIP_CACHE[name] = getPeripByName(name)
  263.     end
  264.     return PERIP_CACHE[name]
  265. end
  266.  
  267. local function localmon()
  268.     return peripFromCache("monitor")
  269. end
  270.  
  271. function clearMonitor(mon)
  272.   if mon == nil then
  273.     mon = localmon()
  274.   end
  275.   mon.clear()
  276.   mon.setCursorPos(1,1)
  277. end
  278.  
  279. function newLine(keepX, mon)
  280.   if mon == nil then
  281.     mon = localmon()
  282.   end
  283.   local x,y = mon.getCursorPos()
  284.   if keepX then
  285.     mon.setCursorPos(x,y+1)
  286.   else
  287.     mon.setCursorPos(1,y+1)
  288.   end
  289. end
  290.  
  291. function writeLine(str,keepX,mon)
  292.   if mon == nil then
  293.     mon = localmon()
  294.   end
  295.   local x,y = mon.getCursorPos()
  296.   mon.write(str)
  297.   if keepX then
  298.     mon.setCursorPos(x,y)
  299.   end
  300.   newLine(keepX, mon)
  301. end
  302.  
  303. function findInInv(inv, search)
  304.     for k,stack in pairs(inv.list()) do
  305.         local detail = inv.getItemDetail(k)
  306.         if string.match(detail.displayName, search) then
  307.             return k,stack
  308.         end
  309.     end
  310.     return nil
  311. end
  312.  
  313. function findInInvByNbt(inv, nbt)
  314.     for k,stack in pairs(inv.list()) do
  315.         local detail = inv.getItemDetail(k)
  316.         if detail.nbt == nbt then
  317.             return k,stack
  318.         end
  319.     end
  320.     return nil
  321. end
  322.  
  323. custColors = {
  324.   ["ui-grey"] = 0xd2d2d2,
  325.   ["black"] = 0x000000,
  326.   ["ui-red"] = 0xba3f3f,
  327.   ["white"] = 0xffffff,
  328.   ["navy"] = 0x213a63
  329. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement