Advertisement
Techokami

print.lua

May 21st, 2014
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.27 KB | None | 0 0
  1. --A very simplistic printing program.
  2.  
  3. local component = require("component")
  4. local computer = require("computer")
  5.  
  6. if not component.isAvailable("openprinter") then
  7.   io.stderr:write("Can't print without a printer!")
  8.   return
  9. end
  10.  
  11. local args = {...}
  12.  
  13. if #args == 0 or (#args == 1 and args[1] == "-g") then
  14.   io.write("Usage: print [-g] <filename>\n")
  15.   io.write(" -g: Global path - Use a global path for the target file instead of a local path.")
  16.   return
  17. end
  18.  
  19. if args[1] == "-g" then
  20.   path = args[2]
  21.   name = args[2]
  22. else
  23.   path = os.getenv("PWD").."/"..args[1]
  24.   name = args[1]
  25. end
  26.  
  27. local lines = {}
  28. local component = require "component"
  29. local op = component.openprinter
  30.  
  31. print "Opening file..."
  32. local fobj = io.open(path,"rb")
  33.  
  34. print "Reading file..."
  35. for l in fobj:lines() do
  36.   done = false
  37.   while done == false do
  38.     if l:len() > 30 then
  39.       table.insert(lines,l:sub(1,30))
  40.       l=">"..l:sub(31)
  41.     else
  42.       table.insert(lines,l)
  43.       done = true
  44.     end
  45.   end
  46. end
  47.  
  48. fobj:close()
  49. local counter = 0
  50.  
  51. print "Beginning to print..."
  52. for key,value in pairs(lines) do
  53.   if counter == 20 then
  54.     op.setTitle(name)
  55.     op.print()
  56.     counter = 0
  57.   end
  58.   op.writeln(value)
  59.   counter = counter + 1
  60. end
  61.  
  62. op.setTitle(name)
  63. op.print()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement