Guest User

printText.lua

a guest
Apr 25th, 2019
71
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --Setting up various variables that come into play
  2. term.clear()
  3. local arg = {...}
  4. printer = peripheral.wrap(arg[1])
  5. io.input(arg[2])
  6. text = io.read('*all')
  7. spacesTable = {}
  8. table = {}
  9. printerTable = {}
  10. page = {}
  11. paperWidth, paperHeight = 25, 21
  12.  
  13. --Checking the program is run with correct arguments
  14.  
  15. assert(#arg == 2,'usage: printText.lua (printer side) (file to print)')
  16.  
  17. --Checks if there is paper and starts a new page if there is
  18.  
  19. function check()
  20.     assert(printer.newPage(),'please insert some paper!')
  21. end
  22.  
  23. --Clears the page in case it had previous writing
  24.  
  25. function clearPage()
  26.     for i = 1, 25 do
  27.         for j = 1, 21 do
  28.             printer.setCursorPos(i,j)
  29.             printer.write(' ')
  30.         end
  31.     end
  32.     printer.setCursorPos(1,1)
  33. end
  34.  
  35. --Creates a table containing the text split into strings which are smaller than paperWidth and without cutting through words
  36.  
  37. function printerTableFunction()
  38.     local space1 = 0
  39.     local i = 1
  40.     local j = 1
  41.     local k = 1
  42.     while (space1 ~= nil) do -- Makes a table with all of the spaces' location
  43.         space2 = space1
  44.         space1 = string.find(text,' ', space2+1)
  45.         spacesTable[i] = space2
  46.         i = i + 1
  47.     end
  48.     table[1] = 0
  49.    
  50.     repeat -- Keeps reassigning the location of the next space to a table until the the distance between the new line and the last space is greater than 25, where it moves on to the next table index
  51.     repeat
  52.         table[j+1] = spacesTable[k]
  53.         k = k + 1
  54.     until ((spacesTable[k] == nil) or (spacesTable[k] - table[j] >= paperWidth))
  55.     j = j + 1
  56.     until (spacesTable[k] == nil)
  57.    
  58.     for i = 1, #table do -- Associates the table of space emplacements to string values in a table
  59.         printerTable[i] = string.sub(text,table[i]+1,table[i+1])
  60.     end
  61.     printerTable[#table] = string.sub(text,table[#table],string.len(text))
  62.     return printerTable
  63. end
  64.  
  65. --Quick check
  66.  
  67. printerTable = printerTableFunction()
  68.  
  69. for i, v in ipairs(printerTable) do
  70.     print(i,v)
  71.     os.sleep(0.5)
  72. end
  73.  
  74. --Distributes the strings over several pages
  75.  
  76. function pageDist()
  77.     for i = 1, math.ceil(#printerTable/21) do
  78.         page[i] = {}
  79.         local l = 1
  80.         repeat
  81.         page[i][l] = printerTable[(i-1)*21 + l]
  82.         l = l + 1
  83.         until printerTable[l] == nil or l > paperHeight
  84.     end
  85.     return page
  86. end
  87.  
  88. page = pageDist()
  89. print(#page)
  90.  
  91. --Main code, just writes the table to paper
  92.  
  93. for i = 1, #page do
  94.     os.sleep(1)
  95.     check()
  96.     clearPage()
  97.     for k, v in ipairs(page[i]) do
  98.         print(k,v)
  99.         printer.write(v)
  100.         printer.setCursorPos(1,k+1)
  101.         os.sleep(0.2)
  102.     end
  103.     printer.endPage()
  104. end
  105.  
  106. print(string.len(text))
RAW Paste Data