Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- BASE_CHAR_WIDTH = 5
- BASE_CHAR_HEIGHT = 8
- BASE_EXCL_WIDTH = 2
- -- Used to store 'default' peripherals to fall back on
- local PERIP_CACHE = {}
- string.starts = function(String,Start)
- return string.sub(String,1,string.len(Start))==Start
- end
- table.reduce = function (list, fn, init) -- Technically a table as with everything else
- local acc = init
- for k, v in ipairs(list) do
- if 1 == k and not init then
- acc = v
- else
- acc = fn(acc, v)
- end
- end
- return acc
- end
- table.sum = function(list)
- if #list == 0 then
- return 0
- end
- if #list == 1 then
- return list[1]
- end
- return table.reduce(list,
- function(a,b)
- return a+b
- end
- )
- end
- function comma_value(amount)
- local formatted = amount
- while true do
- formatted, k = string.gsub(formatted, "^(-?%d+)(%d%d%d)", '%1,%2')
- if (k==0) then
- break
- end
- end
- return formatted
- end
- function tablelength(T)
- local count = 0
- for _ in pairs(T) do count = count + 1 end
- return count
- end
- table.size = function(tbl)
- local count = 0
- for _ in pairs(tbl) do count = count + 1 end
- return count
- end
- function round(val, decimal)
- if (decimal) then
- return math.floor( (val * 10^decimal) + 0.5) / (10^decimal)
- else
- return math.floor(val+0.5)
- end
- end
- function format_num(amount, decimal, prefix, neg_prefix)
- local str_amount, formatted, famount, remain
- decimal = decimal or 2 -- default 2 decimal places
- neg_prefix = neg_prefix or "-" -- default negative sign
- famount = math.abs(round(amount,decimal))
- famount = math.floor(famount)
- remain = round(math.abs(amount) - famount, decimal)
- -- comma to separate the thousands
- formatted = comma_value(famount)
- -- attach the decimal portion
- if (decimal > 0) then
- remain = string.sub(tostring(remain),3)
- formatted = formatted .. "." .. remain ..
- string.rep("0", decimal - string.len(remain))
- end
- -- attach prefix string e.g '$'
- formatted = (prefix or "") .. formatted
- -- if value is negative then format accordingly
- if (amount<0) then
- if (neg_prefix=="()") then
- formatted = "("..formatted ..")"
- else
- formatted = neg_prefix .. formatted
- end
- end
- return formatted
- end
- function debugPrint(str)
- if isDebugging then
- print(str)
- end
- end
- function findStr(mp, str)
- for i,v in ipairs(mp) do
- if string.match(v, str) then
- return v
- end
- end
- return nil
- end
- function tableSize(tbl)
- local n = 0
- for i,v in pairs(tbl) do
- n = n+1
- end
- return n
- end
- function clearTable(tbl)
- count = tableSize(tbl)
- for i=0, count do
- tbl[i]=nil
- end
- end
- function clear()
- term.clear()
- term.setCursorPos(1,1)
- end
- function string:split()
- local fields = {}
- local pattern = string.format("([^%s]+)")
- self:gsub(pattern, function(c) fields[#fields+1] = c end)
- return fields
- end
- function getStrungTable(tbl, pre, post)
- if pre == nil then
- pre = ""
- end
- if post == nil then
- post = ""
- end
- local strs = {}
- local n = 1
- for k,v in pairs(tbl) do
- strs[n] = tostring(pre)..tostring(k).."\t"..tostring(v)..tostring(post)
- n=n+1
- end
- return strs
- end
- local function getStrungFuncTable(tbl, pre, post)
- if pre == nil then
- pre = ""
- end
- if post == nil then
- post = ""
- end
- local strs = {}
- local n = 1
- for k,v in pairs(tbl) do
- debugPrint(pre..k..post)
- strs[n] = pre..k..post
- n=n+1
- end
- return strs
- end
- function string:splitArr()
- local fields = {}
- local pattern = string.format("([^%s]+)")
- self:gsub(pattern, function(c) fields[#fields+1] = c end)
- return fields
- end
- function split(str, pat)
- local t = {} -- NOTE: use {n = 0} in Lua-5.0
- local fpat = "(.-)" .. pat
- local last_end = 1
- local s, e, cap = str:find(fpat, 1)
- while s do
- if s ~= 1 or cap ~= "" then
- table.insert(t,cap)
- end
- last_end = e+1
- s, e, cap = str:find(fpat, last_end)
- end
- if last_end <= #str then
- cap = str:sub(last_end)
- table.insert(t, cap)
- end
- return t
- end
- table.slice = function(tbl, first, last, step)
- local sliced = {}
- for i = first or 1, last or #tbl, step or 1 do
- sliced[#sliced+1] = tbl[i]
- end
- return sliced
- end
- function printArgs(args)
- local argstring = ""
- for i,a in pairs(args) do
- argstring = argstring .. a .. " "
- end
- print("Args: "..argstring)
- end
- function printMap(map)
- print("Debugging map:")
- for i,a in pairs(map) do
- print(i)
- end
- end
- function debugMap(map, pretext)
- for i,a in pairs(map) do
- print(tostring(i) .. " " .. tostring(a))
- end
- end
- function longestString(list)
- local mx = 0
- local str = ""
- debugPrint("<<<< longestString")
- for i,v in ipairs(list) do
- debugPrint(v..": "..#v)
- if #v > mx then
- mx = #v
- str = v
- end
- end
- return str
- end
- function getPeripByName(str)
- return peripheral.wrap(findStr(peripheral.getNames(), str))
- end
- function setPeripForCache(name, perip)
- PERIP_CACHE[name] = perip
- return PERIP_CACHE[name]
- end
- function peripFromCache(name)
- if PERIP_CACHE[name] == nil then
- PERIP_CACHE[name] = getPeripByName(name)
- end
- return PERIP_CACHE[name]
- end
- local function localmon()
- return peripFromCache("monitor")
- end
- function clearMonitor(mon)
- if mon == nil then
- mon = localmon()
- end
- mon.clear()
- mon.setCursorPos(1,1)
- end
- function newLine(keepX, mon)
- if mon == nil then
- mon = localmon()
- end
- local x,y = mon.getCursorPos()
- if keepX then
- mon.setCursorPos(x,y+1)
- else
- mon.setCursorPos(1,y+1)
- end
- end
- function writeLine(str,keepX,mon)
- if mon == nil then
- mon = localmon()
- end
- local x,y = mon.getCursorPos()
- mon.write(str)
- if keepX then
- mon.setCursorPos(x,y)
- end
- newLine(keepX, mon)
- end
- function findInInv(inv, search)
- for k,stack in pairs(inv.list()) do
- local detail = inv.getItemDetail(k)
- if string.match(detail.displayName, search) then
- return k,stack
- end
- end
- return nil
- end
- function findInInvByNbt(inv, nbt)
- for k,stack in pairs(inv.list()) do
- local detail = inv.getItemDetail(k)
- if detail.nbt == nbt then
- return k,stack
- end
- end
- return nil
- end
- custColors = {
- ["ui-grey"] = 0xd2d2d2,
- ["black"] = 0x000000,
- ["ui-red"] = 0xba3f3f,
- ["white"] = 0xffffff,
- ["navy"] = 0x213a63
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement