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 | sleep(1) | |
78 | ||
79 | printer.newPage() | |
80 | charMax, lineMax = printer.getPageSize() | |
81 | end | |
82 | ||
83 | --This will print the word with a basic write command but updates | |
84 | --the char and line fields, and starts a new page if it needs to | |
85 | function safePrint(word) | |
86 | printer.write(word) | |
87 | charCount = charCount + #word | |
88 | - | print("Safe print: Writing new line") |
88 | + | |
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!") |
98 | + | |
99 | else | |
100 | local _,oy = printer.getCursorPos() | |
101 | printer.setCursorPos(1,oy + 1) | |
102 | end | |
103 | - | print("Now at line "..(oy + 1)) |
103 | + | |
104 | ||
105 | --The pagination script. | |
106 | ||
107 | printer.newPage() | |
108 | ||
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) |
118 | + | |
119 | --If the word goes over the line, we just print as much of it as we can | |
120 | --until it's short enough to fit on one line | |
121 | while #subword > charMax do | |
122 | safePrint(subword) | |
123 | subword = string.sub(subword, charMax - charCount + 1) | |
124 | - | subword = string.sub(subword, charMax - charCount) |
124 | + | |
125 | - | print("Word too big! Reduced to "..subword) |
125 | + | |
126 | --If the word is blocked by other stuff on the line, we just go to the next line | |
127 | if #subword + charCount > charMax then | |
128 | nextLine() | |
129 | end | |
130 | - | print("No space on line (length = "..#subword..", count = "..charCount..")") |
130 | + | |
131 | --We then print our word (or what's left of it) | |
132 | safePrint(subword) | |
133 | ||
134 | --Finally, if there's room a space character is added | |
135 | - | print("Printing "..subword) |
135 | + | |
136 | safePrint(" ") | |
137 | end | |
138 | end | |
139 | nextLine() | |
140 | - | print("Adding space character") |
140 | + | |
141 | ||
142 | printer.endPage() | |
143 | - | os.pullEvent("key") |
143 | + | rs.setOutput("bottom", true) |
144 | sleep(0.1) | |
145 | - | print("Reached NL- next line") |
145 | + | rs.setOutput("bottom", false) |
146 | sleep(1) |