Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local config = '/etc/print.conf'
- local labels = {}
- local printers = {}
- local activePrinter
- function version()
- return 'netPrint v1.0.1'
- end
- --local functions
- --Gets the name of the printer.
- local function getPrinterName(printer)
- if type(printer) == 'string' then return labels[printer] end
- for n, p in pairs(printers) do
- if p == printer then
- return n
- end
- end
- return nil
- end
- --Gets the label of the printer
- local function getPrinterLabel(p)
- if type(p) == 'string' then
- for l, n in pairs(labels) do
- if n == p then
- return l
- end
- end
- return p
- else
- name = getPrinterName(p)
- return getPrinterLabel(name)
- end
- return nil
- end
- --Loads the saved labels from the configuration file
- local function load()
- if fs.exists(config) then
- local conf = fs.open(config, 'r')
- activePrinter = conf:read()
- labels = textutils.unserialize(conf:readAll())
- conf:close()
- else
- activePrinter = nil
- labels = {}
- end
- end
- -- Saves the labels to the configuration file
- local function save()
- local conf = fs.open(config, 'w')
- conf:write(getPrinterName(activePrinter)..'\n')
- conf:write(textutils.serialize(labels))
- conf.close()
- end
- --label setting and getting functions
- -- Returns the printer with label printLabel
- function getPrinter(printLabel)
- if type(printLabel) ~= 'string' then
- return nil, 'Value needs to be a String'
- end
- if not labels[printLabel] then
- if printers[printLabel] then
- return printers[printLabel]
- else
- return nil, 'Printer '..printLabel..' not found'
- end
- else
- return printers[labels[printLabel]]
- end
- end
- -- Removes the label for printer printerName
- function removeLabel(printerName)
- for label, printer in pairs(labels) do
- if printer == printerName then
- labels[label] = nil
- end
- end
- save()
- end
- -- Sets the label label for printer printerName
- function setPrinterLabel(printerName, label)
- if not printers[printerName] then
- return false, 'No such printer: '..printerName
- end
- if labels[label] then
- return false, 'Already a printer with label '..label
- end
- removeLabel(printerName)
- labels[label] = printerName
- save()
- return true
- end
- --Active printer settings
- -- Comparator used for comparing two printers.
- comp = function(p1, p2)
- pagep1 = math.min(p1.getPaperLevel(), p1.getInkLevel())
- pagep2 = math.min(p2.getPaperLevel(), p2.getInkLevel())
- if (pagep1 == pagep2) then
- return 0
- elseif (pagep1 > pagep2) then
- return 1
- else
- return -1
- end
- end
- -- Sets the active printer to printer label
- function setActivePrinter(label)
- p, e = getPrinter(label)
- if not p and e then
- return e
- else
- activePrinter = p
- end
- end
- -- Sets the active printer according to the comp function
- function autoSetActivePrinter()
- local bestPrinter = nil
- for name, printer in pairs(printers) do
- if not bestPrinter then
- bestPrinter = name
- elseif(comp(printers[bestPrinter], printer) == -1) then
- bestPrinter = name
- end
- end
- setActivePrinter(bestPrinter)
- end
- -- Returns the active printer
- local function getActivePrinter()
- if not activePrinter then autoSetActivePrinter() end
- return activePrinter
- end
- --Printer table functions
- -- Only update the printer table
- local function rawUpdate()
- for _, name in ipairs(peripheral.getNames()) do
- if peripheral.getType(name) == "printer" then
- local printer = peripheral.wrap(name)
- printers[name] = printer
- end
- end
- end
- -- Save labels and update the printer table
- function update()
- printers = {}
- local actName = getPrinterName(activePrinter)
- activePrinter = nil
- rawUpdate()
- if actName then
- setActivePrinter(actName)
- else
- autoSetActivePrinter()
- end
- end
- -- initialize the system
- function init()
- load()
- rawUpdate()
- getActivePrinter()
- end
- -- reutrns a table with the names for all the printers + ink and paper levels
- function getPrinterTable()
- local printTable = {}
- for n, p in pairs(printers) do
- local print = {}
- local name = getPrinterLabel(n) or n
- table.insert(print, name)
- table.insert(print, p.getPaperLevel())
- table.insert(print, p.getInkLevel())
- table.insert(printTable, print)
- end
- return printTable
- end
- -- Return getPrinterTable as string
- function printerTableString()
- local printTable = getPrinterTable()
- local strT = {}
- for _, t in pairs(printTable) do
- local str = t[1]..'\t'..t[2]..'\t'..t[3]
- table.insert(strT, str)
- end
- return table.concat(strT, '\n')
- end
- --Printer API functions
- function getPaperLevel()
- local act = getActivePrinter()
- if act then
- return act.getPaperLevel()
- else
- return nil
- end
- end
- function newPage()
- local act = getActivePrinter()
- if act then
- return act.newPage()
- else
- return nil
- end
- end
- function endPage()
- local act = getActivePrinter()
- if act then
- return act.endPage()
- else
- return nil
- end
- end
- function write(...)
- local mes = table.concat({...}, '')
- local act = getActivePrinter()
- if act then
- return act.write(mes)
- else
- return nil
- end
- end
- function print(...)
- write(...)
- local x, y = getCursorPos()
- setCursorPos(1, y+1)
- end
- function setPageTitle(...)
- local act = getActivePrinter()
- if act then
- return act.setPageTitle(...)
- else
- return nil
- end
- end
- function getInkLevel()
- local act = getActivePrinter()
- if act then
- return act.getInkLevel()
- else
- return nil
- end
- end
- function getCursorPos()
- local act = getActivePrinter()
- if act then
- return act.getCursorPos()
- else
- return nil
- end
- end
- function setCursorPos(...)
- local act = getActivePrinter()
- if act then
- return act.setCursorPos(...)
- else
- return nil
- end
- end
- function getPageSize()
- local act = getActivePrinter()
- if act then
- return act.getPageSize()
- else
- return nil
- end
- end
- -- Wrap the text while writing
- function wrapWrite(...)
- local mes = table.concat(..., '')
- local x, y = getCursorPos()
- local width, height = getPageSize()
- local function newLine()
- setCursorPos(1, y + 1)
- end
- for i = 1, #mes do
- local c = mes:sub(i,i)
- x, y = getCursorPos()
- if c == '\n' then
- newLine()
- elseif (c == ' ' and x == 1) then
- c = nil
- else
- if x > width then
- newLine()
- end
- if y > height then
- endPage()
- newPage()
- x=1
- y=1
- setCursorPos(x,y)
- end
- write(c)
- end
- end
- end
- --Same as wrapWrite, but set the cursor to the next line after printing
- function wrapPrint(...)
- wrapWrite(...)
- local x, y = getCursorPos()
- setCursorPos(1, y+1)
- end
- init()
Advertisement
Add Comment
Please, Sign In to add comment