Skip to content

Instantly share code, notes, and snippets.

@SammyForReal
Created April 8, 2023 15:05
Show Gist options
  • Save SammyForReal/e19f7cd1f2e5f08dc0724de42602e4ee to your computer and use it in GitHub Desktop.
Save SammyForReal/e19f7cd1f2e5f08dc0724de42602e4ee to your computer and use it in GitHub Desktop.
Print .2dja poster easily!
-- By Sammy (1Turtle)
-- Licence: The Unlicence
-- Get poster printer
local poster = peripheral.find("poster_printer")
if not poster then
error("A poster printer is required.")
end
-- Print usage
local tArgs = { ... }
if #tArgs == 0 then
local programName = arg[0] or fs.getName(shell.getRunningProgram())
print("Usage: " .. programName .. " <file.2dja>")
return
end
-- Error checking
local sPath = shell.resolve(tArgs[1])
if not fs.exists(sPath) then
error("File does nto exist.")
elseif fs.exists(sPath) and fs.isDir(sPath) then
error("Cannot edit a directory.")
end
local file = fs.open(sPath, 'r')
local content = file.readAll()
file.close()
-- Load file
term.write("Please wait... ")
content = textutils.unserialiseJSON(content)
if type(content) ~= "table" then
error("File seems to be corrupt.")
end
print("READY")
local bRunning = true
local total = ""..#(content.pages)
local page
local index = 1
-- Setup cursor
local nX,nY = term.getCursorPos()
if nY >= 19-5 then
term.scroll(5)
nY = nY-5
end
nX = 1
term.setBackgroundColor(colors.black)
term.setTextColor(colors.white)
local oldStatus, oldProgress = "", ""
parallel.waitForAny(
function()
-- Print loop
for i,p in pairs(content.pages) do
-- Wait until ready
while poster.status() == "busy" do
sleep(0.25)
end
index = i
page = p
-- Poster
poster.reset()
if page.label then poster.setLabel(page.label) end
if page.tooltip then poster.setTooltip(page.tooltip) end
poster.blitPalette(page.palette)
poster.blitPixels(1,1, page.pixels)
poster.commit(1)
sleep()
end
bRunning = false
end,
function()
while bRunning do
if type(page) == "table" then
-- Status
term.setTextColor(colors.gray)
term.setCursorPos(nX,nY)
term.clearLine()
term.write("Printing:")
term.setCursorPos(nX,nY+1)
term.clearLine()
term.setTextColor(colors.lightGray)
term.write(" Name: ")
term.setTextColor(colors.white)
term.write(("%s"):format(page.label or "???"))
term.setCursorPos(nX,nY+2)
term.clearLine()
term.setTextColor(colors.lightGray)
term.write(" Size: ")
term.setTextColor(colors.white)
term.write(("%sx%spx"):format(""..page.width, ""..page.height))
term.setCursorPos(nX,nY+3)
term.clearLine()
term.setTextColor(colors.lightGray)
term.write(" Index: ")
term.setTextColor(colors.white)
term.write(("%s of %s"):format(""..index, total))
local status,progress = poster.status()
if status ~= oldStatus or tostring(progress) ~= oldProgress then
progress = tostring(progress)
oldStatus = status
oldProgress = progress
term.setCursorPos(nX,nY+4)
term.clearLine()
term.setTextColor(colors.lightGray)
term.write(" Progress: ")
term.setTextColor(colors.white)
if status == "busy" and progress == "0" then
term.setTextColor(colors.red)
term.write("Paper jam")
else
term.write(("%s (%s)"):format(status, progress))
end
end
end
sleep(0.25)
end
end
)
@piguman3
Copy link

piguman3 commented Apr 8, 2023

nice

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment