@bobfrankston/brother-label 1.0.14 → 1.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +45 -2
- package/api.d.ts +26 -26
- package/api.d.ts.map +1 -1
- package/api.js +299 -464
- package/api.js.map +1 -1
- package/api.ts +429 -615
- package/cli.d.ts +1 -1
- package/cli.js +232 -262
- package/cli.js.map +1 -1
- package/cli.ts +301 -344
- package/index.d.ts +3 -1
- package/index.d.ts.map +1 -1
- package/index.js +4 -2
- package/index.js.map +1 -1
- package/index.ts +43 -24
- package/package.json +16 -1
- package/render.d.ts +11 -33
- package/render.d.ts.map +1 -1
- package/render.js +11 -235
- package/render.js.map +1 -1
- package/render.ts +29 -280
- package/tsconfig.json +25 -19
package/cli.ts
CHANGED
|
@@ -1,344 +1,301 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
/**
|
|
3
|
-
* Brother Label Printer CLI
|
|
4
|
-
* Thin wrapper around
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
import * as fs from "fs";
|
|
8
|
-
import * as path from "path";
|
|
9
|
-
import {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
if (
|
|
40
|
-
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
return;
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
const effectiveTape = tape ?? getConfig().defaultTape ?? 24;
|
|
303
|
-
console.log(`Printed ${parsed.segments.length} segments on ${effectiveTape}mm tape`);
|
|
304
|
-
}
|
|
305
|
-
} catch (err) {
|
|
306
|
-
console.error(`Error: ${(err as Error).message}`);
|
|
307
|
-
process.exit(1);
|
|
308
|
-
}
|
|
309
|
-
return;
|
|
310
|
-
}
|
|
311
|
-
|
|
312
|
-
// Single input mode
|
|
313
|
-
if (!parsed.input) {
|
|
314
|
-
showHelp();
|
|
315
|
-
return;
|
|
316
|
-
}
|
|
317
|
-
|
|
318
|
-
try {
|
|
319
|
-
const options = buildPrintOptions(parsed.input, {
|
|
320
|
-
tape: parsed.opts.tape as string | undefined,
|
|
321
|
-
printer: parsed.opts.printer as string | undefined,
|
|
322
|
-
text: parsed.opts.text as boolean | undefined,
|
|
323
|
-
html: parsed.opts.html as boolean | undefined,
|
|
324
|
-
image: parsed.opts.image as boolean | undefined,
|
|
325
|
-
aspect: parsed.opts.aspect as string | undefined,
|
|
326
|
-
height: parsed.opts.height as string | undefined,
|
|
327
|
-
});
|
|
328
|
-
|
|
329
|
-
if (parsed.opts.output) {
|
|
330
|
-
const buffer = await render(options);
|
|
331
|
-
fs.writeFileSync(parsed.opts.output as string, buffer);
|
|
332
|
-
console.log(`Saved to ${parsed.opts.output}`);
|
|
333
|
-
} else {
|
|
334
|
-
await print(options);
|
|
335
|
-
const tape = options.tape ?? getConfig().defaultTape ?? 24;
|
|
336
|
-
console.log(`Printed on ${tape}mm tape`);
|
|
337
|
-
}
|
|
338
|
-
} catch (err) {
|
|
339
|
-
console.error(`Error: ${(err as Error).message}`);
|
|
340
|
-
process.exit(1);
|
|
341
|
-
}
|
|
342
|
-
}
|
|
343
|
-
|
|
344
|
-
main();
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* Brother Label Printer CLI
|
|
4
|
+
* Thin wrapper around api.ts using shared CLI primitives from label-core.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import * as fs from "fs";
|
|
8
|
+
import * as path from "path";
|
|
9
|
+
import {
|
|
10
|
+
preprocessSingleQuotes,
|
|
11
|
+
parseArgs,
|
|
12
|
+
} from "@bobfrankston/label-core";
|
|
13
|
+
import type {
|
|
14
|
+
PrintOptions as CorePrintOptions,
|
|
15
|
+
PrinterStatus,
|
|
16
|
+
Segment,
|
|
17
|
+
ValuedOptionSpec,
|
|
18
|
+
BooleanFlagSpec,
|
|
19
|
+
} from "@bobfrankston/label-core";
|
|
20
|
+
import {
|
|
21
|
+
print,
|
|
22
|
+
render,
|
|
23
|
+
renderSegments,
|
|
24
|
+
printSegments,
|
|
25
|
+
getConfig,
|
|
26
|
+
setConfig,
|
|
27
|
+
getConfigPath,
|
|
28
|
+
listPrinters,
|
|
29
|
+
detectTapeSize,
|
|
30
|
+
brotherPrinter,
|
|
31
|
+
} from "./api.js";
|
|
32
|
+
import type { TapeSize, PrintOptions, SegmentOptions } from "./api.js";
|
|
33
|
+
|
|
34
|
+
const VERSION = "1.1.0";
|
|
35
|
+
const VALID_TAPES: TapeSize[] = [6, 9, 12, 18, 24];
|
|
36
|
+
|
|
37
|
+
function parseTape(value: string): TapeSize {
|
|
38
|
+
const num = parseInt(value.replace("mm", ""), 10) as TapeSize;
|
|
39
|
+
if (!VALID_TAPES.includes(num)) {
|
|
40
|
+
throw new Error(`Invalid tape size: ${value}. Valid: ${VALID_TAPES.join(", ")}`);
|
|
41
|
+
}
|
|
42
|
+
return num;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const VALUED: ValuedOptionSpec[] = [
|
|
46
|
+
{ names: ["-tape"], key: "tape" },
|
|
47
|
+
{ names: ["-p", "-printer"], key: "printer" },
|
|
48
|
+
{ names: ["-o", "-output"], key: "output" },
|
|
49
|
+
{ names: ["-a", "-aspect"], key: "aspect" },
|
|
50
|
+
{ names: ["-H", "-height"], key: "height" },
|
|
51
|
+
{ names: ["-s", "-space"], key: "space" },
|
|
52
|
+
{ names: ["-timeout"], key: "timeout" },
|
|
53
|
+
{ names: ["-interval"], key: "interval" },
|
|
54
|
+
];
|
|
55
|
+
|
|
56
|
+
const BOOLEAN: BooleanFlagSpec[] = [
|
|
57
|
+
{ names: ["-w", "-html"], key: "html" },
|
|
58
|
+
{ names: ["-i", "-image"], key: "image" },
|
|
59
|
+
{ names: ["-t"], key: "text" },
|
|
60
|
+
{ names: ["-c", "-clip"], key: "clip" },
|
|
61
|
+
{ names: ["-no-wait", "-nowait"], key: "nowait" },
|
|
62
|
+
{ names: ["-help", "-h", "-?"], key: "help" },
|
|
63
|
+
{ names: ["-version", "-v"], key: "version" },
|
|
64
|
+
];
|
|
65
|
+
|
|
66
|
+
const SUBCOMMANDS = ["list", "status", "config"];
|
|
67
|
+
|
|
68
|
+
function showHelp(): void {
|
|
69
|
+
console.log(`Brother Label Printer CLI v${VERSION}
|
|
70
|
+
|
|
71
|
+
Usage:
|
|
72
|
+
brother-print [options] <text> Print text
|
|
73
|
+
brother-print [options] <file> Print html/txt/image file (auto-detected)
|
|
74
|
+
brother-print -clip [options] Print clipboard contents (image or text)
|
|
75
|
+
brother-print -text <v> -qr <v> ... Print ordered text/qr segments
|
|
76
|
+
brother-print list List Brother printers and status
|
|
77
|
+
brother-print status [-p <name>] Show printer status
|
|
78
|
+
brother-print config Show / set defaults
|
|
79
|
+
|
|
80
|
+
Tape size is auto-detected from the printer. Use -tape to override.
|
|
81
|
+
Single quotes can wrap arguments: '7"' 'line1\\nline2'
|
|
82
|
+
|
|
83
|
+
Options:
|
|
84
|
+
-tape <size> Tape size: 6, 9, 12, 18, 24 (mm) [auto-detected]
|
|
85
|
+
-p, -printer <name> Printer queue name
|
|
86
|
+
-o, -output <file> Save PNG to file instead of printing
|
|
87
|
+
-a, -aspect <r> Aspect ratio width:height for HTML (e.g. 3.5:2)
|
|
88
|
+
-H, -height <size> Text height: 12mm, .5in, or 50% (of tape height)
|
|
89
|
+
-s, -space <size> Space between segments: 12px, 1mm, .2in
|
|
90
|
+
-t, -text Force input as literal text (-text <v> for segments)
|
|
91
|
+
-qr <data> QR code segment
|
|
92
|
+
-w, -html Force input as HTML file path
|
|
93
|
+
-i, -image Force input as image file path
|
|
94
|
+
-c, -clip Read content from clipboard (image preferred, then text)
|
|
95
|
+
-no-wait Fail immediately if printer is offline (default: wait)
|
|
96
|
+
-timeout <secs> Max wait time when printer is offline
|
|
97
|
+
-interval <secs> Polling interval while waiting (default 2)
|
|
98
|
+
-help Show this help
|
|
99
|
+
-version Show version`);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
async function main(): Promise<void> {
|
|
103
|
+
const argv = preprocessSingleQuotes(process.argv.slice(2));
|
|
104
|
+
let parsed;
|
|
105
|
+
try {
|
|
106
|
+
parsed = parseArgs(argv, VALUED, BOOLEAN, SUBCOMMANDS);
|
|
107
|
+
} catch (e: any) {
|
|
108
|
+
console.error(`Error: ${e.message}`);
|
|
109
|
+
process.exit(1);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
if (parsed.unknown.length > 0) {
|
|
113
|
+
console.error(`Unknown option(s): ${parsed.unknown.join(", ")}`);
|
|
114
|
+
console.error(`Run "brother-print -help" for usage.`);
|
|
115
|
+
process.exit(1);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
if (parsed.opts.help) { showHelp(); return; }
|
|
119
|
+
if (parsed.opts.version) { console.log(VERSION); return; }
|
|
120
|
+
|
|
121
|
+
try {
|
|
122
|
+
switch (parsed.command) {
|
|
123
|
+
case "list": await cmdList(); return;
|
|
124
|
+
case "status": await cmdStatus(parsed.opts); return;
|
|
125
|
+
case "config": await cmdConfig(parsed.opts); return;
|
|
126
|
+
}
|
|
127
|
+
await cmdPrint(parsed.opts, parsed.segments, parsed.inputs);
|
|
128
|
+
} catch (e: any) {
|
|
129
|
+
console.error(`Error: ${e.message}`);
|
|
130
|
+
process.exit(1);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
async function cmdList(): Promise<void> {
|
|
135
|
+
const printers = await listPrinters();
|
|
136
|
+
if (printers.length === 0) {
|
|
137
|
+
console.log("No Brother printers found.");
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
console.log(`Brother printers (${printers.length}):`);
|
|
141
|
+
for (const p of printers) {
|
|
142
|
+
let statusStr = "?";
|
|
143
|
+
try {
|
|
144
|
+
const s = await brotherPrinter.getStatus(p.name);
|
|
145
|
+
statusStr = s.online ? "online" : `offline (${s.statusText}${s.error ? ", " + s.error : ""})`;
|
|
146
|
+
} catch (e: any) {
|
|
147
|
+
statusStr = `status error: ${e.message}`;
|
|
148
|
+
}
|
|
149
|
+
console.log(` ${p.name} — ${statusStr}`);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
async function cmdStatus(opts: Record<string, string | boolean>): Promise<void> {
|
|
154
|
+
const name = (opts.printer as string) || undefined;
|
|
155
|
+
const s = await brotherPrinter.getStatus(name);
|
|
156
|
+
console.log(`Printer: ${s.name}`);
|
|
157
|
+
console.log(`Online: ${s.online ? "yes" : "no"}`);
|
|
158
|
+
console.log(`Status: ${s.statusText}`);
|
|
159
|
+
if (s.error) console.log(`Error: ${s.error}`);
|
|
160
|
+
console.log(`Queue: ${s.queueLength} job(s)`);
|
|
161
|
+
if (s.workOffline) console.log(`Work offline flag: true`);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
async function cmdConfig(opts: Record<string, string | boolean>): Promise<void> {
|
|
165
|
+
const hasUpdate = !!(opts.tape || opts.printer);
|
|
166
|
+
if (!hasUpdate) {
|
|
167
|
+
const cfg = getConfig();
|
|
168
|
+
console.log(`Configuration:`);
|
|
169
|
+
console.log(` File: ${getConfigPath()}`);
|
|
170
|
+
console.log(` Default tape: ${cfg.defaultTape ? cfg.defaultTape + "mm" : "(not set)"}`);
|
|
171
|
+
console.log(` Default printer: ${cfg.defaultPrinter ?? "(not set)"}`);
|
|
172
|
+
console.log("\nValid tape sizes: 6, 9, 12, 18, 24");
|
|
173
|
+
return;
|
|
174
|
+
}
|
|
175
|
+
if (opts.tape) {
|
|
176
|
+
const t = parseTape(opts.tape as string);
|
|
177
|
+
setConfig({ defaultTape: t });
|
|
178
|
+
console.log(`Default tape set to: ${t}mm`);
|
|
179
|
+
}
|
|
180
|
+
if (opts.printer) {
|
|
181
|
+
setConfig({ defaultPrinter: opts.printer as string });
|
|
182
|
+
console.log(`Default printer set to: ${opts.printer}`);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
async function resolveTape(opts: Record<string, string | boolean>): Promise<TapeSize> {
|
|
187
|
+
const explicit = opts.tape ? parseTape(opts.tape as string) : null;
|
|
188
|
+
const detected = await detectTapeSize(opts.printer as string | undefined);
|
|
189
|
+
if (explicit) {
|
|
190
|
+
if (detected && detected !== explicit) {
|
|
191
|
+
console.warn(`Warning: -tape ${explicit}mm specified but printer reports ${detected}mm tape loaded`);
|
|
192
|
+
}
|
|
193
|
+
return explicit;
|
|
194
|
+
}
|
|
195
|
+
if (detected) return detected;
|
|
196
|
+
return getConfig().defaultTape ?? 24;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
async function cmdPrint(
|
|
200
|
+
opts: Record<string, string | boolean>,
|
|
201
|
+
segments: Segment[],
|
|
202
|
+
inputs: string[]
|
|
203
|
+
): Promise<void> {
|
|
204
|
+
const tape = await resolveTape(opts);
|
|
205
|
+
|
|
206
|
+
// If there are 2+ positional inputs, treat each as a text segment.
|
|
207
|
+
// (A single positional input is handled below via classifyInput so it can
|
|
208
|
+
// auto-detect file paths.)
|
|
209
|
+
if (inputs.length > 1 || (inputs.length >= 1 && segments.length > 0)) {
|
|
210
|
+
for (const inp of inputs) {
|
|
211
|
+
segments.push({ type: "text", value: inp });
|
|
212
|
+
}
|
|
213
|
+
inputs.length = 0;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
const baseOpts = {
|
|
217
|
+
printer: opts.printer as string | undefined,
|
|
218
|
+
wait: !opts.nowait,
|
|
219
|
+
waitTimeoutMs: opts.timeout ? Math.round(parseFloat(opts.timeout as string) * 1000) : undefined,
|
|
220
|
+
waitIntervalMs: opts.interval ? Math.round(parseFloat(opts.interval as string) * 1000) : undefined,
|
|
221
|
+
onWaiting: makeWaitCallback(),
|
|
222
|
+
log: (msg: string) => console.log(msg),
|
|
223
|
+
};
|
|
224
|
+
|
|
225
|
+
// Multi-segment mode: explicit -text/-qr (>=1 segment), with positional joined in
|
|
226
|
+
if (segments.length > 1 || (segments.length === 1 && inputs.length === 0)) {
|
|
227
|
+
const segOpts: SegmentOptions = {
|
|
228
|
+
...baseOpts,
|
|
229
|
+
tape,
|
|
230
|
+
textHeight: opts.height as string | undefined,
|
|
231
|
+
space: opts.space as string | undefined,
|
|
232
|
+
};
|
|
233
|
+
if (opts.output) {
|
|
234
|
+
const buffer = await renderSegments(segments, tape, opts.height as string | undefined, opts.space as string | undefined);
|
|
235
|
+
fs.writeFileSync(opts.output as string, buffer);
|
|
236
|
+
console.log(`Saved to ${opts.output}`);
|
|
237
|
+
} else {
|
|
238
|
+
await printSegments(segments, segOpts);
|
|
239
|
+
console.log(`Printed ${segments.length} segment(s) on ${tape}mm tape`);
|
|
240
|
+
}
|
|
241
|
+
return;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
// Single content op
|
|
245
|
+
const printOpts: PrintOptions = {
|
|
246
|
+
...baseOpts,
|
|
247
|
+
tape,
|
|
248
|
+
aspect: opts.aspect as string | undefined,
|
|
249
|
+
textHeight: opts.height as string | undefined,
|
|
250
|
+
};
|
|
251
|
+
|
|
252
|
+
if (opts.clip) {
|
|
253
|
+
printOpts.clip = true;
|
|
254
|
+
} else if (inputs.length === 1) {
|
|
255
|
+
Object.assign(printOpts, classifyInput(inputs[0], opts));
|
|
256
|
+
} else if (inputs.length === 0 && segments.length === 0) {
|
|
257
|
+
showHelp();
|
|
258
|
+
return;
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
if (opts.output) {
|
|
262
|
+
const buffer = await render(printOpts);
|
|
263
|
+
fs.writeFileSync(opts.output as string, buffer);
|
|
264
|
+
console.log(`Saved to ${opts.output}`);
|
|
265
|
+
return;
|
|
266
|
+
}
|
|
267
|
+
await print(printOpts);
|
|
268
|
+
console.log(`Printed on ${tape}mm tape`);
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
function classifyInput(input: string, opts: Record<string, string | boolean>): Partial<CorePrintOptions> {
|
|
272
|
+
if (opts.text) return { text: input };
|
|
273
|
+
if (opts.html) return { htmlPath: path.resolve(input) };
|
|
274
|
+
if (opts.image) return { imagePath: path.resolve(input) };
|
|
275
|
+
|
|
276
|
+
const lower = input.toLowerCase();
|
|
277
|
+
if (lower.endsWith(".html") || lower.endsWith(".htm")) return { htmlPath: path.resolve(input) };
|
|
278
|
+
if (lower.endsWith(".txt")) return { textFile: path.resolve(input) };
|
|
279
|
+
if (/\.(png|jpg|jpeg|bmp|gif)$/i.test(lower)) return { imagePath: path.resolve(input) };
|
|
280
|
+
if (fs.existsSync(input)) {
|
|
281
|
+
const ext = path.extname(lower);
|
|
282
|
+
if (ext === ".html" || ext === ".htm") return { htmlPath: path.resolve(input) };
|
|
283
|
+
return { textFile: path.resolve(input) };
|
|
284
|
+
}
|
|
285
|
+
return { text: input };
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
function makeWaitCallback() {
|
|
289
|
+
let lastReport = 0;
|
|
290
|
+
return (status: PrinterStatus, elapsedMs: number, alternatives: PrinterStatus[]) => {
|
|
291
|
+
const secs = Math.floor(elapsedMs / 1000);
|
|
292
|
+
if (secs - lastReport < 5) return;
|
|
293
|
+
lastReport = secs;
|
|
294
|
+
const altStr = alternatives.length > 0
|
|
295
|
+
? ` (online alternatives: ${alternatives.map(a => a.name).join(", ")})`
|
|
296
|
+
: "";
|
|
297
|
+
console.log(`[brother-print] still waiting for ${status.name} (${status.statusText}${status.error ? ", " + status.error : ""}); ${secs}s elapsed${altStr}`);
|
|
298
|
+
};
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
main();
|