Skip to content

Commit 9db3cd1

Browse files
committed
move balloon template to config
1 parent 82b2481 commit 9db3cd1

File tree

6 files changed

+75
-79
lines changed

6 files changed

+75
-79
lines changed

package.json

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "xcpc-tools-workspace",
3-
"packageManager": "yarn@4.2.0",
3+
"packageManager": "yarn@4.9.2",
44
"private": true,
55
"workspaces": [
66
"packages/*",
@@ -15,17 +15,17 @@
1515
"build:pkg": "yarn build:ui && node -r ./register.js build.ts && pkg dist/xcpc-tools.js --targets linux,macos,win --out-path dist/pkg"
1616
},
1717
"devDependencies": {
18-
"@expo-google-fonts/noto-sans-sc": "^0.4.1",
18+
"@expo-google-fonts/noto-sans-sc": "^0.4.2",
1919
"@hydrooj/eslint-config": "^2.0.3",
2020
"@hydrooj/register": "^1.0.3",
21-
"@hydrooj/utils": "^1.4.36",
22-
"@neutralinojs/neu": "^11.5.0",
21+
"@hydrooj/utils": "^1.4.37",
22+
"@neutralinojs/neu": "^11.6.0",
2323
"@types/lodash": "^4.17.20",
24-
"@types/node": "^20.19.10",
25-
"@yao-pkg/pkg": "^6.6.0",
24+
"@types/node": "^20.19.23",
25+
"@yao-pkg/pkg": "^6.9.0",
2626
"chardet": "^2.1.0",
2727
"dejavu-fonts-ttf": "^2.37.3",
28-
"eslint": "^9.33.0",
28+
"eslint": "^9.38.0",
2929
"eslint-import-resolver-typescript": "^4.4.4",
3030
"iconv-lite": "^0.6.3",
3131
"lodash": "^4.17.21",

packages/machine-setup/frontend/package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@
99
"preview": "vite preview"
1010
},
1111
"dependencies": {
12-
"@neutralinojs/lib": "^6.2.0",
13-
"vue": "^3.5.18"
12+
"@neutralinojs/lib": "^6.3.0",
13+
"vue": "^3.5.22"
1414
},
1515
"devDependencies": {
1616
"@vitejs/plugin-vue": "^6.0.1",
17-
"naive-ui": "^2.42.0",
17+
"naive-ui": "^2.43.1",
1818
"typescript": "5.8.3",
19-
"vite": "^7.1.2",
19+
"vite": "^7.1.12",
2020
"vue-tsc": "^2.2.12"
2121
}
2222
}

packages/server/client/balloon.ts

Lines changed: 26 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
/* eslint-disable no-await-in-loop */
2-
import path from 'path';
32
import EscPosEncoder from '@freedom_sky/esc-pos-encoder';
4-
import fs from 'fs-extra';
53
import superagent from 'superagent';
64
import { config } from '../config';
75
import {
@@ -33,65 +31,37 @@ const i18n = {
3331
},
3432
};
3533

36-
let template = [
37-
'#align center',
38-
'',
39-
'#bold true',
40-
'#size 2',
41-
'#receipt',
42-
'',
43-
'#id',
44-
'',
45-
'#bold false',
46-
'#size 1',
47-
'===============================',
48-
'',
49-
'#location',
50-
'#problem',
51-
'#color',
52-
'#comment',
53-
'',
54-
'#align center',
55-
'#bold true',
56-
'===============================',
57-
'',
58-
'#size 0',
59-
'#team',
60-
'#status',
61-
'#time',
62-
];
63-
try {
64-
template = fs.readFileSync(path.resolve(process.cwd(), 'balloon.template'), 'utf8').split('\n').map((i) => i.trim());
65-
} catch (e) {
66-
logger.info('Using builtin balloon template');
67-
}
68-
6934
export const receiptBalloonText = (
7035
id: number, location: string, problem: string, color: string, comment: string, teamname: string, status: string, lang: 'zh' | 'en' = 'zh',
7136
) => {
7237
let enc = encoder.initialize().codepage('cp936').setPinterType(config.balloonType ?? 80);
73-
const commands = {
74-
align: (align: 'center' | 'left' | 'right') => enc.align(align),
75-
emptyLine: (lines: number) => enc.emptyLine(lines),
76-
bold: (bold: boolean) => enc.bold(bold),
77-
size: (size: number) => enc.size(size),
78-
line: (line: string) => enc.line(line),
79-
oneLine: (left: string, right: string) => enc.oneLine(left, right),
80-
cut: () => enc.cut(),
81-
id: () => enc.line(`ID: ${String(id).substring(0, 8)}`),
82-
location: () => enc.oneLine(i18n[lang].location, location),
83-
problem: () => enc.oneLine(i18n[lang].problem, problem),
84-
color: () => enc.oneLine(i18n[lang].color, color),
85-
comment: () => enc.oneLine(i18n[lang].comment, comment),
86-
team: () => enc.line(`${i18n[lang].team}: ${teamname}`),
87-
status: () => enc.line(`${i18n[lang].status}:\n${status}`),
88-
time: () => enc.line(`Time: ${new Date().toLocaleString()}`),
89-
};
90-
for (const line of template) {
91-
if (!line.startsWith('#')) enc = enc.line(line);
38+
const whitelist = [
39+
'align', 'barcode', 'bold', 'cut', 'curPartial',
40+
'emptyLine', 'image', 'italic', 'line', 'newLine',
41+
'oneLine', 'qrcode', 'size', 'text', 'underline',
42+
];
43+
const replace = (input: string) => input
44+
.replace(/\{id\}/g, String(id).substring(0, 8))
45+
.replace(/\{location\}/g, location)
46+
.replace(/\{problem\}/g, problem)
47+
.replace(/\{color\}/g, color)
48+
.replace(/\{comment\}/g, comment)
49+
.replace(/\{team\}/g, teamname)
50+
.replace(/\{status\}/g, status)
51+
.replace(/\{time\}/g, new Date().toLocaleString())
52+
.replace(/%LOCATION/g, i18n[lang].location)
53+
.replace(/%PROBLEM/g, i18n[lang].problem)
54+
.replace(/%COLOR/g, i18n[lang].color)
55+
.replace(/%COMMENT/g, i18n[lang].comment)
56+
.replace(/%TEAM/g, i18n[lang].team)
57+
.replace(/%STATUS/g, i18n[lang].status)
58+
.replace(/%RECEIPT/g, i18n[lang].receipt);
59+
for (const line of config.balloonTemplate.split('\n')) {
60+
if (!line.startsWith('#')) enc = enc.line(replace(line));
9261
const [command, ...rawArgs] = line.slice(1).split(' ');
93-
const args = rawArgs.map((arg) => (arg === 'true' ? true : arg === 'false' ? false : Number.isSafeInteger(arg) ? +arg : arg));
94-
if (commands[command]) enc = commands[command](...args);
62+
const args = rawArgs.map((arg) => (arg === 'true' ? true : arg === 'false' ? false : Number.isSafeInteger(arg) ? +arg : replace(arg)));
63+
if (whitelist.includes(command)) enc = enc[command](...args);
64+
else logger.warn(`Unsupported printer command: ${command}`);
9565
}
9666
return enc
9767
.line('Powered by hydro-dev/xcpc-tools')

packages/server/config.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,30 @@ const isClient = process.argv.includes('--client');
1313
const configPath = path.resolve(process.cwd(), `config.${isClient ? 'client' : 'server'}.yaml`);
1414
fs.ensureDirSync(path.resolve(process.cwd(), 'data'));
1515

16+
const balloonTemplateDefault = `\
17+
#align center
18+
19+
#bold true
20+
#size 2
21+
%RECEIPT
22+
23+
{id}
24+
25+
#bold false
26+
#size 1
27+
===============================
28+
29+
#oneLine %LOCATION {location}
30+
#oneLine %PROBLEM {problem}
31+
#oneLine %COLOR {color}
32+
#oneLine %COMMENT {comment}
33+
#align center
34+
#bold true
35+
#size 0
36+
%TEAM: {team}
37+
%STATUS: {status}
38+
Time: {time}`;
39+
1640
// eslint-disable-next-line import/no-mutable-exports
1741
export let exit: Promise<void> | null = null;
1842

@@ -47,6 +71,7 @@ monitor:
4771
balloonType: 80,
4872
printColor: false,
4973
printers,
74+
balloonTemplate: balloonTemplateDefault,
5075
});
5176
fs.writeFileSync(configPath, isClient ? clientConfigDefault : serverConfigDefault);
5277
logger.error('Config file generated, please fill in the config.yaml');
@@ -96,6 +121,7 @@ const clientSchema = Schema.object({
96121
balloonLang: Schema.union(['zh', 'en']).default('zh').required(),
97122
balloonType: Schema.union([58, 80, 'plain']).default(80),
98123
balloonCommand: Schema.string().default(''),
124+
balloonTemplate: Schema.string().default(balloonTemplateDefault),
99125
printColor: Schema.boolean().default(false),
100126
printPageMax: Schema.number().default(5),
101127
printMergeQueue: Schema.number().default(1),

packages/server/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"@cordisjs/plugin-logger": "^1.0.1",
1919
"@cordisjs/plugin-timer": "^1.1.1",
2020
"@freedom_sky/esc-pos-encoder": "^5.9.0",
21-
"@hydrooj/framework": "0.2.1",
21+
"@hydrooj/framework": "0.2.4",
2222
"@myriaddreamin/typst-ts-web-compiler": "0.6.1-rc2",
2323
"@myteril/node-win-printer": "patch:@myteril/node-win-printer@npm%3A1.1.5#~/.yarn/patches/@myteril-node-win-printer-npm-1.1.5-c62910fcfd.patch",
2424
"base16384": "^1.0.0",
@@ -27,7 +27,7 @@
2727
"nedb-promises": "^6.2.3",
2828
"pdf-lib": "^1.17.1",
2929
"prom-client": "^15.1.3",
30-
"schemastery": "^3.16.1",
30+
"schemastery": "^3.17.1",
3131
"superagent": "^10.2.3",
3232
"unix-print": "^1.3.2",
3333
"ws": "^8.18.3"

packages/ui/package.json

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@
1010
"preferUnplugged": true,
1111
"devDependencies": {
1212
"@hydrooj/xcpc-tools": "workspace:*",
13-
"@mantine/core": "^8.2.4",
14-
"@mantine/form": "^8.2.4",
15-
"@mantine/hooks": "^8.2.4",
16-
"@mantine/modals": "^8.2.4",
17-
"@mantine/notifications": "^8.2.4",
18-
"@mantine/nprogress": "^8.2.4",
19-
"@tabler/icons-react": "^3.34.1",
20-
"@tanstack/react-query": "^5.85.0",
21-
"@types/react": "^18.3.23",
13+
"@mantine/core": "^8.3.5",
14+
"@mantine/form": "^8.3.5",
15+
"@mantine/hooks": "^8.3.5",
16+
"@mantine/modals": "^8.3.5",
17+
"@mantine/notifications": "^8.3.5",
18+
"@mantine/nprogress": "^8.3.5",
19+
"@tabler/icons-react": "^3.35.0",
20+
"@tanstack/react-query": "^5.90.5",
21+
"@types/react": "^18.3.26",
2222
"@types/react-dom": "^18.3.7",
2323
"css-loader": "^6.11.0",
2424
"esbuild-loader": "^4.3.0",
@@ -31,7 +31,7 @@
3131
"react-dom": "^18.3.1",
3232
"react-router-dom": "^6.30.1",
3333
"style-loader": "^3.3.4",
34-
"webpack": "^5.101.1",
34+
"webpack": "^5.102.1",
3535
"webpack-dev-server": "^5.2.2"
3636
}
3737
}

0 commit comments

Comments
 (0)