Daniel_I_Am

Function Collection

Dec 19th, 2017
319
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.33 KB | None | 0 0
  1. --##################################################  Libraries
  2. io = require("io");os = require("os");component = require("component");term = require("term");sides = require("sides");colors = require("colors");fs = require("filesystem");gpu = component.gpu
  3.  
  4. io = require("io")
  5. os = require("os")
  6. component = require("component")
  7. term = require("term")
  8. sides = require("sides")
  9. colors = require("colors")
  10. fs = require("filesystem")
  11.  
  12. gpu = component.gpu
  13.  
  14. --##################################################  Debug
  15. function throwError(errorMessage)
  16.   gpu.setResolution(gpu.maxResolution())
  17.   gpu.setForeground(0xff0000)
  18.   gpu.setBackground(0x000000)
  19.   term.clear()
  20.   print("The program ran into a serious error during execution.")
  21.   print(errorMessage)
  22.   gpu.setForeground(0xffffff)
  23.   os.exit()
  24. end
  25.  
  26. function debugLog(str)
  27.     fileWrite("debug.log", str, false)
  28. end
  29.  
  30. --##################################################  GUIS
  31.  
  32. function addRect(x1,y1,x2,y2,color)
  33.   temp = gpu.getBackground()
  34.   gpu.setBackground(color)
  35.   gpu.fill(x1,y1,x2-x1+1,y2-y1+1," ")
  36.   gpu.setBackground(temp)
  37. end
  38.  
  39. function addProgressBar(x1,y1,x2,y2,sideWidth,sideHeight,borderColor,filledBarColor,emptyBarColor,textColor,headerText,filledPercentage,isSideways,isInverted)
  40.   isSideways = isSideways or false
  41.   isInverted = isInverted or false
  42.   if type(x1)~="number" then throwError("addProgressBar() argument 1 (x1) type int expected, "..type(x1).." found") end
  43.   if type(x2)~="number" then throwError("addProgressBar() argument 3 (x2) type int expected, "..type(x2).." found") end
  44.   if type(y1)~="number" then throwError("addProgressBar() argument 2 (y1) type int expected, "..type(y1).." found") end
  45.   if type(y2)~="number" then throwError("addProgressBar() argument 4 (y2) type int expected, "..type(y2).." found") end
  46.   if type(sideWidth)~="number" then throwError("addProgressBar() argument 5 (sideWidth) type int expected, "..type(sideWidth).." found") end
  47.   if type(sideHeight)~="number" then throwError("addProgressBar() argument 6 (sideHeight) type int expected, "..type(sideHeight).." found") end
  48.   if type(filledPercentage)~="number" then throwError("addProgressBar() argument 11 (filledPercentage) type float expected, "..type(filledPercentage).." found") end
  49.   if filledPercentage > 1 then filledPercentage = 1 end
  50.   if isSideways == false then
  51.     addRect(x1,y1,x2,y2,borderColor)
  52.     addRect(x1+sideWidth,y1+sideHeight,x2-sideWidth,y2-sideHeight,emptyBarColor)
  53.     barSize = (y2-y1-2*sideHeight)
  54.     pixelsFilled = math.floor(filledPercentage*barSize+0.5)
  55.     if isInverted == false then
  56.       addRect(x1+sideWidth,y2-sideHeight-pixelsFilled,x2-sideWidth,y2-sideHeight,filledBarColor)
  57.     elseif isInverted == true then
  58.       addRect(x1+sideWidth,y1+sideHeight,x2-sideWidth,y1+sideHeight+pixelsFilled,filledBarColor)
  59.     else
  60.       throwError("addProgressBar() argument 13 (isInverted) type boolean expected, "..type(isInverted).." found")
  61.     end
  62.   elseif isSideways == true then
  63.     addRect(x1,y1,x2,y2,borderColor)
  64.     addRect(x1+sideWidth,y1+sideHeight,x2-sideWidth,y2-sideHeight,emptyBarColor)
  65.     barSize = (x2-x1-2*sideWidth)
  66.     pixelsFilled = math.floor(filledPercentage*barSize+0.5)
  67.     if isInverted == false then
  68.       addRect(x1+sideWidth,y1+sideHeight,x1+sideWidth+pixelsFilled,y2-sideHeight,filledBarColor)
  69.     elseif isInverted == true then
  70.       addRect(x2-sideWidth-pixelsFilled,y1+sideHeight,x2-sideWidth,y2-sideHeight,filledBarColor)
  71.     else
  72.       throwError("addProgressBar() argument 13 (isInverted) type boolean expected, "..type(isInverted).." found")
  73.     end
  74.   else
  75.     throwError("addProgressBar() argument 12 (isSideways) type boolean expected, "..type(isSideways).." found")
  76.   end
  77.   if sideHeight>0 then
  78.     col = {f = gpu.getForeground(),b = gpu.getBackground()}
  79.     centerX = math.ceil((x1+x2)/2)
  80.     centerY = math.floor(y1+sideHeight/2)
  81.     gpu.setForeground(textColor)
  82.     gpu.setBackground(borderColor)
  83.     gpu.set(centerX-(string.len(tostring(headerText))/2),centerY,tostring(headerText))
  84.     gpu.setForeground(col.f)
  85.     gpu.setBackground(col.b)
  86.     centerX = nil
  87.     centerY = nil
  88.     col = nil
  89.   end
  90. end
  91.  
  92. function centerText(str, x1, x2, y, FGCol, BGCol)
  93.     local w,_ = gpu.getResolution()
  94.     x1 = x1 or 1
  95.     x2 = x2 or w
  96.     y = y or 1
  97.     str = str or "text"
  98.     FGCol = FGCol or 0xffffff
  99.     BGCol = BGCol or 0x000000
  100.     local prevFG = gpu.getForeground()
  101.     local prevBG = gpu.getBackground()
  102.     local l = string.len(tostring(str))
  103.     local center = math.floor((x1+x2)/2)
  104.     local startPos = math.ceil(center - (l-1)/2)
  105.     gpu.setForeground(FGCol)
  106.     gpu.setBackground(BGCol)
  107.     gpu.set(startPos,y,str)
  108.     gpu.setForeground(prevFG)
  109.     gpu.setBackground(prevBG)
  110. end
  111.  
  112. BA = require("ButtonAPI")
  113. function updateButtons(list, buttonsPerColumn, buttonXSize, buttonYSize, buttonXOffset, buttonYOffset, buttonColor, textColor, buttonXSpacing, buttonYSpacing)
  114.   BA.clear()
  115.   local list = listToPage(list,buttonsPerColumn)
  116.   for i = 1,#list do
  117.     for j = 1, #list[i] do
  118.       BA.create(buttonXOffset + (i-1) * (buttonXSpacing + buttonXSize), buttonYOffset + (j-1) * (buttonYSpacing + buttonYSize), buttonXSize, buttonYSize, buttonColor, textColor, -1, -1, tostring(list[i][j]))
  119.     end
  120.   end
  121.   BA.drawAll()
  122. end
  123.  
  124. --##################################################  File manipulation
  125. function fileRead(fileLocation)
  126.     local str = {}
  127.     if fs.exists("/home/"..fileLocation) then
  128.         f = io.open("/home/"..fileLocation, "r")
  129.         repeat
  130.             line = f:read("*line")
  131.             if line ~= nil then
  132.                 table.insert(str, line)
  133.             end
  134.         until line == nil
  135.     else
  136.         throwError("File /home/"..fileLocation.." not found!")
  137.     end
  138.     f:close()
  139.     f = nil
  140.     return str
  141. end
  142.  
  143. function fileWrite(fileLocation, str, isOverwrite)
  144.   if isOverwrite==true then mode = "w" else mode = "a" end
  145.   f = io.open("/home/"..fileLocation,mode)
  146.   f:write(tostring(str).."\n")
  147.   f:close()
  148. end
  149.  
  150. --##################################################  Timing
  151.  
  152. function displaySleepTime(y,toSleep, txtcol, bgcol, textToShowPre, textToShowPost)
  153.     repeat
  154.         centerText("   " .. textToShowPre .. " " .. math.floor(toSleep/60) .. " minutes and " .. toSleep%60 .. " seconds" .. textToShowPost .. "   ", 1, w, y, txtcol, bgcol)
  155.         toSleep = toSleep - 1
  156.         os.sleep(1)
  157.     until toSleep == -1
  158. end
  159.  
  160. --##################################################  Data Manipulation
  161. function listToPages(list,entriesPerPage)
  162.     pageList = {}
  163.     value = nil
  164.     tempList = {}
  165.    
  166.     for i = 1,#list,entriesPerPage do
  167.         tempList = {}
  168.         for j = 1,entriesPerPage do
  169.             value = table.remove(list, 1)
  170.             table.insert(tempList, value)
  171.         end
  172.         table.insert(pageList, tempList)
  173.     end
  174.     return pageList
  175. end
  176.  
  177. function comma_value(n) -- credit http://richard.warburton.it
  178.   local left,num,right = string.match(n,'^([^%d]*%d)(%d*)(.-)$')
  179.   return left..(num:reverse():gsub('(%d%d%d)','%1.'):reverse())..right
  180. end
  181.  
  182. function randomDigitNoRep(lowerBound, upperBound, numberOfNumbers)
  183.   lowerBound = lowerBound or 1
  184.   upperBound = upperBound or 10
  185.   numberOfNumbers = numberOfNumbers or 1
  186.   if upperBound - lowerBound + 1 < numberOfNumbers then print("wrong size"); return nil end
  187.   local hasBeenPicked = false
  188.   local list = {}
  189.   for i = 1, numberOfNumbers do
  190.     repeat
  191.       hasBeenPicked = false
  192.       n = math.random(lowerBound, upperBound)
  193.       for j = 1, #list do
  194.         if n == list[j] then
  195.           hasBeenPicked = true
  196.         end
  197.       end
  198.     until hasBeenPicked == false
  199.     table.insert(list, n)
  200.   end
  201.   return list
  202. end
  203.  
  204. function sortList(array, mode)
  205.   local arrayCopy = {}
  206.   for k,v in pairs(array) do
  207.     arrayCopy[k] = v
  208.   end
  209.  
  210.   mode = mode or "a"
  211.   if mode == "d" then
  212.     for i = 1, #arrayCopy do
  213.       for j = 1, #arrayCopy - i do
  214.         local a = arrayCopy[j]
  215.         local b = arrayCopy[j+1]
  216.         if a < b then
  217.           arrayCopy[j] = b
  218.           arrayCopy[j+1] = a
  219.         end
  220.       end
  221.     end
  222.     return arrayCopy
  223.   else
  224.     for i = 1, #arrayCopy do
  225.       for j = 1, #arrayCopy - i do
  226.         local a = arrayCopy[j]
  227.         local b = arrayCopy[j+1]
  228.         if a > b then
  229.           arrayCopy[j] = b
  230.           arrayCopy[j+1] = a
  231.         end
  232.       end
  233.     end
  234.     return arrayCopy
  235.   end
  236.   return nil
  237. end
  238.  
  239. function sortArray(array, mode)
  240.   local i = 0
  241.   local pointers = {}
  242.   local data = {}
  243.   local arrayCopy = {}
  244.   for k,v in pairs(array) do
  245.     arrayCopy[k] = v
  246.   end
  247.   for k,v in pairs(arrayCopy) do
  248.     pointers[i] = k
  249.     data[i] = v
  250.     i = i + 1
  251.   end
  252.   mode = mode or "a"
  253.   if mode == "d" then
  254.     for i = 0, #data do
  255.       for j = 0, #data - i - 1 do
  256.         local a = data[j]
  257.         local b = data[j+1]
  258.         local c = pointers[j]
  259.         local d = pointers[j+1]
  260.         if a < b then
  261.           data[j] = b
  262.           pointers[j] = d
  263.           data[j+1] = a
  264.           pointers[j+1] = c
  265.         end
  266.       end
  267.     end
  268.     return pointers, data
  269.   else
  270.     for i = 0, #data do
  271.       for j = 0, #data - i - 1 do
  272.         local a = data[j]
  273.         local b = data[j+1]
  274.         local c = pointers[j]
  275.         local d = pointers[j+1]
  276.         if a > b then
  277.           data[j] = b
  278.           pointers[j] = d
  279.           data[j+1] = a
  280.           pointers[j+1] = c
  281.         end
  282.       end
  283.     end
  284.     return pointers, data
  285.   end
  286.   return nil
  287. end
Advertisement
Add Comment
Please, Sign In to add comment