SHOW:
|
|
- or go back to the newest paste.
1 | --Splits a string according to a pattern into a table | |
2 | local function split(str, pattern) | |
3 | local t = { } | |
4 | local fpat = "(.-)" .. pattern | |
5 | local last_end = 1 | |
6 | local s, e, cap = str:find(fpat, 1) | |
7 | while s do | |
8 | if s ~= 1 or cap ~= "" then | |
9 | table.insert(t,cap) | |
10 | end | |
11 | last_end = e+1 | |
12 | s, e, cap = str:find(fpat, last_end) | |
13 | end | |
14 | if last_end <= #str then | |
15 | cap = str:sub(last_end) | |
16 | table.insert(t, cap) | |
17 | end | |
18 | return t | |
19 | end | |
20 | ||
21 | --[[ Main code ]]-- | |
22 | ||
23 | textStore = { } | |
24 | --The number of characters on the present line, and the number of lines on the present page | |
25 | local charCount, lineCount = 0, 0 | |
26 | --The maximum char and line count | |
27 | local charMax, lineMax = 25, 21 | |
28 | --The command line arguments | |
29 | local tArgs = {...} | |
30 | --The wrapped peripheral | |
31 | local printer = nil | |
32 | ||
33 | --We locate the printer first | |
34 | for k,v in pairs(rs.getSides()) do | |
35 | if peripheral.getType(v) == "printer" then | |
36 | printer = peripheral.wrap(v) | |
37 | end | |
38 | end | |
39 | if not printer then | |
40 | print("No printer attached!") | |
41 | return | |
42 | end | |
43 | ||
44 | --Catching missing arguments | |
45 | if #tArgs == 0 then | |
46 | print("Usage: printpage <filename>") | |
47 | return | |
48 | end | |
49 | ||
50 | local filePath = shell.resolve(".").."/"..tArgs[1] | |
51 | ||
52 | --Files use relative pathing, catching missing files | |
53 | if not fs.exists(filePath) then | |
54 | print("File not found") | |
55 | return | |
56 | end | |
57 | --Catches trying to print entire directories | |
58 | if fs.isDir(filePath) then | |
59 | print("Cannot print directory") | |
60 | return | |
61 | end | |
62 | ||
63 | --We rip the file open and cut it up here | |
64 | local file = fs.open(filePath, "r") | |
65 | textStore = split(file.readAll(), "\n") | |
66 | file.close() | |
67 | ||
68 | --Finishes the page, fires the new page down and keeps going | |
69 | function finishPage() | |
70 | printer.endPage() | |
71 | lineCount = 0 | |
72 | wordCount = 0 | |
73 | ||
74 | rs.setOutput("bottom", true) | |
75 | sleep(0.1) | |
76 | rs.setOutput("bottom", false) | |
77 | ||
78 | printer.newPage() | |
79 | charMax, lineMax = printer.getPageSize() | |
80 | end | |
81 | ||
82 | --This will print the word with a basic write command but updates | |
83 | --the char and line fields, and starts a new page if it needs to | |
84 | function safePrint(word) | |
85 | printer.write(word) | |
86 | charCount = charCount + #word | |
87 | if charCount >= charMax then | |
88 | print("Safe print: Writing new line") | |
89 | nextLine() | |
90 | end | |
91 | end | |
92 | ||
93 | --Moves to the next line, or the next page if need be | |
94 | function nextLine() | |
95 | charCount = 0 | |
96 | lineCount = lineCount + 1 | |
97 | if lineCount >= lineMax then | |
98 | print("End of page!") | |
99 | finishPage() | |
100 | else | |
101 | local _,oy = printer.getCursorPos() | |
102 | printer.setCursorPos(1,oy + 1) | |
103 | print("Now at line "..(oy + 1)) | |
104 | end | |
105 | end | |
106 | ||
107 | --The pagination script. | |
108 | printer.newPage() | |
109 | charMax, lineMax = printer.getPageSize() | |
110 | for i=1,#textStore do | |
111 | local word = textStore[i] | |
112 | --We replace tab characters with spaces, for convenience | |
113 | string.gsub(word, "\t", " ") | |
114 | --We split by space for each subword | |
115 | local subwords = split(word, " ") | |
116 | for j=1,#subwords do | |
117 | local subword = subwords[j] | |
118 | print("Printing "..subword) | |
119 | ||
120 | --If the word goes over the line, we just print as much of it as we can | |
121 | --until it's short enough to fit on one line | |
122 | while #subword > charMax do | |
123 | safePrint(subword) | |
124 | subword = string.sub(subword, charMax - charCount) | |
125 | print("Word too big! Reduced to "..subword) | |
126 | end | |
127 | ||
128 | --If the word is blocked by other stuff on the line, we just go to the next line | |
129 | if #subword + charCount > charMax then | |
130 | print("No space on line (length = "..#subword..", count = "..charCount..")") | |
131 | nextLine() | |
132 | end | |
133 | ||
134 | --We then print our word (or what's left of it) | |
135 | print("Printing "..subword) | |
136 | safePrint(subword) | |
137 | ||
138 | --Finally, if there's room a space character is added | |
139 | if charCount < charMax then | |
140 | print("Adding space character") | |
141 | safePrint(" ") | |
142 | end | |
143 | os.pullEvent("key") | |
144 | end | |
145 | print("Reached NL- next line") | |
146 | nextLine() | |
147 | end |