Advertisement
Guest User

Untitled

a guest
Aug 19th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 11.95 KB | None | 0 0
  1. --STD addon by Microeinstein
  2. --rPrint is not mine, but I modified it a bit
  3.  
  4. __std = {}
  5.  
  6. function term.wash()
  7.     term.setBackgroundColor(colors.black)
  8.     term.setTextColor(colors.white)
  9.     term.clear()
  10.     term.setCursorPos(1, 1)
  11. end
  12. function term.input(text, prefix)
  13.     prefix = prefix or ""
  14.     term.write(text..prefix)
  15.     return prefix..io.read()
  16. end
  17. function term.lineBefore()
  18.     local w, h = term.getSize()
  19.     print(string.rep(" ", w))
  20.     local x, y = term.getCursorPos()
  21.     term.setCursorPos(x, y - 1)
  22. end
  23. function term.printc(fg, bg, ...)
  24.     if term.isColor() then
  25.         local fc, bc = term.getTextColor(), term.getBackgroundColor()
  26.         term.setTextColor(fg or fc)
  27.         term.setBackgroundColor(bg or bc)
  28.         print(unpack(arg))
  29.         term.setTextColor(fc)
  30.         term.setBackgroundColor(bc)
  31.     else
  32.         print(unpack(arg))
  33.     end
  34.     term.lineBefore()
  35. end
  36. --[[function term.printec(str)
  37.     local p = nil
  38.     local l = nil
  39.     repeat
  40.         p, l = string.find(str, "%e%d+m")
  41.         if p ~= nil then
  42.             if p > 1 then
  43.                 print(string.sub(str, 1, p + 1))
  44.                 term.lineBefore()
  45.             end
  46.             local color = tonumber(string.sub(str, p + 3, l - 1))
  47.             if term.isColor() then
  48.                 term.setTextColor(color)
  49.             end
  50.             str = string.sub(str, 1, p + l)
  51.         else
  52.             print(str)
  53.             term.lineBefore()
  54.         end
  55.     until p == nil
  56. end]]
  57. function term.log(pause, ...)
  58.     local oldX, oldY = term.getCursorPos()
  59.     local oldF, oldB = term.getTextColor(), term.getBackgroundColor()
  60.     local sx, sy = term.getSize()
  61.    
  62.     paintutils.drawLine(1, sy, sx, sy, colors.black)
  63.     term.setCursorPos(1, sy)
  64.     term.setTextColor(colors.white)
  65.     term.setBackgroundColor(colors.black)
  66.    
  67.     local txt = table.concat(table.allToString(arg), " ")
  68.     term.write(#txt > sx and string.negSub(txt, 1, #txt - sx) or txt)
  69.     if pause then
  70.         term.pause("", true)
  71.     end
  72.    
  73.     --paintutils.drawLine(1, sy, sx, sy, colors.black)
  74.     term.setCursorPos(oldX, oldY)
  75.     term.setTextColor(oldF)
  76.     term.setBackgroundColor(oldB)
  77. end
  78. function term.pause(text, writeMode)
  79.     if not writeMode then
  80.         print(tostring(text or "..."))
  81.         term.lineBefore()
  82.     else
  83.         term.write(tostring(text or "..."))
  84.     end
  85.     os.pullEvent("key")
  86. end
  87. function term.pausec(fg, bg, text)
  88.     if term.isColor() then
  89.         local fc, bc = term.getTextColor(), term.getBackgroundColor()
  90.         term.setTextColor(fg or fc)
  91.         term.setBackgroundColor(bg or bc)
  92.         term.pause(text, true)
  93.         term.setTextColor(fc)
  94.         term.setBackgroundColor(bc)
  95.     else
  96.         term.pause(text, true)
  97.     end
  98.     print("")
  99.     term.lineBefore()
  100. end
  101. function term.more(text)
  102.     term.wash()
  103.     local lns = string.split(text, "\n")
  104.     for k, l in pairs(lns) do
  105.         print(l);
  106.         if string.isBlank(l) then
  107.             term.log(true, "--More--")
  108.             term.log(false, "")
  109.         end
  110.     end
  111. end
  112.  
  113. fs.mex = {
  114.     okRead      = "File read",
  115.     okWrite     = "File saved",
  116.     genericErr  = "Unable to open file",
  117.     notFound    = "File or directory not found",
  118.     dirNotExp   = "This is a directory",
  119.     fileNotExp  = "This is not a directory",
  120. }
  121. function fs.read(path)
  122.     if not fs.exists(path) then
  123.         return fs.mex.notFound
  124.     end
  125.     if fs.isDir(path) then
  126.         return fs.mex.dirNotExp
  127.     end
  128.    
  129.     local file = fs.open(path, "r")
  130.     if not file then
  131.         return fs.mex.genericErr
  132.     end
  133.    
  134.     local cont = file.readAll();
  135.     file.close()
  136.    
  137.     return fs.mex.okRead, cont
  138. end
  139. function fs.write(path, content)
  140.     if fs.isDir(path) then
  141.         return fs.mex.dirNotExp
  142.     end
  143.    
  144.     local file = fs.open(path, "w")
  145.     if not file then
  146.         return fs.mex.genericErr
  147.     end
  148.    
  149.     file.write(content)
  150.     file.flush()
  151.     file.close()
  152.    
  153.     return fs.mex.okWrite
  154. end
  155. function fs.readDir(path, recursive)
  156.     if not fs.exists(path) then
  157.         return fs.mex.notFound
  158.     end
  159.     if not fs.isDir(path) then
  160.         return fs.mex.dirNotExp
  161.     end
  162.    
  163.     local list = fs.list(path)
  164.     local files = {}
  165.    
  166.     for _, f in pairs(list) do
  167.         local p = fs.combine(path, f)
  168.         if fs.isDir(p) then
  169.             if recursive then
  170.                 local mex, obj = fs.readDir(p, recursive)
  171.                 if mex == fs.mex.okRead then
  172.                     for rp, rc in pairs(obj) do
  173.                         files[rp] = rc
  174.                     end
  175.                 end
  176.             end
  177.         else
  178.             local mex, cont = fs.read(p)
  179.             if mex == fs.mex.okRead then
  180.                 files[p] = cont
  181.             else
  182.                 return mex, p
  183.             end
  184.         end
  185.     end
  186.    
  187.     return fs.mex.okRead, files
  188. end
  189.  
  190. function math.round(num)
  191.     return math.floor(num + .5)
  192. end
  193. function math.sum(tab, i)
  194.     local sum = 0
  195.     for j=1, i do
  196.         sum = sum + tab[j]
  197.     end
  198.     return sum
  199. end
  200. function math.center(p1, p2, length)
  201.     return math.floor((p1 + p2) / 2) - math.floor(length / 2)
  202. end
  203. function math.between(nMin, nV, nMax)
  204.     return math.max(nMin, math.min(nV, nMax))
  205. end
  206. function math.numToXY(num, nMax)
  207.     local tY = math.floor((num - 1) / nMax)
  208.     return {
  209.         x = num - (tY * nMax),
  210.         y = tY + 1
  211.     }
  212. end
  213. function math.xyToNum(x, y, nMax)
  214.     return x + (y - 1) * nMax
  215. end
  216.  
  217. function string.split(self, sep, esc)
  218.     if type(sep) == "number" then
  219.         if self then
  220.             sep = math.between(0, sep, #self)
  221.             return self:sub(1, sep), self:sub(sep + 1, #self)
  222.         else
  223.             return "", ""
  224.         end
  225.     elseif type(sep) == "string" then
  226.         if self then
  227.             local res = {}
  228.             local escape = false
  229.             local sepM, sepP, last = 0, 0, 1
  230.             for i = 1, #self do
  231.                 local c = self:sub(i, i)
  232.                 sepP = sepM + 1
  233.                 if esc ~= nil and type(esc) == "string" and sepM == 0 and not escape and c == esc then
  234.                     escape = true
  235.                 elseif c == sep:sub(sepP, sepP) then
  236.                     sepM = sepP
  237.                     if sepM == #sep then
  238.                         if not escape then
  239.                             local s = self:sub(last, i - #sep)
  240.                             table.insert(res, s)
  241.                             last = i + 1
  242.                         end
  243.                         escape = false
  244.                         sepM = 0
  245.                     end
  246.                 else
  247.                     escape = false
  248.                     sepM = 0
  249.                 end
  250.             end
  251.             local s = self:sub(last, #self)
  252.             table.insert(res, s)
  253.             return res
  254.         else
  255.             return {}
  256.         end
  257.     end
  258. end
  259. function string.replace(self, strF, strR, esc)
  260.     local res = {}
  261.     local escape = false
  262.     local sepM, sepP, ret = 0, 0, ""
  263.     for i = 1, #self do
  264.         local c = self:sub(i, i)
  265.         sepP = sepM + 1
  266.         if esc ~= nil and type(esc) == "string" and sepM == 0 and not escape and c == esc then
  267.             escape = true
  268.         elseif c == strF:sub(sepP, sepP) then
  269.             sepM = sepP
  270.             if sepM == #strF then
  271.                 if not escape then
  272.                     ret = ret .. strR
  273.                 else
  274.                     ret = ret .. self:sub(i - sepP, i)
  275.                 end
  276.                 escape = false
  277.                 sepM = 0
  278.             end
  279.         else
  280.             ret = ret .. self:sub(i - sepM, i)
  281.             escape = false
  282.             sepM = 0
  283.         end
  284.     end
  285.     return ret
  286. end
  287. function string.remChars(self, chrs, esc)
  288.     for i, c in pairs(string.chars(chrs)) do
  289.         self = string.replace(self, c, "", esc)
  290.     end
  291.     return self
  292. end
  293. function string.overwrite(self, str, from)
  294.     from = (from or 0) + 1
  295.     local slfC = string.chars(self)
  296.     local strC = string.chars(str)
  297.     local ret = ""
  298.     local frmE = from + #strC
  299.     for i = 1, #slfC do
  300.         if i >= from and i < frmE then
  301.             ret = ret .. strC[i - from + 1]
  302.         else
  303.             ret = ret .. slfC[i]
  304.         end
  305.     end
  306.     return ret
  307. end
  308. function string.chars(self)
  309.     local ch = {}
  310.     for i = 1, #self do
  311.         table.insert(ch, self:sub(i, i))
  312.     end
  313.     return ch
  314. end
  315. function string.contains(self, str)
  316.     if self and str then
  317.         local sepM, sepP = 0, 0
  318.         for i = 1, #self do
  319.             local c = self:sub(i, i)
  320.             sepP = sepM + 1
  321.             if c == str:sub(sepP, sepP) then
  322.                 sepM = sepP
  323.                 if sepM == #str then
  324.                     return true
  325.                 end
  326.             else
  327.                 sepM = 0
  328.             end
  329.         end
  330.         return false
  331.     else
  332.         return false
  333.     end
  334. end
  335. function string.firstUpper(self)
  336.     return string.upper(string.chars(self)[1]) .. string.sub(self, 2)
  337. end
  338. function string.lenX(self)
  339.     local m = -1
  340.     for i, l in pairs(string.split(self, "\n")) do
  341.         if m == -1 then
  342.             m = #l
  343.         else
  344.             m = math.max(m, #l)
  345.         end
  346.     end
  347.     return m
  348. end
  349. function string.lenY(self)
  350.     return table.len(string.split(self, "\n"))
  351. end
  352. function string.isBlank(self)
  353.     return self == nil or #self == 0 or string.match(self, [[^%s+$]]) ~= nil
  354. end
  355. function string.negSub(self, from, to)
  356.     local str = ""
  357.     for i, c in pairs(string.chars(self)) do
  358.         if i < from or i > to then
  359.             str = str .. c
  360.         end
  361.     end
  362.     return str
  363. end
  364. function string.multimatch(self, ...)
  365.     local resS, resP = {}, {}
  366.     local noFind, first, temp, found, mi, mk
  367.     local last = 1
  368.    
  369.     repeat
  370.         noFind = true
  371.         temp = {}
  372.         for kp, pat in pairs(arg) do
  373.             found = {string.find(self, pat, last)}
  374.             --{start, end, matches...} or {nil}
  375.             if #found > 0 then
  376.                 noFind = false
  377.                 --add which pattern is found
  378.                 found.pat = kp
  379.                 table.insert(temp, found)
  380.             end
  381.         end
  382.         if #temp > 0 then
  383.             if #temp == 1 then
  384.                 first = temp[1]
  385.             else
  386.                 mk = 1
  387.                 mi = temp[1][1]
  388.                 for k, pat in pairs(temp) do
  389.                     --find which pattern has lower start index
  390.                     if k > 1 and pat[1] < mi then
  391.                         mi = pat[1]
  392.                         mk = k
  393.                     end
  394.                 end
  395.                 first = temp[mk]
  396.             end
  397.             last = first[2] + 1
  398.             table.insert(resS, string.sub(self, first[1], first[2]))
  399.             table.insert(resP, first.pat)
  400.         end
  401.     until noFind
  402.    
  403.     --[[
  404.     Example:
  405.     {
  406.         {"A", "B", "C"},
  407.         {1, 2, 1} (first, second and first pattern found)
  408.     }
  409.     ]]
  410.     return {resS, resP}
  411. end
  412.  
  413. --[[Sample Lambda Functions
  414. table.first(
  415.     table,
  416.     function(key, value, custom1, custom2, ...)
  417.         return key == custom1 and value[1] == custom2
  418.     end,
  419.     myKey,
  420.     myValue)
  421. ]]
  422. function table.len(self, includeNil)
  423.     includeNil = includeNil or false
  424.     local c = 0
  425.     for k, v in pairs(self) do
  426.         if v or includeNil then
  427.             c = c + 1
  428.         end
  429.     end
  430.     return c
  431. end
  432. function table.copy(self, deep) --from http://lua-users.org/wiki/CopyTable shallow + deep
  433.     deep = deep or false
  434.     local orig_type = type(self)
  435.     local copy
  436.     if orig_type == 'table' then
  437.         copy = {}
  438.         for orig_key, orig_value in pairs(self) do
  439.             if deep then
  440.                 copy[table.copy(orig_key, true)] = table.copy(orig_value, true)
  441.             else
  442.                 copy[orig_key] = orig_value
  443.             end
  444.         end
  445.         if deep then
  446.             setmetatable(copy, table.copy(getmetatable(self)))
  447.         end
  448.     else
  449.         copy = self
  450.     end
  451.     return copy
  452. end
  453. function table.reverse(self, noString)
  454.     noString = noString or false
  455.     local indexed, reversed = {}, {}
  456.     local n = 0
  457.     for k, v in pairs(self) do
  458.         if noString or type(k) == "number" then
  459.             n = n + 1
  460.             indexed[n] = v
  461.         else
  462.             indexed[k] = v
  463.         end
  464.     end
  465.     local l = #indexed
  466.     for i, v in pairs(indexed) do
  467.         if noString or type(i) == "number" then
  468.             reversed[l - i + 1] = v
  469.         else
  470.             reversed[i] = v
  471.         end
  472.     end
  473.     return reversed
  474. end
  475. function table.cycle(array, i, inc)
  476.     if not array then return; end
  477.     local l = #array
  478.    
  479.     if l < 1 then
  480.         return
  481.     elseif l == 1 then
  482.         return 1
  483.     else
  484.         local r = (i or 0) + (inc or 1)
  485.         if r > l then
  486.             r = r - (l * math.floor(r / l))
  487.         end
  488.         return r
  489.     end
  490. end
  491. function table.exist(self, lambda, ...)
  492.     for k, v in pairs(self) do
  493.         if lambda(k, v, unpack(arg)) then
  494.             return true
  495.         end
  496.     end
  497.     return false
  498. end
  499. function table.first(self, lambda, ...)
  500.     for k, v in pairs(self) do
  501.         if lambda(k, v, unpack(arg)) then
  502.             return k, v
  503.         end
  504.     end
  505. end
  506. function table.last(self, lambda, ...)
  507.     local a, b
  508.     for k, v in pairs(self) do
  509.         if lambda(k, v, unpack(arg)) then
  510.             a, b = k, v
  511.         end
  512.     end
  513.     return a, b
  514. end
  515. function table.where(self, lambda, ...)
  516.     local ret = {}
  517.     for k, v in pairs(self) do
  518.         if lambda(k, v, unpack(arg)) then
  519.             ret[k] = v
  520.         end
  521.     end
  522.     return table.len(ret) > 0 and ret or nil
  523. end
  524. function table.allToString(self)
  525.     if not self then
  526.         return {"<all nil>"}
  527.     end
  528.     for k, v in pairs(self) do
  529.         self[k] = tostring(v)
  530.     end
  531.     return self
  532. end
  533.  
  534. function rPrint(s, d, i)        -- recursive Print (structure, maxDepth, indent)
  535.     d = d or 10;
  536.     i = i or "";                -- default item limit, indent string
  537.    
  538.     if d < 0 then
  539.         return
  540.     end
  541.     local ts = type(s)
  542.     if ts ~= "table" then
  543.         term.log(true, i, tostring(s))
  544.         return
  545.     end
  546.     term.log(true, i, ts)
  547.     for k, v in pairs(s) do     -- print "[KEY] VALUE"
  548.         if v ~= s then
  549.             rPrint(v, d - 1, i .. " " .. tostring(k))
  550.             if d < 0 then
  551.                 break
  552.             end
  553.         end
  554.     end
  555. end
  556. function objDebug(obj, prefix, depth)
  557.     rPrint(obj, depth, prefix)
  558.     term.log(false, "OBJECT END")
  559.     os.sleep(0.35)
  560. end
  561. function toInt(str)
  562.     return tonumber(string.remChars(str:upper(), ".,xABCDEF")) or 0
  563. end
  564. function isBool(a)
  565.     return not not a == a
  566. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement