@nbtca/prompt 1.5.20 → 1.5.22
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 +32 -2
- package/SECURITY.md +7 -2
- package/dist/app/views/schedule-render.js +4 -0
- package/dist/app/views/schedule.js +148 -12
- package/dist/app/views/student-records-render.js +268 -0
- package/dist/app/views/student-records.js +309 -0
- package/dist/auth/cookie-transport.js +8 -2
- package/dist/cli.js +18 -0
- package/dist/config/campus.js +2 -0
- package/dist/features/student-records-client.js +19 -0
- package/dist/features/student-records-render.js +50 -0
- package/dist/features/student-records.js +61 -0
- package/dist/features/student-timetable.js +23 -4
- package/dist/i18n/locales/en.json +60 -0
- package/dist/i18n/locales/zh.json +60 -0
- package/package.json +2 -2
|
@@ -0,0 +1,309 @@
|
|
|
1
|
+
import { JWXT_ORIGIN } from '../../config/campus.js';
|
|
2
|
+
import { wrapAnsiWithIndent } from '../../core/text.js';
|
|
3
|
+
import { space, type, glyph } from '../../core/theme.js';
|
|
4
|
+
import { setVimKeysActive } from '../../core/vim-keys.js';
|
|
5
|
+
import { isSessionExpired, safeMessage, displaySemesterLabel, } from '../../features/student-timetable.js';
|
|
6
|
+
import { createStudentRecordsClient } from '../../features/student-records-client.js';
|
|
7
|
+
import { t } from '../../i18n/index.js';
|
|
8
|
+
import { ListField, computeMaxVisible, renderListFieldWithContext } from '../fields/list-field.js';
|
|
9
|
+
import { TextField } from '../fields/text-field.js';
|
|
10
|
+
import { fitFooterHint, passiveFooterHint } from '../chrome.js';
|
|
11
|
+
import { renderStudentRecordsPanel, renderStudentRecordDetail, visibleStudentRecords, } from './student-records-render.js';
|
|
12
|
+
export class StudentRecordsPanel {
|
|
13
|
+
kind;
|
|
14
|
+
term;
|
|
15
|
+
catalog;
|
|
16
|
+
transport;
|
|
17
|
+
isActive;
|
|
18
|
+
onSessionExpired;
|
|
19
|
+
data;
|
|
20
|
+
error;
|
|
21
|
+
termsField;
|
|
22
|
+
searchField;
|
|
23
|
+
previousQuery = '';
|
|
24
|
+
query = '';
|
|
25
|
+
attention = false;
|
|
26
|
+
selected = 0;
|
|
27
|
+
pageSize = 1;
|
|
28
|
+
detailIndex;
|
|
29
|
+
controller;
|
|
30
|
+
pending = false;
|
|
31
|
+
results = new Map();
|
|
32
|
+
constructor(kind, term, catalog, transport, isActive, onSessionExpired) {
|
|
33
|
+
this.kind = kind;
|
|
34
|
+
this.term = term;
|
|
35
|
+
this.catalog = catalog;
|
|
36
|
+
this.transport = transport;
|
|
37
|
+
this.isActive = isActive;
|
|
38
|
+
this.onSessionExpired = onSessionExpired;
|
|
39
|
+
}
|
|
40
|
+
async load(ctx) {
|
|
41
|
+
this.controller?.abort();
|
|
42
|
+
const controller = new AbortController();
|
|
43
|
+
this.controller = controller;
|
|
44
|
+
const signal = ctx.signal
|
|
45
|
+
? AbortSignal.any([ctx.signal, controller.signal])
|
|
46
|
+
: controller.signal;
|
|
47
|
+
this.pending = true;
|
|
48
|
+
this.error = undefined;
|
|
49
|
+
this.data = undefined;
|
|
50
|
+
this.results.delete(this.kind);
|
|
51
|
+
this.termsField = undefined;
|
|
52
|
+
this.detailIndex = undefined;
|
|
53
|
+
ctx.resetScroll();
|
|
54
|
+
ctx.rerender();
|
|
55
|
+
try {
|
|
56
|
+
const data = await createStudentRecordsClient(this.transport, JWXT_ORIGIN).fetchRecords(this.kind, this.term, signal);
|
|
57
|
+
if (signal.aborted || !this.isActive())
|
|
58
|
+
return;
|
|
59
|
+
this.data = data;
|
|
60
|
+
this.results.set(data.kind, data);
|
|
61
|
+
const next = data.kind === 'exams'
|
|
62
|
+
? data.items.findIndex((exam) => exam.start && exam.end && exam.end > new Date())
|
|
63
|
+
: -1;
|
|
64
|
+
this.selected = Math.max(0, this.indices().indexOf(next));
|
|
65
|
+
}
|
|
66
|
+
catch (error) {
|
|
67
|
+
if (signal.aborted || !this.isActive())
|
|
68
|
+
return;
|
|
69
|
+
if (isSessionExpired(error))
|
|
70
|
+
this.onSessionExpired();
|
|
71
|
+
else
|
|
72
|
+
this.error = safeMessage(error);
|
|
73
|
+
}
|
|
74
|
+
finally {
|
|
75
|
+
if (!signal.aborted && this.isActive()) {
|
|
76
|
+
this.pending = false;
|
|
77
|
+
ctx.rerender();
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
dispose() {
|
|
82
|
+
this.controller?.abort();
|
|
83
|
+
this.data = undefined;
|
|
84
|
+
this.results.clear();
|
|
85
|
+
this.termsField = undefined;
|
|
86
|
+
this.searchField = undefined;
|
|
87
|
+
this.query = '';
|
|
88
|
+
setVimKeysActive(true);
|
|
89
|
+
}
|
|
90
|
+
isBusy() {
|
|
91
|
+
return this.pending;
|
|
92
|
+
}
|
|
93
|
+
capturesInput() {
|
|
94
|
+
return this.searchField !== undefined;
|
|
95
|
+
}
|
|
96
|
+
capturesPageKeys() {
|
|
97
|
+
return this.detailIndex === undefined;
|
|
98
|
+
}
|
|
99
|
+
acceptsActions() {
|
|
100
|
+
return !this.termsField && !this.searchField;
|
|
101
|
+
}
|
|
102
|
+
contextPath() {
|
|
103
|
+
return [
|
|
104
|
+
t().records[this.kind],
|
|
105
|
+
`${this.term.academicYearLabel} · ${displaySemesterLabel(this.term)}`,
|
|
106
|
+
];
|
|
107
|
+
}
|
|
108
|
+
footerHint(tabCount, cols) {
|
|
109
|
+
if (this.searchField)
|
|
110
|
+
return fitFooterHint(cols, `${t().records.searchFooter} · Ctrl+C ${t().menu.hintQuit}`, 'Enter · Esc · Ctrl+C', 'Esc Ctrl+C');
|
|
111
|
+
if (this.pending)
|
|
112
|
+
return passiveFooterHint(tabCount, cols);
|
|
113
|
+
if (this.termsField || this.detailIndex !== undefined)
|
|
114
|
+
return passiveFooterHint(tabCount, cols);
|
|
115
|
+
const trans = t();
|
|
116
|
+
return fitFooterHint(cols, `${glyph.updown()} ${trans.records.moveAction} · ${glyph.enter()} ${trans.records.detailAction} · ←→ ${trans.records.switchView} · Esc ${trans.menu.hintBack} · ? ${trans.help.title}`, `${glyph.updown()} ${glyph.enter()} ${trans.records.detailAction} · ←→ ${trans.records.switchView} · Esc · ?`, `${glyph.updown()} ${glyph.enter()} · ←→ · Esc · ?`, 'Esc · ?');
|
|
117
|
+
}
|
|
118
|
+
handleBack() {
|
|
119
|
+
if (this.searchField) {
|
|
120
|
+
this.query = this.previousQuery;
|
|
121
|
+
this.searchField = undefined;
|
|
122
|
+
this.selected = 0;
|
|
123
|
+
setVimKeysActive(true);
|
|
124
|
+
return true;
|
|
125
|
+
}
|
|
126
|
+
if (this.termsField) {
|
|
127
|
+
this.termsField = undefined;
|
|
128
|
+
return true;
|
|
129
|
+
}
|
|
130
|
+
if (this.detailIndex !== undefined) {
|
|
131
|
+
this.detailIndex = undefined;
|
|
132
|
+
return true;
|
|
133
|
+
}
|
|
134
|
+
if (this.query || this.attention) {
|
|
135
|
+
this.query = '';
|
|
136
|
+
this.attention = false;
|
|
137
|
+
this.selected = 0;
|
|
138
|
+
return true;
|
|
139
|
+
}
|
|
140
|
+
return false;
|
|
141
|
+
}
|
|
142
|
+
shortcuts() {
|
|
143
|
+
if (this.searchField || this.termsField)
|
|
144
|
+
return [];
|
|
145
|
+
if (this.pending)
|
|
146
|
+
return [
|
|
147
|
+
{ key: '← →', label: t().records.switchView },
|
|
148
|
+
{ key: 'x', label: t().timetable.hubLogout },
|
|
149
|
+
];
|
|
150
|
+
const trans = t();
|
|
151
|
+
return [
|
|
152
|
+
...(this.detailIndex === undefined
|
|
153
|
+
? [
|
|
154
|
+
{ key: glyph.updown(), label: trans.records.moveAction },
|
|
155
|
+
{ key: glyph.enter(), label: trans.records.detailAction },
|
|
156
|
+
]
|
|
157
|
+
: []),
|
|
158
|
+
{ key: '← →', label: trans.records.switchView },
|
|
159
|
+
...(this.detailIndex === undefined
|
|
160
|
+
? [
|
|
161
|
+
{ key: '/', label: trans.records.searchAction },
|
|
162
|
+
{
|
|
163
|
+
key: 'f',
|
|
164
|
+
label: this.kind === 'exams'
|
|
165
|
+
? trans.records.upcomingFilter
|
|
166
|
+
: trans.records.attentionFilter,
|
|
167
|
+
},
|
|
168
|
+
]
|
|
169
|
+
: []),
|
|
170
|
+
{ key: 's', label: trans.records.switchTerm },
|
|
171
|
+
{ key: 'r', label: trans.records.refresh },
|
|
172
|
+
{ key: 'x', label: trans.timetable.hubLogout },
|
|
173
|
+
];
|
|
174
|
+
}
|
|
175
|
+
render(ctx) {
|
|
176
|
+
if (this.termsField) {
|
|
177
|
+
this.termsField.setMaxVisible(computeMaxVisible(ctx.bodyRows));
|
|
178
|
+
return renderListFieldWithContext([], this.termsField, ctx.bodyRows, ctx.size.cols);
|
|
179
|
+
}
|
|
180
|
+
if (this.detailIndex !== undefined && this.data) {
|
|
181
|
+
const index = this.detailIndex;
|
|
182
|
+
return [
|
|
183
|
+
...wrapAnsiWithIndent(type.hint(t().records.detailHint), ctx.size.cols, space.indent),
|
|
184
|
+
'',
|
|
185
|
+
...renderStudentRecordDetail(this.data, index, Math.max(1, ctx.size.cols - 6), new Date()).map((line) => space.indent + line),
|
|
186
|
+
];
|
|
187
|
+
}
|
|
188
|
+
const rendered = renderStudentRecordsPanel({
|
|
189
|
+
kind: this.kind,
|
|
190
|
+
term: this.term,
|
|
191
|
+
data: this.data,
|
|
192
|
+
pending: this.pending,
|
|
193
|
+
error: this.error,
|
|
194
|
+
selected: this.selected,
|
|
195
|
+
query: this.query,
|
|
196
|
+
attention: this.attention,
|
|
197
|
+
searching: this.searchField !== undefined,
|
|
198
|
+
}, ctx.bodyRows, ctx.size.cols, new Date());
|
|
199
|
+
this.pageSize = rendered.pageSize;
|
|
200
|
+
return rendered.lines;
|
|
201
|
+
}
|
|
202
|
+
indices() {
|
|
203
|
+
return visibleStudentRecords(this.data, this.query, this.attention, new Date());
|
|
204
|
+
}
|
|
205
|
+
handleKey(key, ctx) {
|
|
206
|
+
if (this.searchField) {
|
|
207
|
+
const result = this.searchField.handleKey(key);
|
|
208
|
+
this.query = this.searchField.currentValue;
|
|
209
|
+
this.selected = 0;
|
|
210
|
+
if (result.cancelled)
|
|
211
|
+
this.handleBack();
|
|
212
|
+
else if (result.submitted !== undefined) {
|
|
213
|
+
this.searchField = undefined;
|
|
214
|
+
setVimKeysActive(true);
|
|
215
|
+
}
|
|
216
|
+
return;
|
|
217
|
+
}
|
|
218
|
+
if (this.termsField) {
|
|
219
|
+
const result = this.termsField.handleKey(key);
|
|
220
|
+
if (result.cancelled || result.selected === '__back__')
|
|
221
|
+
this.termsField = undefined;
|
|
222
|
+
else if (result.selected) {
|
|
223
|
+
const term = this.catalog.find((candidate) => `${candidate.academicYear}:${candidate.semester}` === result.selected);
|
|
224
|
+
if (term) {
|
|
225
|
+
this.term = term;
|
|
226
|
+
this.results.clear();
|
|
227
|
+
this.query = '';
|
|
228
|
+
this.attention = false;
|
|
229
|
+
this.selected = 0;
|
|
230
|
+
void this.load(ctx);
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
return;
|
|
234
|
+
}
|
|
235
|
+
if (key === '\x1b[C' || key === '\x1b[D') {
|
|
236
|
+
this.controller?.abort();
|
|
237
|
+
this.kind = this.kind === 'exams' ? 'grades' : 'exams';
|
|
238
|
+
this.detailIndex = undefined;
|
|
239
|
+
this.query = '';
|
|
240
|
+
this.attention = false;
|
|
241
|
+
this.selected = 0;
|
|
242
|
+
this.error = undefined;
|
|
243
|
+
ctx.resetScroll();
|
|
244
|
+
const cached = this.results.get(this.kind);
|
|
245
|
+
if (cached) {
|
|
246
|
+
this.data = cached;
|
|
247
|
+
this.pending = false;
|
|
248
|
+
}
|
|
249
|
+
else
|
|
250
|
+
void this.load(ctx);
|
|
251
|
+
return;
|
|
252
|
+
}
|
|
253
|
+
if (this.pending)
|
|
254
|
+
return;
|
|
255
|
+
if (key === 'r') {
|
|
256
|
+
void this.load(ctx);
|
|
257
|
+
return;
|
|
258
|
+
}
|
|
259
|
+
if (key === 's') {
|
|
260
|
+
this.termsField = new ListField({
|
|
261
|
+
title: t().records.switchTerm,
|
|
262
|
+
options: [
|
|
263
|
+
...this.catalog.map((term) => ({
|
|
264
|
+
value: `${term.academicYear}:${term.semester}`,
|
|
265
|
+
label: `${term.academicYearLabel} · ${displaySemesterLabel(term)}`,
|
|
266
|
+
})),
|
|
267
|
+
{ value: '__back__', label: t().common.back },
|
|
268
|
+
],
|
|
269
|
+
initialIndex: Math.max(0, this.catalog.findIndex((term) => term.academicYear === this.term.academicYear && term.semester === this.term.semester)),
|
|
270
|
+
});
|
|
271
|
+
ctx.resetScroll();
|
|
272
|
+
return;
|
|
273
|
+
}
|
|
274
|
+
if (this.detailIndex !== undefined)
|
|
275
|
+
return;
|
|
276
|
+
if (key === '/') {
|
|
277
|
+
this.previousQuery = this.query;
|
|
278
|
+
this.searchField = new TextField({ message: t().records.searchPrompt, allowEmpty: true });
|
|
279
|
+
this.query = '';
|
|
280
|
+
this.selected = 0;
|
|
281
|
+
setVimKeysActive(false);
|
|
282
|
+
return;
|
|
283
|
+
}
|
|
284
|
+
if (key === 'f') {
|
|
285
|
+
this.attention = !this.attention;
|
|
286
|
+
this.selected = 0;
|
|
287
|
+
return;
|
|
288
|
+
}
|
|
289
|
+
const count = this.indices().length;
|
|
290
|
+
const last = Math.max(0, count - 1);
|
|
291
|
+
this.selected = Math.min(last, this.selected);
|
|
292
|
+
if (key === '\x1b[A')
|
|
293
|
+
this.selected = Math.max(0, this.selected - 1);
|
|
294
|
+
if (key === '\x1b[B')
|
|
295
|
+
this.selected = Math.min(last, this.selected + 1);
|
|
296
|
+
if (key === '\x1b[5~')
|
|
297
|
+
this.selected = Math.max(0, this.selected - this.pageSize);
|
|
298
|
+
if (key === '\x1b[6~' || key === ' ')
|
|
299
|
+
this.selected = Math.min(last, this.selected + this.pageSize);
|
|
300
|
+
if (key === '\x1b[H')
|
|
301
|
+
this.selected = 0;
|
|
302
|
+
if (key === '\x1b[F')
|
|
303
|
+
this.selected = last;
|
|
304
|
+
if ((key === '\r' || key === '\n') && count > 0) {
|
|
305
|
+
this.detailIndex = this.indices()[this.selected];
|
|
306
|
+
ctx.resetScroll();
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { createCookieJar } from './cookie-jar.js';
|
|
2
2
|
import { AuthError, SessionExpiredError } from './errors.js';
|
|
3
|
+
import { JWXT_ORIGIN, STUDENT_RECORD_ROUTES } from '../config/campus.js';
|
|
3
4
|
const MAX_REDIRECTS = 8;
|
|
4
5
|
const REDIRECT_STATUSES = new Set([301, 302, 303, 307, 308]);
|
|
5
6
|
const BODY_HEADERS = [
|
|
@@ -11,7 +12,7 @@ const BODY_HEADERS = [
|
|
|
11
12
|
];
|
|
12
13
|
const WEBVPN_HOST = 'webvpn.nbt.edu.cn';
|
|
13
14
|
const AUTH_HOST = 'authserver-443.webvpn.nbt.edu.cn';
|
|
14
|
-
export const JWXT_HOST =
|
|
15
|
+
export const JWXT_HOST = new URL(JWXT_ORIGIN).hostname;
|
|
15
16
|
const WEBVPN_PATHS = new Set([
|
|
16
17
|
'/',
|
|
17
18
|
'/users/sign_in',
|
|
@@ -30,6 +31,7 @@ const JWXT_EXACT_PATHS = new Set([
|
|
|
30
31
|
'/jwglxt/kbcx/xskbcx_cxXskbcxIndex.html',
|
|
31
32
|
'/jwglxt/kbcx/xskbcx_cxXsgrkb.html',
|
|
32
33
|
'/jwglxt/kbcx/xskbcx_cxRjc.html',
|
|
34
|
+
...Object.values(STUDENT_RECORD_ROUTES).flatMap((route) => [route.index, route.query]),
|
|
33
35
|
]);
|
|
34
36
|
function isAllowedJwxtPath(pathname) {
|
|
35
37
|
return JWXT_EXACT_PATHS.has(pathname);
|
|
@@ -100,8 +102,12 @@ export function assertAllowedCampusUrl(url) {
|
|
|
100
102
|
assertAllowedCasService(service);
|
|
101
103
|
return;
|
|
102
104
|
}
|
|
103
|
-
if (hostname === JWXT_HOST && isAllowedJwxtPath(url.pathname))
|
|
105
|
+
if (hostname === JWXT_HOST && isAllowedJwxtPath(url.pathname)) {
|
|
106
|
+
const academicRoute = Object.values(STUDENT_RECORD_ROUTES).some((route) => route.index === url.pathname || route.query === url.pathname);
|
|
107
|
+
if (academicRoute && url.searchParams.getAll('doType').some((action) => action !== 'query'))
|
|
108
|
+
throw new AuthError('UNTRUSTED_URL', 'session', 'Only academic query operations are allowed.');
|
|
104
109
|
return;
|
|
110
|
+
}
|
|
105
111
|
if (hostname === AUTH_HOST && url.pathname === PROFILE_GATE_PATH) {
|
|
106
112
|
throw new AuthError('PROFILE_INCOMPLETE', 'credentials', 'The campus requires this account to complete its profile.');
|
|
107
113
|
}
|
package/dist/cli.js
CHANGED
|
@@ -13,6 +13,7 @@ import { clearScreen, handleGracefulExit } from './core/ui.js';
|
|
|
13
13
|
import { APP_INFO, URLS } from './config/data.js';
|
|
14
14
|
import { runUpdateCheck } from './features/update.js';
|
|
15
15
|
import { runStudentTimetableCommand } from './features/student-timetable.js';
|
|
16
|
+
import { runStudentRecordsCommand } from './features/student-records.js';
|
|
16
17
|
import { showAbout } from './features/about.js';
|
|
17
18
|
import { openUrlInBrowser } from './features/links.js';
|
|
18
19
|
const ACTION_ALIASES = {
|
|
@@ -119,6 +120,12 @@ function getAllowedFlagsFor(command) {
|
|
|
119
120
|
return allowed;
|
|
120
121
|
if (command === 'theme')
|
|
121
122
|
return allowed;
|
|
123
|
+
if (command === 'exams' || command === 'grades') {
|
|
124
|
+
allowed.add('--json');
|
|
125
|
+
allowed.add('--one-shot');
|
|
126
|
+
allowed.add('--no-save');
|
|
127
|
+
return allowed;
|
|
128
|
+
}
|
|
122
129
|
if (command === 'schedule' || command === 'timetable') {
|
|
123
130
|
allowed.add('--one-shot');
|
|
124
131
|
allowed.add('--no-save');
|
|
@@ -154,6 +161,10 @@ function getAllowedFlagsFor(command) {
|
|
|
154
161
|
function getAllowedFlagPrefixesFor(command) {
|
|
155
162
|
if (!command)
|
|
156
163
|
return [];
|
|
164
|
+
if (command === 'exams')
|
|
165
|
+
return ['--term=', '--output='];
|
|
166
|
+
if (command === 'grades')
|
|
167
|
+
return ['--term='];
|
|
157
168
|
if (command === 'schedule' || command === 'timetable') {
|
|
158
169
|
return ['--term=', '--output=', '--week-one='];
|
|
159
170
|
}
|
|
@@ -213,6 +224,8 @@ function printHelp() {
|
|
|
213
224
|
console.log(` status ${trans.menu.statusDesc}`);
|
|
214
225
|
console.log(' schedule <login|logout|status|terms|export>');
|
|
215
226
|
console.log(` ${c.cmdSchedule}`);
|
|
227
|
+
console.log(` exams ${trans.records.exams}`);
|
|
228
|
+
console.log(` grades ${trans.records.grades}`);
|
|
216
229
|
console.log(` website ${c.cmdWebsite}`);
|
|
217
230
|
console.log(` github ${c.cmdGithub}`);
|
|
218
231
|
console.log(` roadmap ${c.cmdRoadmap}`);
|
|
@@ -450,6 +463,11 @@ async function runCommandMode(argv) {
|
|
|
450
463
|
return;
|
|
451
464
|
}
|
|
452
465
|
validateFlags(command, flags);
|
|
466
|
+
if (command === 'exams' || command === 'grades') {
|
|
467
|
+
rejectUnexpectedArguments(command, args);
|
|
468
|
+
process.exitCode = await runStudentRecordsCommand(command, { flags });
|
|
469
|
+
return;
|
|
470
|
+
}
|
|
453
471
|
if (!command) {
|
|
454
472
|
if (!hasInteractiveTerminal()) {
|
|
455
473
|
const cliTrans = t().cli;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { createNbtStudentRecordsClient } from '@nbtca/nbtcal/academic';
|
|
2
|
+
import { sanitizeTerminalLine } from '../core/text.js';
|
|
3
|
+
function sanitizeRecord(record) {
|
|
4
|
+
return Object.fromEntries(Object.entries(record).map(([key, value]) => [
|
|
5
|
+
key,
|
|
6
|
+
typeof value === 'string' ? sanitizeTerminalLine(value) : value,
|
|
7
|
+
]));
|
|
8
|
+
}
|
|
9
|
+
export function createStudentRecordsClient(transport, baseUrl) {
|
|
10
|
+
const client = createNbtStudentRecordsClient(transport, { baseUrl });
|
|
11
|
+
return {
|
|
12
|
+
async fetchRecords(kind, term, signal) {
|
|
13
|
+
const records = await client.fetchRecords(kind, term, signal ? { signal } : {});
|
|
14
|
+
return records.kind === 'exams'
|
|
15
|
+
? { ...records, items: records.items.map(sanitizeRecord) }
|
|
16
|
+
: { ...records, items: records.items.map(sanitizeRecord) };
|
|
17
|
+
},
|
|
18
|
+
};
|
|
19
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { sanitizeTerminalLine, wrapAnsiWithIndent } from '../core/text.js';
|
|
2
|
+
import { fmt, t } from '../i18n/index.js';
|
|
3
|
+
import { displaySemesterLabel } from './student-timetable.js';
|
|
4
|
+
export function renderStudentRecords(data, term, cols = Number.POSITIVE_INFINITY) {
|
|
5
|
+
const trans = t().records;
|
|
6
|
+
const lines = [];
|
|
7
|
+
const add = (value) => {
|
|
8
|
+
lines.push(...wrapAnsiWithIndent(sanitizeTerminalLine(value), cols, ' '));
|
|
9
|
+
};
|
|
10
|
+
add(`${trans[data.kind]} · ${term.academicYearLabel} · ${displaySemesterLabel(term)}`);
|
|
11
|
+
lines.push('');
|
|
12
|
+
if (data.items.length === 0) {
|
|
13
|
+
add(data.kind === 'exams' ? trans.noExams : trans.noGrades);
|
|
14
|
+
return lines;
|
|
15
|
+
}
|
|
16
|
+
if (data.kind === 'exams') {
|
|
17
|
+
for (const exam of data.items) {
|
|
18
|
+
add(exam.courseName + (exam.courseCode ? ` (${exam.courseCode})` : ''));
|
|
19
|
+
add(exam.timeText ?? trans.timeUnavailable);
|
|
20
|
+
if (exam.examName)
|
|
21
|
+
add(exam.examName);
|
|
22
|
+
if (exam.location || exam.campus)
|
|
23
|
+
add([exam.campus, exam.location].filter(Boolean).join(' · '));
|
|
24
|
+
if (exam.seat)
|
|
25
|
+
add(fmt(trans.seat, { value: exam.seat }));
|
|
26
|
+
if (exam.assessment)
|
|
27
|
+
add(exam.assessment);
|
|
28
|
+
lines.push('');
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
else {
|
|
32
|
+
for (const grade of data.items) {
|
|
33
|
+
add(grade.courseName + (grade.courseCode ? ` (${grade.courseCode})` : ''));
|
|
34
|
+
add(fmt(trans.gradeValue, { value: grade.grade ?? trans.notPublished }));
|
|
35
|
+
if (grade.credits !== null)
|
|
36
|
+
add(fmt(trans.credits, { value: grade.credits }));
|
|
37
|
+
if (grade.gradePoint !== null)
|
|
38
|
+
add(fmt(trans.gradePoint, { value: grade.gradePoint }));
|
|
39
|
+
if (grade.examNature || grade.courseNature)
|
|
40
|
+
add([grade.examNature, grade.courseNature].filter(Boolean).join(' · '));
|
|
41
|
+
if (grade.voided === true)
|
|
42
|
+
add(trans.voided);
|
|
43
|
+
if (grade.remark)
|
|
44
|
+
add(grade.remark);
|
|
45
|
+
lines.push('');
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
add(trans.memoryOnly);
|
|
49
|
+
return lines;
|
|
50
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
import { studentExamsToIcs } from '@nbtca/nbtcal/academic';
|
|
3
|
+
import { createNbtTimetableClient } from '@nbtca/nbtcal/timetable';
|
|
4
|
+
import { AuthError } from '../auth/errors.js';
|
|
5
|
+
import { createSessionStore } from '../auth/session-store.js';
|
|
6
|
+
import { JWXT_ORIGIN } from '../config/campus.js';
|
|
7
|
+
import { resolveTerm, PromptCancelledError, safeMessage, withAuthenticatedSession, writePrivateIcs, } from './student-timetable.js';
|
|
8
|
+
import { sanitizeAcademicTerm } from './timetable-sanitize.js';
|
|
9
|
+
import { createStudentRecordsClient } from './student-records-client.js';
|
|
10
|
+
import { renderStudentRecords } from './student-records-render.js';
|
|
11
|
+
import { fmt, t } from '../i18n/index.js';
|
|
12
|
+
export async function runStudentRecordsCommand(kind, options) {
|
|
13
|
+
const stdout = options.stdout ?? process.stdout;
|
|
14
|
+
const stderr = options.stderr ?? process.stderr;
|
|
15
|
+
const isInteractive = options.isInteractive ?? (process.stdin.isTTY && process.stdout.isTTY);
|
|
16
|
+
const outputFlag = [...options.flags].find((flag) => flag.startsWith('--output='));
|
|
17
|
+
const output = outputFlag?.slice('--output='.length);
|
|
18
|
+
if (output !== undefined && (kind !== 'exams' || !output.trim() || options.flags.has('--json'))) {
|
|
19
|
+
stderr.write(`${t().records.invalidExportOptions}\n`);
|
|
20
|
+
return 1;
|
|
21
|
+
}
|
|
22
|
+
const termFlag = [...options.flags].find((flag) => flag.startsWith('--term='));
|
|
23
|
+
if (termFlag === '--term=') {
|
|
24
|
+
stderr.write(`${t().timetable.unknownTerm}\n`);
|
|
25
|
+
return 1;
|
|
26
|
+
}
|
|
27
|
+
try {
|
|
28
|
+
return await withAuthenticatedSession(async (session) => {
|
|
29
|
+
const client = createNbtTimetableClient(session.timetableTransport, {
|
|
30
|
+
baseUrl: JWXT_ORIGIN,
|
|
31
|
+
});
|
|
32
|
+
const terms = (await client.listTerms()).map(sanitizeAcademicTerm);
|
|
33
|
+
const term = resolveTerm(terms, termFlag?.slice('--term='.length));
|
|
34
|
+
const records = await createStudentRecordsClient(session.timetableTransport, JWXT_ORIGIN).fetchRecords(kind, term);
|
|
35
|
+
if (output !== undefined && records.kind === 'exams') {
|
|
36
|
+
writePrivateIcs(output, studentExamsToIcs(records));
|
|
37
|
+
stdout.write(`${fmt(t().records.exported, { file: path.resolve(output) })}\n`);
|
|
38
|
+
return 0;
|
|
39
|
+
}
|
|
40
|
+
stdout.write(options.flags.has('--json')
|
|
41
|
+
? `${JSON.stringify(records, null, 2)}\n`
|
|
42
|
+
: `${renderStudentRecords(records, term).join('\n')}\n`);
|
|
43
|
+
return 0;
|
|
44
|
+
}, {
|
|
45
|
+
oneShot: options.flags.has('--one-shot') || options.flags.has('--no-save'),
|
|
46
|
+
isInteractive,
|
|
47
|
+
store: options.store ?? createSessionStore(),
|
|
48
|
+
stderr,
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
catch (error) {
|
|
52
|
+
if (error instanceof PromptCancelledError)
|
|
53
|
+
return 130;
|
|
54
|
+
if (!isInteractive && error instanceof AuthError && error.code === 'INVALID_CREDENTIALS') {
|
|
55
|
+
stderr.write(`${t().timetable.noSession}\n`);
|
|
56
|
+
return 2;
|
|
57
|
+
}
|
|
58
|
+
stderr.write(`${safeMessage(error)}\n`);
|
|
59
|
+
return 1;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { StudentRecordsError } from '@nbtca/nbtcal/academic';
|
|
1
2
|
import fs from 'node:fs';
|
|
2
3
|
import path from 'node:path';
|
|
3
4
|
import { randomUUID } from 'node:crypto';
|
|
@@ -11,7 +12,8 @@ import { clearScheduleCache, termKey } from './schedule-store.js';
|
|
|
11
12
|
import { sanitizeTerminalLine } from '../core/text.js';
|
|
12
13
|
import { fmt, t } from '../i18n/index.js';
|
|
13
14
|
import { sanitizeAcademicTerm, sanitizeTimetable } from './timetable-sanitize.js';
|
|
14
|
-
|
|
15
|
+
import { JWXT_ORIGIN } from '../config/campus.js';
|
|
16
|
+
export { JWXT_ORIGIN };
|
|
15
17
|
function flagValue(flags, prefix) {
|
|
16
18
|
const flag = [...flags].find((value) => value.startsWith(prefix));
|
|
17
19
|
return flag?.slice(prefix.length);
|
|
@@ -36,7 +38,7 @@ export function relevantTerms(catalog) {
|
|
|
36
38
|
return Number.isInteger(year) && year <= currentYear && year >= currentYear - 4;
|
|
37
39
|
});
|
|
38
40
|
}
|
|
39
|
-
function displaySemesterLabel(term) {
|
|
41
|
+
export function displaySemesterLabel(term) {
|
|
40
42
|
const aliases = { '3': '1', '12': '2', '16': '3' };
|
|
41
43
|
const aliased = aliases[term.semester];
|
|
42
44
|
if (aliased)
|
|
@@ -74,7 +76,8 @@ class IcsWriteError extends Error {
|
|
|
74
76
|
}
|
|
75
77
|
export function isSessionExpired(error) {
|
|
76
78
|
return ((error instanceof AuthError && error.code === 'SESSION_EXPIRED') ||
|
|
77
|
-
(error instanceof TimetableError && error.code === 'SESSION_EXPIRED')
|
|
79
|
+
(error instanceof TimetableError && error.code === 'SESSION_EXPIRED') ||
|
|
80
|
+
(error instanceof StudentRecordsError && error.code === 'SESSION_EXPIRED'));
|
|
78
81
|
}
|
|
79
82
|
export function safeMessage(error) {
|
|
80
83
|
const trans = t().timetable;
|
|
@@ -117,6 +120,22 @@ export function safeMessage(error) {
|
|
|
117
120
|
return trans.genericError;
|
|
118
121
|
}
|
|
119
122
|
}
|
|
123
|
+
if (error instanceof StudentRecordsError) {
|
|
124
|
+
switch (error.code) {
|
|
125
|
+
case 'SESSION_EXPIRED':
|
|
126
|
+
return trans.sessionExpired;
|
|
127
|
+
case 'HTTP_ERROR':
|
|
128
|
+
return trans.httpError;
|
|
129
|
+
case 'NETWORK_ERROR':
|
|
130
|
+
return trans.network;
|
|
131
|
+
case 'INVALID_RECORDS':
|
|
132
|
+
return trans.unexpectedResponse;
|
|
133
|
+
case 'TERM_MISMATCH':
|
|
134
|
+
return trans.termMismatch;
|
|
135
|
+
case 'UNPUBLISHED_EXAM_TIME':
|
|
136
|
+
return t().records.exportNeedsTime;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
120
139
|
if (error instanceof TimetableError) {
|
|
121
140
|
switch (error.code) {
|
|
122
141
|
case 'MISSING_CALENDAR_DATES':
|
|
@@ -148,7 +167,7 @@ export function safeMessage(error) {
|
|
|
148
167
|
}
|
|
149
168
|
return trans.genericError;
|
|
150
169
|
}
|
|
151
|
-
class PromptCancelledError extends Error {
|
|
170
|
+
export class PromptCancelledError extends Error {
|
|
152
171
|
}
|
|
153
172
|
function answered(value) {
|
|
154
173
|
if (value === null)
|
|
@@ -212,6 +212,66 @@
|
|
|
212
212
|
"watchJsonConflict": "--watch cannot be used with --json.",
|
|
213
213
|
"intervalNeedsWatch": "--interval requires --watch."
|
|
214
214
|
},
|
|
215
|
+
"records": {
|
|
216
|
+
"exams": "Exam schedule",
|
|
217
|
+
"grades": "Grades",
|
|
218
|
+
"noExams": "No exam arrangements have been published for this term.",
|
|
219
|
+
"noGrades": "No grades have been published for this term.",
|
|
220
|
+
"timeUnavailable": "Time not published",
|
|
221
|
+
"notPublished": "Not published",
|
|
222
|
+
"seat": "Seat: {value}",
|
|
223
|
+
"gradeValue": "Grade: {value}",
|
|
224
|
+
"credits": "Credits: {value}",
|
|
225
|
+
"gradePoint": "Grade points: {value}",
|
|
226
|
+
"voided": "The school has marked this grade as void.",
|
|
227
|
+
"memoryOnly": "Exam and grade query results are not cached.",
|
|
228
|
+
"refresh": "Refresh",
|
|
229
|
+
"switchTerm": "Switch term",
|
|
230
|
+
"exportNeedsTime": "Some exam times are unpublished. A complete exam calendar cannot be exported yet.",
|
|
231
|
+
"invalidExportOptions": "Use a nonempty --output path for exams without --json.",
|
|
232
|
+
"exported": "Exported the exam calendar to {file}.",
|
|
233
|
+
"pendingTag": "Pending",
|
|
234
|
+
"ended": "Ended",
|
|
235
|
+
"inProgress": "In progress",
|
|
236
|
+
"today": "Today",
|
|
237
|
+
"tomorrow": "Tomorrow",
|
|
238
|
+
"daysAway": "In {days}d",
|
|
239
|
+
"voidTag": "Void",
|
|
240
|
+
"courseNature": "Course type",
|
|
241
|
+
"examNature": "Attempt",
|
|
242
|
+
"location": "Room",
|
|
243
|
+
"campus": "Campus",
|
|
244
|
+
"examName": "Exam",
|
|
245
|
+
"assessment": "Assessment",
|
|
246
|
+
"gradeSummary": "{published}/{total} courses published",
|
|
247
|
+
"examSummary": "{total} exams · {pending} awaiting time",
|
|
248
|
+
"needsAttention": "Needs attention",
|
|
249
|
+
"upcomingOnly": "Upcoming + pending",
|
|
250
|
+
"updated": "Updated",
|
|
251
|
+
"nextExam": "Next exam",
|
|
252
|
+
"course": "Course",
|
|
253
|
+
"result": "Grade",
|
|
254
|
+
"creditLabel": "Credits",
|
|
255
|
+
"pointLabel": "Points",
|
|
256
|
+
"loading": "Querying the selected term…",
|
|
257
|
+
"noMatches": "No courses match this view.",
|
|
258
|
+
"clearFilters": "Press Esc to clear the search and filter.",
|
|
259
|
+
"emptyHint": "Press s to check another term.",
|
|
260
|
+
"termAction": "Term",
|
|
261
|
+
"searchAction": "Search",
|
|
262
|
+
"filterAction": "Filter",
|
|
263
|
+
"detailAction": "Details",
|
|
264
|
+
"moveAction": "Select course",
|
|
265
|
+
"switchView": "Switch exams / grades",
|
|
266
|
+
"searchPrompt": "Search courses",
|
|
267
|
+
"searchHint": "Course name or code",
|
|
268
|
+
"allRecords": "All",
|
|
269
|
+
"detailHint": "Esc returns to the list",
|
|
270
|
+
"searchFooter": "Enter apply · Esc cancel",
|
|
271
|
+
"upcomingFilter": "Upcoming exams",
|
|
272
|
+
"attentionFilter": "Unpublished / void grades",
|
|
273
|
+
"filtered": "Filtered"
|
|
274
|
+
},
|
|
215
275
|
"timetable": {
|
|
216
276
|
"menuTitle": "My Timetable",
|
|
217
277
|
"actionExport": "Export ICS",
|