dealingwith

printer

Jan 24th, 2021 (edited)
420
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.01 KB | None | 0 0
  1. local function printUsage()
  2.   print( "Usage:" )
  3.   print( "print <pastebin code> <side>" )
  4. end
  5.  
  6. local tArgs = { ... }
  7. if #tArgs < 2 then
  8.   printUsage()
  9.   return
  10. end
  11.  
  12. if not http then
  13.   printError( "Pastebin requires http API" )
  14.   printError( "Set http_enable to true in ComputerCraft.cfg" )
  15.   return
  16. end
  17.  
  18. local function get(paste)
  19.   write( "Connecting to pastebin.com... " )
  20.   local response = http.get(
  21.       "http://pastebin.com/raw/"..textutils.urlEncode( paste )
  22.   )
  23.      
  24.   if response then
  25.       print( "Success." )
  26.      
  27.       local sResponse = response.readAll()
  28.       response.close()
  29.       return sResponse
  30.   else
  31.       print( "Failed." )
  32.   end
  33. end
  34.  
  35. function mysplit (inputstr)
  36.   local t = {}
  37.   for str in string.gmatch(inputstr, "([^%s]+)") do
  38.     table.insert(t, str)
  39.   end
  40.   return t
  41. end
  42.  
  43. -- get the string to print from pastebin
  44. sCopyToPrint = get(tArgs[1])
  45.  
  46. -- attach to the printer
  47. p = peripheral.wrap(tArgs[2])
  48.  
  49. -- loads input string into an array where each element of the array is one word
  50. local tWords = mysplit(sCopyToPrint)
  51.  
  52. local iLineLengthCounter = 0
  53. local sLineText = ""
  54. local iLineCount = 1
  55.  
  56. -- print a page
  57. for i=1,21,1 do
  58.   -- create a new page
  59.   p.newPage()
  60.   -- reset line count
  61.   iLineCount = 1
  62.   -- print each line
  63.   for i, v in ipairs(tWords) do
  64.     -- how long will the current line be if we add this string to it?
  65.     iLineLengthCounter = iLineLengthCounter + string.len(v) + 1
  66.     -- if under 25 chars, we're safe to continue the current line
  67.     if iLineLengthCounter < 25 then
  68.       sLineText = sLineText .. v .. " "
  69.     -- else, print the current line and add this string to the next line
  70.     else
  71.       p.write(sLineText)
  72.       -- put current word on next line
  73.       sLineText = v .. " "
  74.       -- increment the line counter
  75.       iLineLengthCounter = string.len(v) + 1
  76.       -- set cursor on next line
  77.       iLineCount = iLineCount + 1
  78.       p.setCursorPos(1, iLineCount)
  79.     end
  80.   end
  81.   -- this will print the page out
  82.   p.endPage()
  83. end
Add Comment
Please, Sign In to add comment