Advertisement
Guest User

printText.lua

a guest
Apr 22nd, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.66 KB | None | 0 0
  1. term.clear()
  2. local arg = {...}
  3. printer = peripheral.wrap(arg[1])
  4. io.input(arg[2])
  5. text = io.read('*all')
  6. spacesTable = {}
  7. table = {}
  8. printerTable = {}
  9.  
  10. function check()
  11.     assert(printer.newPage(),'please insert some paper!')
  12.     paperWidth, paperHeight = printer.getPageSize()
  13. end
  14.  
  15. function clearPage()
  16.     for i = 1, 25 do
  17.         for j = 1, 21 do
  18.             printer.setCursorPos(i,j)
  19.             printer.write(' ')
  20.         end
  21.     end
  22.     printer.setCursorPos(1,1)
  23. end
  24.  
  25. function printerTableFunction()
  26.     local space1 = 0
  27.     local l = 1
  28.     local k = 1
  29.     local i = 1
  30.     while (space1 ~= nil) do -- Makes a table with all of the spaces' location
  31.         space2 = space1
  32.         space1 = string.find(text,' ', space2+1)
  33.         spacesTable[i] = space2
  34.         i = i + 1
  35.     end
  36.    
  37.     repeat
  38.     repeat
  39.         table[l+1] = spacesTable[k]
  40.         k = k + 1
  41.     until ((spacesTable[k+1] == nil) or (spacesTable[k-1] -spacesTable[k]> paperWidth))
  42.     l = l + 1
  43.     until (spacesTable[k] == nil)
  44.     table[1] = 0
  45.    
  46.    
  47.     for i = 1, #table do
  48.         printerTable[i] = string.sub(text,table[i],table[i+1])
  49.         print(printerTable[i],table[i],table[i+1])
  50.     end
  51.     printerTable[#table] = string.sub(text,table[#table],string.len(text))
  52.     return printerTable
  53. end
  54.  
  55. check()
  56. clearPage()
  57. if paperWidth*paperHeight<string.len(text) then
  58.     print('This text file won\'t fit on this page!')
  59. else
  60.     for i, v in ipairs(printerTableFunction()) do
  61.         printer.write(v)
  62.         printer.setCursorPos(1,i+1)
  63.     end
  64. end
  65.  
  66. printer.endPage()
  67. print(text)
  68. --print(text)
  69. --print(paperWidth,paperHeight)
  70. --print(string.len(text))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement