clean up printer formatting

This commit is contained in:
Sean Sube 2025-06-14 21:02:57 -05:00
parent 66d89adfd0
commit 95a3a08492
No known key found for this signature in database
GPG Key ID: 3EED7B957D362AF1
4 changed files with 78 additions and 31 deletions

View File

@ -60,11 +60,15 @@ export const formatUtils = {
* @param bannerChar Character to use for the banner * @param bannerChar Character to use for the banner
* @returns Array of formatted lines * @returns Array of formatted lines
*/ */
formatSection(header: string, content: string, bannerChar: string = '='): string[] { formatSection(header: string, content: string, bannerChar: string = '=', trailingNewline: boolean = false): string[] {
return [ const parts = [
this.formatCheckbox(header), this.formatCheckbox(header),
this.createBanner(bannerChar), this.createBanner(bannerChar),
content, content,
]; ];
if (trailingNewline) {
parts.push('');
}
return parts;
} }
}; };

View File

@ -0,0 +1,33 @@
export const PRINTER_CONFIG = {
ENCODING: 'CP437',
DEFAULT_USER_ID: 1,
} as const;
export const FONT_SIZES = {
NORMAL: { width: 1, height: 1 }, // 0.08 x 2.13 mm
SMALL: { width: 1, height: 1 }, // Same as normal for now, but can be adjusted if needed
LARGE: { width: 2, height: 2 }, // Double size
} as const;
export const BARCODE_CONFIG = {
TYPE: 'CODE128',
DIMENSIONS: { width: 2, height: 50 },
} as const;
export const PAPER_CONFIG = {
BANNER_LENGTH: 32,
CUT_LINES: 2,
} as const;
export const ALIGNMENT = {
CENTER: 'ct',
LEFT: 'lt',
} as const;
export const FONT = {
DEFAULT: 'a',
} as const;
export const STYLE = {
BOLD: 'b',
} as const;

View File

@ -5,6 +5,15 @@ import { StepRepository, PrintHistoryRepository } from '../db/repositories';
import { Knex } from 'knex'; import { Knex } from 'knex';
import logger from '../logger'; import logger from '../logger';
import { formatUtils } from './format-utils'; import { formatUtils } from './format-utils';
import {
PRINTER_CONFIG,
FONT_SIZES,
BARCODE_CONFIG,
PAPER_CONFIG,
ALIGNMENT,
FONT,
STYLE,
} from './printer-constants';
export class SerialPrinter implements PrinterInterface { export class SerialPrinter implements PrinterInterface {
private device: USB | null = null; private device: USB | null = null;
@ -32,7 +41,7 @@ export class SerialPrinter implements PrinterInterface {
}); });
}); });
const options = { encoding: 'CP437' }; const options = { encoding: PRINTER_CONFIG.ENCODING };
this.printer = new Printer(this.device, options); this.printer = new Printer(this.device, options);
logger.info('Printer initialized successfully'); logger.info('Printer initialized successfully');
} catch (error) { } catch (error) {
@ -54,18 +63,18 @@ export class SerialPrinter implements PrinterInterface {
try { try {
// Print header with task ID as barcode // Print header with task ID as barcode
await this.printer await this.printer
.font('a') .font(FONT.DEFAULT)
.align('ct') .align(ALIGNMENT.CENTER)
.style('b') .style(STYLE.BOLD)
.size(1, 1) // Normal size (0.08 x 2.13 mm) .size(FONT_SIZES.LARGE.width, FONT_SIZES.LARGE.height)
.text(formatUtils.formatCheckbox(`Task: ${task.name}`)) .text(formatUtils.formatCheckbox(`Task: ${task.name}`))
.text(formatUtils.createBanner('=', 32)) .text(formatUtils.createBanner('=', PAPER_CONFIG.BANNER_LENGTH))
.text('') .text('')
.align('lt'); .align(ALIGNMENT.LEFT);
// Print task ID as barcode // Print task ID as barcode
await this.printer await this.printer
.barcode(task.id.toString(), 'CODE128', { width: 2, height: 50 }) .barcode(task.id.toString(), BARCODE_CONFIG.TYPE, BARCODE_CONFIG.DIMENSIONS)
.text('') .text('')
.text(''); .text('');
@ -79,27 +88,27 @@ export class SerialPrinter implements PrinterInterface {
); );
await this.printer await this.printer
.size(1, 1) // Normal size for step header .size(FONT_SIZES.NORMAL.width, FONT_SIZES.NORMAL.height)
.text(stepSection[0]) // Header with checkbox .text(stepSection[0])
.text(stepSection[1]) // Banner .text(stepSection[1])
.size(0, 0) // Smaller size for instructions .size(FONT_SIZES.SMALL.width, FONT_SIZES.SMALL.height)
.text(stepSection[3]) // Instructions .text(stepSection[3])
.text(''); .text('');
// Print step ID as barcode // Print step ID as barcode
await this.printer await this.printer
.barcode(step.id.toString(), 'CODE128', { width: 2, height: 50 }) .barcode(step.id.toString(), BARCODE_CONFIG.TYPE, BARCODE_CONFIG.DIMENSIONS)
.text(''); .text('');
} }
await this.printer await this.printer
.cut(true, 2) .cut(true, PAPER_CONFIG.CUT_LINES)
.close(); .close();
logger.info(`Printed task ${task.id}`); logger.info(`Printed task ${task.id}`);
await this.printHistoryRepo.create({ await this.printHistoryRepo.create({
user_id: 1, // Replace with actual user ID if available user_id: PRINTER_CONFIG.DEFAULT_USER_ID,
task_id: task.id, task_id: task.id,
printed_at: new Date(), printed_at: new Date(),
}); });
@ -125,29 +134,29 @@ export class SerialPrinter implements PrinterInterface {
); );
await this.printer await this.printer
.font('a') .font(FONT.DEFAULT)
.align('ct') .align(ALIGNMENT.CENTER)
.style('b') .style(STYLE.BOLD)
.size(1, 1) // Normal size (0.08 x 2.13 mm) .size(FONT_SIZES.LARGE.width, FONT_SIZES.LARGE.height)
.text(stepSection[0]) // Header with checkbox .text(stepSection[0])
.text(stepSection[1]) // Banner .text(stepSection[1])
.text('') .text('')
.align('lt') .align(ALIGNMENT.LEFT)
.size(0, 0) // Smaller size for instructions .size(FONT_SIZES.NORMAL.width, FONT_SIZES.NORMAL.height)
.text(stepSection[3]) // Instructions .text(stepSection[3])
.text('') .text('')
.text(''); .text('');
// Print step ID as barcode // Print step ID as barcode
await this.printer await this.printer
.barcode(step.id.toString(), 'CODE128', { width: 2, height: 50 }) .barcode(step.id.toString(), BARCODE_CONFIG.TYPE, BARCODE_CONFIG.DIMENSIONS)
.cut(true, 2) .cut(true, PAPER_CONFIG.CUT_LINES)
.close(); .close();
logger.info(`Printed step ${step.id}`); logger.info(`Printed step ${step.id}`);
await this.printHistoryRepo.create({ await this.printHistoryRepo.create({
user_id: 1, // Replace with actual user ID if available user_id: PRINTER_CONFIG.DEFAULT_USER_ID,
step_id: step.id, step_id: step.id,
printed_at: new Date(), printed_at: new Date(),
}); });

View File

@ -41,7 +41,8 @@ export class TestPrinter implements Printer {
formatUtils.formatSection( formatUtils.formatSection(
formatUtils.formatStepHeader(step.name, index + 1, task.name, true), formatUtils.formatStepHeader(step.name, index + 1, task.name, true),
step.instructions, step.instructions,
'-' '-',
true,
) )
).flat(), ).flat(),
].join('\n'); ].join('\n');