QuickMuffin8783

Fix of other program

Aug 10th, 2020
445
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- API SECTION!! DO NOT EDIT OR IT WILL BREAK! --
  2.  
  3.  
  4. local number = {}
  5.  
  6. function number.round(num)
  7.     if num >= 0 then
  8.         return math.floor(num + 0.5)
  9.     else
  10.         return math.ceil(num - 0.5)
  11.     end
  12. end
  13.  
  14. function number.roundToDecimalPlaces(num, decimalPlaces)
  15.     local mult = 10 ^ (decimalPlaces or 0)
  16.     return number.round(num * mult) / mult
  17. end
  18.  
  19. function number.getDigitCount(num)
  20.     return num == 0 and 1 or math.ceil(math.log(num + 1, 10))
  21. end
  22.  
  23. function number.shorten(num, digitCount)
  24. --[[
  25. Here is why it is 1024.
  26. Computers in real life use this standard, and it is widely used
  27. in all of our operating systems.
  28. You can search if a kilobyte is 1024 bytes to make sure.
  29. ]]
  30.     if num < 1024 then
  31.         return num
  32.     else
  33.         local shortcuts = { "KB", "MB", "GB", "TB", "PT", "ET", "ZT", "YB" } -- Shortcuts of all the stoarge formats
  34.         local index = math.floor(math.log(num, 1024))
  35.  
  36.         return number.roundToDecimalPlaces(num / 1024 ^ index, digitCount) .. shortcuts[index]
  37.     end
  38. end
  39.  
  40. local text = {}
  41.  
  42. function text.wrap(data, limit) -- Function to use for text wrapping
  43.     if type(data) == "string" then
  44.         data = { data }
  45.     end
  46.  
  47.     local wrappedLines, result, preResult, position = {}
  48.  
  49.     for i = 1, #data do
  50.         wrappedLines[i] = data[i]
  51.     end
  52.  
  53.     local i = 1
  54.     while i <= #wrappedLines do
  55.         local position = wrappedLines[i]:find("\n")
  56.         if position then
  57.             table.insert(wrappedLines, i + 1, string.sub(wrappedLines[i], position + 1, -1))
  58.             wrappedLines[i] = string.sub(wrappedLines[i], 1, position - 1)
  59.         end
  60.  
  61.         i = i + 1
  62.     end
  63.     local i = 1
  64.     while i <= #wrappedLines do
  65.         result = ""
  66.  
  67.         for word in wrappedLines[i]:gmatch("[^%s]+") do
  68.             preResult = result .. word
  69.  
  70.             if string.len(preResult) > limit then
  71.                 if unicode.len(word) > limit then
  72.                     table.insert(wrappedLines, i + 1, string.sub(wrappedLines[i], limit + 1, -1))
  73.                     result = string.sub(wrappedLines[i], 1, limit)
  74.                 else
  75.                     table.insert(wrappedLines, i + 1, steing.sub(wrappedLines[i], string.len(result) + 1, -1))
  76.                 end
  77.  
  78.                 break
  79.             else
  80.                 result = preResult .. " "
  81.             end
  82.         end
  83.  
  84.         wrappedLines[i] = result:gsub("%s+$", ""):gsub("^%s+", "")
  85.  
  86.         i = i + 1
  87.     end
  88.  
  89.     return wrappedLines
  90. end
  91.  
  92. Monitor = {}
  93. Monitor.__index = Monitor
  94.  
  95. function Monitor:new(side)
  96.     if peripheral.getType(side) ~= "monitor" then
  97.         error("ERROR: This peripheral is not a monitor! It's a " .. monitor.getType())
  98.     end
  99.     local monitor = {}
  100.     local monMain = {} -- To make sure the system works.
  101. --  print(type(peripheral))
  102. --  print(getmetatable(peripheral))
  103.     local mainTable = peripheral.wrap(side)
  104.     setmetatable(monMain, mainTable)
  105.     setmetatable(monitor, mainTable)
  106.     monMain.side = side
  107.     monitor.side = monMain.side
  108.     monitor.curX = 1
  109.     monitor.curY = 1
  110.     monitor.backColor = colors.black
  111.     monitor.textColor = colors.white
  112.     monitor.isBlinking = true
  113.  
  114. function monitor.getCursorPos()
  115.     self.curX, self.curY = monMain.term.getCursorPos()
  116.     return monMain.term.getCursorPos()
  117. end
  118.  
  119. function monitor.setCursorPos(x, y)
  120.     self.curX = x
  121.     self.curY = y
  122. end
  123.  
  124. function monitor.setCursorBlink(bool)
  125.     self.isBlinking = bool
  126. end
  127.  
  128. function monitor.setSizeScale(var1)
  129.     monMain.setTextScale(4 * (1 - (0 - (var1 / 10))))
  130.     monMain.redraw()
  131. end
  132.  
  133. function monitor.write(txt, doWrap)
  134.     if not txt == nil then
  135.         monMain.term.setCursorPos(self.curX, self.curY)
  136.         monMain.setCursorBlink(false)
  137.         if not doWrap then
  138.             for i = 1, txt:len() do
  139.                 monMain.write(txt:gsub(i, i))
  140.                 monMain.term.setCursorPos(self.curX+1, self.curY)
  141.             end
  142.         else
  143.             monMain.write(txt)
  144.         end
  145.     end
  146.     monMain.term.setCursorBlink(self.isBlinking)
  147. end
  148.  
  149. function monitor.print(txt, doWrap)
  150.     if not txt == nil then
  151.         monMain.term.setCursorBlink(false)
  152.         if not doWrap then
  153.             for i = 1, txt:len() do
  154.                 monMain.write(txt:gsub(i, i))
  155.                 monMain.term.setCursorPos(self.curX+1, self.curY)
  156.             end
  157.         else
  158.             monMain.print(txt)
  159.         end
  160.     end
  161.     monMain.term.setCursorBlink(self.isBlinking)
  162. end
  163.  
  164. function monitor.getType()
  165.     return peripheral.getType(self.side)
  166. end
  167.  
  168. function monitor.newLine()
  169.     x,y = self.getCursorPos()
  170.     self.setCursorPos(1,y+1)
  171. end
  172.  
  173.    monMain.term.clear()
  174.    monMain.term.setCursorPos(1,1)
  175.    return monitor
  176. end
  177.  
  178.  
  179. ----------- START OF PROGRAM -----------
  180. local mon = Monitor.new("top")
Add Comment
Please, Sign In to add comment