Last active
February 24, 2018 13:52
-
-
Save InDieTasten/4a798fa15426d5c74104659df62547ae to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Author: InDieTasten on server.lycodon.com | |
-- Date created: 2018-02-24 | |
-- Lua script for use in ComputerCraft | |
-- settings | |
local printerSide = "left" | |
local catalogFilename = "./catalog" | |
-- program-wide variables | |
local selectionScreenPage = 1 | |
local catalog = {} | |
local catalogPagesTotal = 1 | |
local receiptPositions = {} | |
-- function declarations | |
local function getProductCatalogFromFile(page) | |
local _,height = term.getSize() | |
local pageSize = height - 6 | |
local catalog = {} | |
local fileHandle = fs.open(catalogFilename, "r") | |
if (fileHandle == nil) then | |
error("Catalog file '"..tostring(catalogFilename).."' not found") | |
end | |
while (true) do | |
local product = fileHandle.readLine() | |
if (product == nil or product == "") then | |
break | |
end | |
table.insert(catalog, product) | |
end | |
fileHandle.close() | |
return catalog | |
end | |
local function enterSelectionScreen() | |
term.clear() | |
term.setCursorPos(1,1) | |
catalogPagesTotal = math.ceil(#catalog/12) | |
print("Kassenbeleg Printer v2") | |
print("") | |
-- print available products | |
for p = (selectionScreenPage-1)*12+1, math.min(#catalog, selectionScreenPage*12), 1 do | |
print(p, ". ", string.sub(catalog[p], 1, 21)) | |
end | |
-- print current receipt positions | |
for i, v in ipairs(receiptPositions) do | |
term.setCursorPos(28, 2+i) | |
if (i > 11) then | |
term.write("...") | |
break | |
else | |
term.write("- "..string.sub(receiptPositions[i], 1, 21)) | |
end | |
end | |
-- print instructions | |
term.setCursorPos(1, 16) | |
pageControls = "" | |
if (selectionScreenPage > 1) then | |
pageControls = " [prev]" | |
end | |
if (selectionScreenPage < catalogPagesTotal) then | |
pageControls = pageControls.." [next]" | |
end | |
print("Seite "..tostring(selectionScreenPage).." von "..tostring(catalogPagesTotal)..pageControls) | |
print("Wähle Produkt, oder Aktion:\n[print] [clear] [add] [exit]") | |
return read() | |
end | |
local function printReceipt(positions) | |
local pagesTotal = math.ceil(#positions/15) | |
local printer = peripheral.wrap(printerSide) | |
if (printer == nil) then | |
error("Printer not found on side '"..tostring(printerSide).."'") | |
end | |
while (printer.getInkLevel() < pagesTotal) do | |
printError("Nicht genügend Tinte. Fülle Tinte nach und drücker anschliessend Enter/Return") | |
read() | |
end | |
while (printer.getPaperLevel() < pagesTotal) do | |
printError("Nicht genügend Papier. Fülle Papier nach und drücke anschliessend Enter/Return") | |
read() | |
end | |
if (not printer.newPage()) then | |
print("Vorheriger Druckvorgang wird abgebrochen") | |
printer.setPageTitle("Abgebrochener Druckvorgang") | |
printer.endPage() | |
printReceipt(positions) | |
return | |
end | |
for page = 1, pagesTotal, 1 do | |
if (page == 1) then | |
-- header | |
printer.setCursorPos(1,1) | |
printer.write(" -- I-EP --") | |
else | |
while (not printer.newPage()) do | |
if (printer.getInkLevel() == 0) then | |
printError("Nicht genügend Tinte. Fülle Tinte nach und drücke anschliessend Enter/Return") | |
read() | |
elseif (printer.getPaperLevel() == 0) then | |
printError("Nicht genügend Papier. Fülle Papier nach und drücke anschliessend Enter/Return") | |
read() | |
end | |
end | |
end | |
-- set page title | |
if (page == 1) then | |
printer.setPageTitle("I-EP Kassenbeleg") | |
else | |
printer.setPageTitle("I-EP Kassenbeleg ["..tostring(page).."]") | |
end | |
-- print positions | |
printer.setCursorPos(1, 3) | |
for pos = (page-1)*15+1, math.min(#positions, page*15) do | |
printer.write(string.sub(positions[pos], 0, 24)) | |
printer.setCursorPos(1, ({printer.getCursorPos()})[2] + 1) | |
end | |
if (page == pagesTotal) then | |
-- footer | |
printer.setCursorPos(1, 19) | |
printer.write(" Vielen Dank für Ihren") | |
printer.setCursorPos(1, 20) | |
printer.write(" Einkauf bei") | |
printer.setCursorPos(1, 21) | |
printer.write(" Ice-Electronic Programs") | |
end | |
while (not printer.endPage()) do | |
printError("Kein Platz mehr im Ausgabefach. Entnehme Seiten und drücke Enter/Return") | |
read() | |
end | |
end | |
print("Beleg erfolgreich gedruckt") | |
end | |
-- main | |
local function main() | |
catalog = getProductCatalogFromFile() | |
while (true) do | |
local newReceiptPosition = enterSelectionScreen() | |
if (newReceiptPosition == "print") then | |
if (#receiptPositions == 0) then | |
printError("Ein Beleg muss mindestens ein Produkt enthalten") | |
sleep(2) | |
else | |
printReceipt(receiptPositions) | |
break | |
end | |
elseif (newReceiptPosition == "add") then | |
write("Direkteingabe eines Produkts: ") | |
local prodName = read() | |
if (prodName ~= "") then | |
table.insert(receiptPositions, prodName) | |
end | |
elseif (newReceiptPosition == "clear") then | |
receiptPositions = {} | |
elseif (newReceiptPosition == "exit") then | |
break | |
elseif (newReceiptPosition == "next") then | |
if (selectionScreenPage < catalogPagesTotal) then | |
selectionScreenPage = selectionScreenPage + 1 | |
end | |
elseif (newReceiptPosition == "prev") then | |
if (selectionScreenPage > 1) then | |
selectionScreenPage = selectionScreenPage - 1 | |
end | |
else | |
-- Add product to shopping cart | |
local prodName = catalog[tonumber(newReceiptPosition)] | |
if (prodName == nil) then | |
printError("Not found") | |
sleep(1) | |
else | |
table.insert(receiptPositions, prodName) | |
end | |
end | |
end | |
end | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment