Wobbo

CC netPrint API

Nov 2nd, 2013
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.64 KB | None | 0 0
  1. local config = '/etc/print.conf'
  2. local labels = {}
  3. local printers = {}
  4. local activePrinter
  5.  
  6. function version()
  7.     return 'netPrint v1.0.1'
  8. end
  9.  
  10. --local functions
  11.  
  12. --Gets the name of the printer.
  13. local function getPrinterName(printer)
  14.     if type(printer) == 'string' then return labels[printer] end
  15.   for n, p in pairs(printers) do
  16.     if p == printer then
  17.       return n
  18.     end
  19.   end
  20.   return nil
  21. end
  22.  
  23. --Gets the label of the printer
  24. local function getPrinterLabel(p)
  25.   if type(p) == 'string' then
  26.     for l, n in pairs(labels) do
  27.       if n == p then
  28.         return l
  29.       end
  30.     end
  31.     return p
  32.   else
  33.     name = getPrinterName(p)
  34.     return getPrinterLabel(name)
  35.   end
  36.   return nil
  37. end
  38.  
  39. --Loads the saved labels from the configuration file
  40. local function load()
  41.   if fs.exists(config) then
  42.     local conf = fs.open(config, 'r')
  43.     activePrinter = conf:read()
  44.     labels = textutils.unserialize(conf:readAll())
  45.     conf:close()
  46.   else
  47.     activePrinter = nil
  48.     labels = {}
  49.   end
  50. end
  51.  
  52. -- Saves the labels to the configuration file
  53. local function save()
  54.   local conf = fs.open(config, 'w')
  55.   conf:write(getPrinterName(activePrinter)..'\n')
  56.   conf:write(textutils.serialize(labels))
  57.   conf.close()
  58. end
  59.  
  60. --label setting and getting functions
  61.  
  62. -- Returns the printer with label printLabel
  63. function getPrinter(printLabel)
  64.   if type(printLabel) ~= 'string' then
  65.     return nil, 'Value needs to be a String'
  66.   end
  67.   if not labels[printLabel] then
  68.     if printers[printLabel] then
  69.       return printers[printLabel]
  70.     else
  71.       return nil, 'Printer '..printLabel..' not found'
  72.     end
  73.   else
  74.     return printers[labels[printLabel]]
  75.   end
  76. end  
  77.  
  78. -- Removes the label for printer printerName
  79. function removeLabel(printerName)
  80.   for label, printer in pairs(labels) do
  81.     if printer == printerName then
  82.       labels[label] = nil
  83.     end
  84.   end
  85.   save()
  86. end
  87.  
  88. -- Sets the label label for printer printerName
  89. function setPrinterLabel(printerName, label)
  90.   if not printers[printerName] then
  91.     return false, 'No such printer: '..printerName  
  92.   end
  93.   if labels[label] then
  94.     return false, 'Already a printer with label '..label
  95.   end
  96.   removeLabel(printerName)
  97.   labels[label] = printerName
  98.   save()
  99.   return true
  100. end
  101.  
  102. --Active printer settings
  103.  
  104. -- Comparator used for comparing two printers.
  105. comp = function(p1, p2)
  106.     pagep1 = math.min(p1.getPaperLevel(), p1.getInkLevel())
  107.     pagep2 = math.min(p2.getPaperLevel(), p2.getInkLevel())
  108.     if (pagep1 == pagep2) then
  109.         return 0
  110.     elseif (pagep1 > pagep2) then
  111.         return 1
  112.     else
  113.         return -1
  114.     end
  115. end
  116.  
  117. -- Sets the active printer to printer label
  118. function setActivePrinter(label)
  119.   p, e = getPrinter(label)
  120.   if not p and e then
  121.     return e
  122.   else
  123.     activePrinter = p
  124.   end
  125. end
  126.  
  127. -- Sets the active printer according to the comp function
  128. function autoSetActivePrinter()
  129.     local bestPrinter = nil
  130.     for name, printer in pairs(printers) do
  131.         if not bestPrinter then
  132.             bestPrinter = name
  133.         elseif(comp(printers[bestPrinter], printer) == -1) then
  134.             bestPrinter = name
  135.         end
  136.     end
  137.     setActivePrinter(bestPrinter)
  138. end
  139.  
  140. -- Returns the active printer
  141. local function getActivePrinter()
  142.     if not activePrinter then autoSetActivePrinter() end
  143.     return activePrinter
  144. end
  145.  
  146. --Printer table functions
  147.  
  148. -- Only update the printer table
  149. local function rawUpdate()
  150.   for _, name in ipairs(peripheral.getNames()) do
  151.     if peripheral.getType(name) == "printer" then
  152.       local printer = peripheral.wrap(name)
  153.       printers[name] = printer
  154.     end
  155.   end
  156. end
  157.  
  158. -- Save labels and update the printer table
  159. function update()
  160.   printers = {}
  161.   local actName = getPrinterName(activePrinter)
  162.   activePrinter = nil
  163.   rawUpdate()
  164.   if actName then
  165.         setActivePrinter(actName)
  166.   else
  167.         autoSetActivePrinter()
  168.   end
  169. end
  170.  
  171. -- initialize the system
  172. function init()
  173.     load()
  174.     rawUpdate()
  175.     getActivePrinter()
  176. end
  177.    
  178. -- reutrns a table with the names for all the printers + ink and paper levels
  179. function getPrinterTable()
  180.   local printTable = {}
  181.   for n, p in pairs(printers) do
  182.     local print = {}
  183.     local name = getPrinterLabel(n) or n
  184.     table.insert(print, name)
  185.     table.insert(print, p.getPaperLevel())
  186.     table.insert(print, p.getInkLevel())
  187.     table.insert(printTable, print)
  188.   end
  189.   return printTable
  190. end
  191.  
  192. -- Return getPrinterTable as string
  193. function printerTableString()
  194.   local printTable = getPrinterTable()
  195.   local strT = {}
  196.   for _, t in pairs(printTable) do
  197.     local str = t[1]..'\t'..t[2]..'\t'..t[3]
  198.     table.insert(strT, str)
  199.   end
  200.   return table.concat(strT, '\n')
  201. end
  202.  
  203. --Printer API functions
  204.  
  205. function getPaperLevel()
  206.     local act = getActivePrinter()
  207.     if act then
  208.         return act.getPaperLevel()
  209.     else
  210.         return nil
  211.     end
  212. end
  213.  
  214. function newPage()
  215.     local act = getActivePrinter()
  216.     if act then
  217.         return act.newPage()
  218.     else
  219.         return nil
  220.     end
  221. end
  222.  
  223. function endPage()
  224.     local act = getActivePrinter()
  225.     if act then
  226.         return act.endPage()
  227.     else
  228.         return nil
  229.     end
  230. end
  231.  
  232. function write(...)
  233.     local mes = table.concat({...}, '')
  234.     local act = getActivePrinter()
  235.     if act then
  236.         return act.write(mes)
  237.     else
  238.         return nil
  239.     end
  240. end
  241.  
  242. function print(...)
  243.     write(...)
  244.     local x, y = getCursorPos()
  245.     setCursorPos(1, y+1)
  246. end
  247.  
  248. function setPageTitle(...)
  249.     local act = getActivePrinter()
  250.     if act then
  251.         return act.setPageTitle(...)
  252.     else
  253.         return nil
  254.     end
  255. end
  256.  
  257. function getInkLevel()
  258.     local act = getActivePrinter()
  259.     if act then
  260.         return act.getInkLevel()
  261.     else
  262.         return nil
  263.     end
  264. end
  265.  
  266. function getCursorPos()
  267.     local act = getActivePrinter()
  268.     if act then
  269.         return act.getCursorPos()
  270.     else
  271.         return nil
  272.     end
  273. end
  274.  
  275. function setCursorPos(...)
  276.     local act = getActivePrinter()
  277.     if act then
  278.         return act.setCursorPos(...)
  279.     else
  280.         return nil
  281.     end
  282. end
  283.  
  284. function getPageSize()
  285.     local act = getActivePrinter()
  286.     if act then
  287.         return act.getPageSize()
  288.     else
  289.         return nil
  290.     end
  291. end
  292.  
  293. -- Wrap the text while writing
  294. function wrapWrite(...)
  295.     local mes = table.concat(..., '')
  296.     local x, y = getCursorPos()
  297.     local width, height = getPageSize()
  298.     local function newLine()
  299.         setCursorPos(1, y + 1)
  300.     end
  301.     for i = 1, #mes do
  302.         local c = mes:sub(i,i)
  303.         x, y = getCursorPos()
  304.         if c == '\n' then
  305.             newLine()
  306.         elseif (c == ' ' and x == 1) then
  307.             c = nil
  308.         else
  309.             if x > width then
  310.                 newLine()
  311.             end
  312.             if y > height then
  313.                 endPage()
  314.                 newPage()
  315.                 x=1
  316.                 y=1
  317.                 setCursorPos(x,y)
  318.             end
  319.             write(c)
  320.         end
  321.     end
  322. end
  323.  
  324. --Same as wrapWrite, but set the cursor to the next line after printing
  325. function wrapPrint(...)
  326.     wrapWrite(...)
  327.     local x, y = getCursorPos()
  328.     setCursorPos(1, y+1)
  329. end
  330.    
  331. init()
Advertisement
Add Comment
Please, Sign In to add comment