Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --Splits a string according to a pattern into a table
- local function split(str, pattern)
- local t = { }
- local fpat = "(.-)" .. pattern
- local last_end = 1
- local s, e, cap = str:find(fpat, 1)
- while s do
- if s ~= 1 or cap ~= "" then
- table.insert(t,cap)
- end
- last_end = e+1
- s, e, cap = str:find(fpat, last_end)
- end
- if last_end <= #str then
- cap = str:sub(last_end)
- table.insert(t, cap)
- end
- return t
- end
- --[[ Main code ]]--
- textStore = { }
- --The number of characters on the present line, and the number of lines on the present page
- local charCount, lineCount = 0, 0
- --The maximum char and line count
- local charMax, lineMax = 25, 21
- --The command line arguments
- local tArgs = {...}
- --The wrapped peripheral
- local printer = nil
- --We locate the printer first
- for k,v in pairs(rs.getSides()) do
- if peripheral.getType(v) == "printer" then
- printer = peripheral.wrap(v)
- end
- end
- if not printer then
- print("No printer attached!")
- return
- end
- --Catching missing arguments
- if #tArgs == 0 then
- print("Usage: printpage <filename>")
- return
- end
- local filePath = shell.resolve(".").."/"..tArgs[1]
- --Files use relative pathing, catching missing files
- if not fs.exists(filePath) then
- print("File not found")
- return
- end
- --Catches trying to print entire directories
- if fs.isDir(filePath) then
- print("Cannot print directory")
- return
- end
- --We rip the file open and cut it up here
- local file = fs.open(filePath, "r")
- textStore = split(file.readAll(), "\n")
- file.close()
- --Finishes the page, fires the new page down and keeps going
- function finishPage()
- printer.endPage()
- lineCount = 0
- wordCount = 0
- rs.setOutput("bottom", true)
- sleep(0.1)
- rs.setOutput("bottom", false)
- printer.newPage()
- charMax, lineMax = printer.getPageSize()
- end
- --This will print the word with a basic write command but updates
- --the char and line fields, and starts a new page if it needs to
- function safePrint(word)
- printer.write(word)
- charCount = charCount + #word
- if charCount >= charMax then
- print("Safe print: Writing new line")
- nextLine()
- end
- end
- --Moves to the next line, or the next page if need be
- function nextLine()
- charCount = 0
- lineCount = lineCount + 1
- if lineCount >= lineMax then
- print("End of page!")
- finishPage()
- else
- local _,oy = printer.getCursorPos()
- printer.setCursorPos(1,oy + 1)
- print("Now at line "..(oy + 1))
- end
- end
- --The pagination script.
- printer.newPage()
- charMax, lineMax = printer.getPageSize()
- for i=1,#textStore do
- local word = textStore[i]
- --We replace tab characters with spaces, for convenience
- string.gsub(word, "\t", " ")
- --We split by space for each subword
- local subwords = split(word, " ")
- for j=1,#subwords do
- local subword = subwords[j]
- print("Printing "..subword)
- --If the word goes over the line, we just print as much of it as we can
- --until it's short enough to fit on one line
- while #subword > charMax do
- safePrint(subword)
- subword = string.sub(subword, charMax - charCount)
- print("Word too big! Reduced to "..subword)
- end
- --If the word is blocked by other stuff on the line, we just go to the next line
- if #subword + charCount > charMax then
- print("No space on line (length = "..#subword..", count = "..charCount..")")
- nextLine()
- end
- --We then print our word (or what's left of it)
- print("Printing "..subword)
- safePrint(subword)
- --Finally, if there's room a space character is added
- if charCount < charMax then
- print("Adding space character")
- safePrint(" ")
- end
- os.pullEvent("key")
- end
- print("Reached NL- next line")
- nextLine()
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement