Advertisement
minecartchris

PrinterLib

Jul 4th, 2024
1,180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.77 KB | None | 0 0
  1. -- shell.run("wget http://127.0.0.1:8080/code/printer/printer.lua")
  2.  
  3. local lib = {}
  4. local printer
  5. local xp = 1
  6. local yp = 1
  7. local shouldNewPage = false
  8.  
  9. function lib.init(side)
  10.     printer = peripheral.wrap(side)
  11.     if not printer then return nil end
  12.     return true
  13. end
  14.  
  15. function lib.setPrinterObject(obj)
  16.     printer = obj
  17. end
  18.  
  19. function lib.getPrinterObject()
  20.     return printer
  21. end
  22.  
  23. function lib.setPos(x, y)
  24.     if x ~= nil then
  25.         xp = x
  26.     end
  27.     if y ~= nil then
  28.         yp = y
  29.     end
  30.     printer.setCursorPos(xp, yp)
  31. end
  32.  
  33. function lib.getPos()
  34.     return printer.getCursorPos()
  35. end
  36.  
  37. function lib.startPage()
  38.     xp = 1
  39.     yp = 1
  40.     local st = printer.newPage()
  41.     printer.setCursorPos(xp, yp)
  42.     shouldNewPage = false
  43.     return st
  44. end
  45.  
  46. function lib.endPage()
  47.     printer.endPage()
  48.     shouldNewPage = false
  49. end
  50.  
  51. function lib.write(text)
  52.     data = tostring(text)
  53.     for i = 1, #data do
  54.         lib.writeChar(data:sub(i, i))
  55.     end
  56. end
  57.  
  58. function lib.print(text)
  59.     lib.write(text)
  60.     lib.writeChar("\n")
  61. end
  62.  
  63. function lib.printCenter(text)
  64.     data = tostring(text)
  65.     xp = 13-(math.floor(#data/2))
  66.     lib.print(data)
  67. end
  68.  
  69. -------------------
  70.  
  71. function lib.writeChar(char)
  72.     if shouldNewPage then
  73.         local st = false
  74.         while st == false do
  75.             st = printer.newPage()
  76.         end
  77.         printer.setCursorPos(1, 1)
  78.         shouldNewPage = false
  79.     end
  80.     printer.setCursorPos(xp, yp)
  81.     if char == "\n" then
  82.         xp = 0
  83.         yp = yp + 1
  84.     else
  85.         printer.write(char)
  86.     end
  87.    
  88.     xp = xp + 1
  89.     if xp >= 26 then
  90.         xp = 1
  91.         yp = yp + 1
  92.     end
  93.     if yp >= 22 then
  94.         yp = 1
  95.         shouldNewPage = true
  96.     end
  97. end
  98.  
  99. return lib
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement