Advertisement
Guest User

Untitled

a guest
Apr 27th, 2014
21
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.13 KB | None | 0 0
  1. local cmp = require("component")
  2. local fs = require("filesystem")
  3. local shell = require("shell")
  4. local args = shell.parse(...)
  5.  
  6. local printer = cmp.openprinter
  7. local paddr = printer.address
  8.  
  9. local printLines = {}
  10.  
  11. function splitAdd(ln)
  12.     local line
  13.     ln:gsub('(%s*)(%S+)',
  14.       function(spc, word)
  15.         local len = 0
  16.         if line then
  17.             len = printer.charCount(line)
  18.         end
  19.         len = len + #spc + #word
  20.          if not line or len > 29 then
  21.             table.insert(printLines, line)
  22.             line = word
  23.          else
  24.             line = line..spc..word
  25.          end
  26.       end
  27.     )
  28.     table.insert(printLines, line)
  29. end
  30.  
  31. function doPrint()
  32.     printer.setTitle(args[1])
  33.     for i=1, #printLines do
  34.         printer.writeln(printLines[i])
  35.     end
  36.     printer.print()
  37. end
  38.  
  39. if #args < 2 then
  40.     print("Usage: fileprinter [title] [filepath]")
  41. else
  42.     if not fs.exists(args[2]) then
  43.         print("File not found: "..args[2])
  44.     else
  45.         local file = io.open(args[2], "r")
  46.        
  47.        
  48.         for line in file:lines() do
  49.             --ln = line:gsub("(?:§[0-9a-fk-or])+","")
  50.             if printer.charCount(line) > 29 then
  51.                 splitAdd(line)
  52.             else
  53.                 table.insert(printLines, line)
  54.             end
  55.         end
  56.         doPrint()
  57.        
  58.     end
  59. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement