@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
package/README.md
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
# NBTCA Prompt
|
|
2
2
|
|
|
3
|
-
Club events, the school calendar, your campus timetable, and the
|
|
4
|
-
base in one terminal app.
|
|
3
|
+
Club events, the school calendar, your campus timetable, exams, grades, and the
|
|
4
|
+
NBTCA knowledge base in one terminal app. Cached public content and timetables
|
|
5
|
+
stay readable offline. Exam and grade queries require a connection.
|
|
5
6
|
|
|
6
7
|
[](https://www.npmjs.com/package/@nbtca/prompt)
|
|
7
8
|
[](LICENSE)
|
|
@@ -27,6 +28,12 @@ Or try it without installing: `npx @nbtca/prompt`.
|
|
|
27
28
|
- **Home**: today's classes and the next club events.
|
|
28
29
|
- **Schedule**: the current term, week, and next break. Log in to the campus
|
|
29
30
|
system to see your timetable and export it to any calendar app as `.ics`.
|
|
31
|
+
Press `a` for personal exam arrangements or `c` for grades. Both are also
|
|
32
|
+
available from the public Schedule menu before login. Use `←` / `→` to switch
|
|
33
|
+
between exams and grades, `↑` / `↓` or `PgUp` / `PgDn` to select courses, and
|
|
34
|
+
`Enter` to read full details. Press `/` to search by course name or code, `f`
|
|
35
|
+
to filter upcoming exams or unpublished and void grades, and `s` to change
|
|
36
|
+
terms. Wide terminals show details beside the list.
|
|
30
37
|
- **Events**: a countdown to the next event, plus week and month views, search,
|
|
31
38
|
history, and a one-year activity heatmap.
|
|
32
39
|
- **Docs**: read and search the knowledge base. It works even when GitHub is
|
|
@@ -43,6 +50,9 @@ subject to the local cache's 4 MiB size limit.
|
|
|
43
50
|
```text
|
|
44
51
|
nbtca events [--today|--week|--month] [--search=<q>] [--json]
|
|
45
52
|
nbtca schedule login|terms|export|logout
|
|
53
|
+
nbtca exams [--term=2025-2] [--json] [--one-shot]
|
|
54
|
+
nbtca exams --output=exams.ics [--term=2025-2]
|
|
55
|
+
nbtca grades [--term=2025-2] [--json] [--one-shot]
|
|
46
56
|
nbtca status [--watch]
|
|
47
57
|
nbtca docs
|
|
48
58
|
nbtca website|github|roadmap|repair [--open]
|
|
@@ -57,6 +67,19 @@ Service checks wait up to 15 seconds by default. Use `--timeout=<ms>` to change
|
|
|
57
67
|
the limit. A timeout is shown separately from an offline result and appears as
|
|
58
68
|
`timedOut: true` in JSON. Failed public checks still return a nonzero exit code.
|
|
59
69
|
|
|
70
|
+
Exam and grade queries default to the current term. Use `--term` to select
|
|
71
|
+
another term, or press `s` in the interactive results. Press `r` to refresh
|
|
72
|
+
or `x` to log out.
|
|
73
|
+
Exam times remain unavailable until the school publishes them. Grades retain
|
|
74
|
+
the school's original numeric or textual result; Prompt does not calculate an
|
|
75
|
+
overall GPA. Records stay in memory and are not cached. `--json` writes them to
|
|
76
|
+
standard output, so keep redirected output private.
|
|
77
|
+
|
|
78
|
+
Use `nbtca exams --output=exams.ics` to export a private calendar for a calendar
|
|
79
|
+
app. Export requires a published time for every exam; it fails before writing if
|
|
80
|
+
any time is unavailable. Authentication stays in Prompt. The `nbtcal/academic`
|
|
81
|
+
module owns academic queries, parsing, calendar conversion and ICS serialization.
|
|
82
|
+
|
|
60
83
|
Prompt never stores your password. A saved campus session gets user-only file
|
|
61
84
|
permissions. See [SECURITY.md](SECURITY.md).
|
|
62
85
|
|
|
@@ -67,6 +90,13 @@ npm ci
|
|
|
67
90
|
npm run check
|
|
68
91
|
```
|
|
69
92
|
|
|
93
|
+
For unpublished changes shared with `nbtcal`, build and pack that repository,
|
|
94
|
+
then extract the candidate package into `node_modules/@nbtca/nbtcal` after
|
|
95
|
+
`npm ci`. Set `NBTCA_NBTCAL_TARBALL` to the tarball's absolute path when running
|
|
96
|
+
`npm run check` so the isolated package check uses the same candidate. `npm ci`
|
|
97
|
+
restores the published dependency. Publish `nbtcal` before updating Prompt's
|
|
98
|
+
registry dependency to the new API version.
|
|
99
|
+
|
|
70
100
|
## License
|
|
71
101
|
|
|
72
102
|
MIT
|
package/SECURITY.md
CHANGED
|
@@ -10,9 +10,14 @@ to the user's state directory with `0700` directory and `0600` file permissions
|
|
|
10
10
|
on POSIX systems. Use `nbtca schedule logout` to remove it, or `--one-shot` to
|
|
11
11
|
avoid reading or writing a session.
|
|
12
12
|
|
|
13
|
+
Exam and grade queries use allowlisted, read-only academic endpoints. Prompt
|
|
14
|
+
discards unrelated identity fields and does not cache these records. JSON output
|
|
15
|
+
contains personal academic information; redirected output and terminal scrollback
|
|
16
|
+
may retain it.
|
|
17
|
+
|
|
13
18
|
Exported calendars may reveal a person's routine and location. Keep session and
|
|
14
19
|
ICS files private.
|
|
15
20
|
|
|
16
21
|
Report vulnerabilities through GitHub Security Advisories. Never include
|
|
17
|
-
credentials, cookies, raw campus responses
|
|
18
|
-
issues.
|
|
22
|
+
credentials, cookies, raw campus responses, exam arrangements, grades or personal
|
|
23
|
+
calendars in public issues.
|
|
@@ -30,6 +30,8 @@ export function hubShortcuts(tt) {
|
|
|
30
30
|
{ key: 't', label: trans.timetable.hubTermDensity },
|
|
31
31
|
{ key: 's', label: trans.timetable.hubSwitchTerm },
|
|
32
32
|
{ key: 'e', label: trans.timetable.hubExport },
|
|
33
|
+
{ key: 'a', label: trans.records.exams },
|
|
34
|
+
{ key: 'c', label: trans.records.grades },
|
|
33
35
|
];
|
|
34
36
|
if (tt.unresolvedItems.length > 0) {
|
|
35
37
|
shortcuts.push({
|
|
@@ -305,6 +307,8 @@ export function renderSchedule(state, now, bodyRows = 100, cols = 80) {
|
|
|
305
307
|
return loadingLines(trans.common.loading, cols);
|
|
306
308
|
case 'public':
|
|
307
309
|
return renderPublicBody(state, now, bodyRows, cols);
|
|
310
|
+
case 'records':
|
|
311
|
+
return [];
|
|
308
312
|
case 'needsLoginId':
|
|
309
313
|
return [
|
|
310
314
|
...(state.errorMessage ? [...hintLines(state.errorMessage, cols), ''] : []),
|
|
@@ -18,11 +18,14 @@ import { loadSchoolCalendarOrCache } from '../../features/calendar.js';
|
|
|
18
18
|
import { currentAcademicWindow, inferWeekOneMonday, } from '@nbtca/nbtcal';
|
|
19
19
|
import { sanitizeAcademicTerm, sanitizeTimetable } from '../../features/timetable-sanitize.js';
|
|
20
20
|
import { addLocalDays, parseLocalMonday } from '../../core/calendar-day.js';
|
|
21
|
+
import { StudentRecordsPanel } from './student-records.js';
|
|
21
22
|
let state = { mode: 'loading' };
|
|
22
23
|
let session = null;
|
|
23
24
|
let client = null;
|
|
24
25
|
let catalog = [];
|
|
25
26
|
let pendingId = '';
|
|
27
|
+
let pendingRecordsKind = null;
|
|
28
|
+
let recordsPanel = null;
|
|
26
29
|
let lifecycleGeneration = 0;
|
|
27
30
|
const closingSessions = new WeakMap();
|
|
28
31
|
function gridKeys() {
|
|
@@ -60,6 +63,7 @@ async function releaseSession(target = session) {
|
|
|
60
63
|
if (session === target) {
|
|
61
64
|
session = null;
|
|
62
65
|
client = null;
|
|
66
|
+
catalog = [];
|
|
63
67
|
}
|
|
64
68
|
await closeSession(target);
|
|
65
69
|
}
|
|
@@ -111,7 +115,11 @@ function goToLoginId(errorMessage) {
|
|
|
111
115
|
}
|
|
112
116
|
function buildPublicField() {
|
|
113
117
|
return new ListField({
|
|
114
|
-
options: [
|
|
118
|
+
options: [
|
|
119
|
+
{ value: 'login', label: t().timetable.publicLoginAction },
|
|
120
|
+
{ value: 'exams', label: t().records.exams },
|
|
121
|
+
{ value: 'grades', label: t().records.grades },
|
|
122
|
+
],
|
|
115
123
|
});
|
|
116
124
|
}
|
|
117
125
|
async function goToPublic(ctx, generation = lifecycleGeneration) {
|
|
@@ -169,6 +177,12 @@ async function afterAuthenticated(ctx, s, generation) {
|
|
|
169
177
|
if (!isLifecycleActive(ctx, generation))
|
|
170
178
|
return;
|
|
171
179
|
catalog = nextCatalog;
|
|
180
|
+
if (pendingRecordsKind) {
|
|
181
|
+
const kind = pendingRecordsKind;
|
|
182
|
+
pendingRecordsKind = null;
|
|
183
|
+
await openStudentRecords(ctx, kind, generation);
|
|
184
|
+
return;
|
|
185
|
+
}
|
|
172
186
|
const term = resolveTerm(catalog);
|
|
173
187
|
const key = termKey(term);
|
|
174
188
|
let weekOne = loadWeekOne(key);
|
|
@@ -279,11 +293,85 @@ async function refreshFromNetwork(ctx, generation) {
|
|
|
279
293
|
}
|
|
280
294
|
}
|
|
281
295
|
}
|
|
296
|
+
async function openStudentRecords(ctx, kind, generation = ++lifecycleGeneration) {
|
|
297
|
+
if (!isLifecycleActive(ctx, generation))
|
|
298
|
+
return;
|
|
299
|
+
recordsPanel?.dispose();
|
|
300
|
+
recordsPanel = null;
|
|
301
|
+
pendingRecordsKind = kind;
|
|
302
|
+
try {
|
|
303
|
+
if (!session) {
|
|
304
|
+
const saved = createSessionStore().load();
|
|
305
|
+
if (!saved) {
|
|
306
|
+
goToLoginId();
|
|
307
|
+
ctx.rerender();
|
|
308
|
+
return;
|
|
309
|
+
}
|
|
310
|
+
state = { ...state, mode: 'loading' };
|
|
311
|
+
ctx.rerender();
|
|
312
|
+
const restored = await restoreNbtSession(saved);
|
|
313
|
+
await afterAuthenticated(ctx, restored, generation);
|
|
314
|
+
return;
|
|
315
|
+
}
|
|
316
|
+
if (catalog.length === 0) {
|
|
317
|
+
state = { ...state, mode: 'loading' };
|
|
318
|
+
ctx.rerender();
|
|
319
|
+
const nextCatalog = (await createNbtTimetableClient(session.timetableTransport, {
|
|
320
|
+
baseUrl: JWXT_ORIGIN,
|
|
321
|
+
}).listTerms(ctx.signal === undefined ? {} : { signal: ctx.signal })).map(sanitizeAcademicTerm);
|
|
322
|
+
if (!isLifecycleActive(ctx, generation))
|
|
323
|
+
return;
|
|
324
|
+
catalog = nextCatalog;
|
|
325
|
+
}
|
|
326
|
+
const previousTerm = state.term ?? state.timetable?.term;
|
|
327
|
+
const selected = catalog.find((term) => term.academicYear === previousTerm?.academicYear &&
|
|
328
|
+
term.semester === previousTerm.semester) ?? resolveTerm(catalog);
|
|
329
|
+
pendingRecordsKind = null;
|
|
330
|
+
const panel = new StudentRecordsPanel(kind, selected, relevantTerms(catalog), session.timetableTransport, () => isLifecycleActive(ctx, generation) && recordsPanel === panel, () => {
|
|
331
|
+
pendingRecordsKind = panel.kind;
|
|
332
|
+
createSessionStore().clear();
|
|
333
|
+
recordsPanel?.dispose();
|
|
334
|
+
recordsPanel = null;
|
|
335
|
+
void releaseSession();
|
|
336
|
+
goToLoginId(t().timetable.expiredRelogin);
|
|
337
|
+
ctx.rerender();
|
|
338
|
+
});
|
|
339
|
+
recordsPanel = panel;
|
|
340
|
+
state = { ...state, mode: 'records' };
|
|
341
|
+
await panel.load(ctx);
|
|
342
|
+
}
|
|
343
|
+
catch (error) {
|
|
344
|
+
if (!isLifecycleActive(ctx, generation))
|
|
345
|
+
return;
|
|
346
|
+
if (isSessionExpired(error)) {
|
|
347
|
+
createSessionStore().clear();
|
|
348
|
+
await releaseSession();
|
|
349
|
+
goToLoginId(t().timetable.expiredRelogin);
|
|
350
|
+
}
|
|
351
|
+
else {
|
|
352
|
+
state = { ...state, mode: 'error', errorMessage: safeMessage(error) };
|
|
353
|
+
}
|
|
354
|
+
ctx.rerender();
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
function logout(ctx) {
|
|
358
|
+
const generation = ++lifecycleGeneration;
|
|
359
|
+
pendingRecordsKind = null;
|
|
360
|
+
recordsPanel?.dispose();
|
|
361
|
+
recordsPanel = null;
|
|
362
|
+
createSessionStore().clear();
|
|
363
|
+
clearScheduleCache();
|
|
364
|
+
void releaseSession();
|
|
365
|
+
void goToPublic(ctx, generation);
|
|
366
|
+
}
|
|
282
367
|
export const scheduleView = {
|
|
283
368
|
id: 'schedule',
|
|
284
369
|
title: t().timetable.menuEntry,
|
|
285
370
|
async load(ctx) {
|
|
286
371
|
const generation = ++lifecycleGeneration;
|
|
372
|
+
pendingRecordsKind = null;
|
|
373
|
+
recordsPanel?.dispose();
|
|
374
|
+
recordsPanel = null;
|
|
287
375
|
if (!isLifecycleActive(ctx, generation))
|
|
288
376
|
return;
|
|
289
377
|
const ptr = loadCurrentPointer();
|
|
@@ -306,17 +394,32 @@ export const scheduleView = {
|
|
|
306
394
|
},
|
|
307
395
|
async dispose() {
|
|
308
396
|
lifecycleGeneration += 1;
|
|
397
|
+
pendingRecordsKind = null;
|
|
398
|
+
recordsPanel?.dispose();
|
|
399
|
+
recordsPanel = null;
|
|
309
400
|
setVimKeysActive(true);
|
|
310
401
|
await releaseSession();
|
|
311
402
|
},
|
|
312
403
|
render(ctx) {
|
|
404
|
+
if (state.mode === 'records' && recordsPanel)
|
|
405
|
+
return recordsPanel.render(ctx);
|
|
313
406
|
state.termField?.setMaxVisible(computeMaxVisible(ctx.bodyRows));
|
|
314
407
|
return renderSchedule(state, new Date(), ctx.bodyRows, ctx.size.cols);
|
|
315
408
|
},
|
|
316
409
|
isBusy() {
|
|
410
|
+
if (state.mode === 'records')
|
|
411
|
+
return recordsPanel?.isBusy() ?? false;
|
|
317
412
|
return state.mode === 'loading';
|
|
318
413
|
},
|
|
414
|
+
scrollsBody() {
|
|
415
|
+
return state.mode === 'records' && !recordsPanel?.capturesPageKeys();
|
|
416
|
+
},
|
|
417
|
+
contextPath() {
|
|
418
|
+
return state.mode === 'records' ? recordsPanel?.contextPath() : undefined;
|
|
419
|
+
},
|
|
319
420
|
shortcuts() {
|
|
421
|
+
if (state.mode === 'records')
|
|
422
|
+
return recordsPanel?.shortcuts() ?? [];
|
|
320
423
|
if ((state.mode !== 'hub' && state.mode !== 'week') || !state.timetable)
|
|
321
424
|
return [];
|
|
322
425
|
const trans = t();
|
|
@@ -331,14 +434,20 @@ export const scheduleView = {
|
|
|
331
434
|
return [...grid, ...hubShortcuts(state.timetable).map(({ key, label }) => ({ key, label }))];
|
|
332
435
|
},
|
|
333
436
|
capturesInput() {
|
|
437
|
+
if (state.mode === 'records')
|
|
438
|
+
return recordsPanel?.capturesInput() ?? false;
|
|
334
439
|
return (state.mode === 'needsLoginId' ||
|
|
335
440
|
state.mode === 'needsLoginPassword' ||
|
|
336
441
|
state.mode === 'needsWeekOne');
|
|
337
442
|
},
|
|
338
443
|
capturesPageKeys() {
|
|
444
|
+
if (state.mode === 'records')
|
|
445
|
+
return recordsPanel?.capturesPageKeys() ?? false;
|
|
339
446
|
return state.mode === 'public' || state.mode === 'termPicker';
|
|
340
447
|
},
|
|
341
448
|
footerHint(tabCount, cols = Number.POSITIVE_INFINITY) {
|
|
449
|
+
if (state.mode === 'records')
|
|
450
|
+
return recordsPanel?.footerHint(tabCount, cols) ?? passiveFooterHint(tabCount, cols);
|
|
342
451
|
const capturing = state.mode === 'needsLoginId' ||
|
|
343
452
|
state.mode === 'needsLoginPassword' ||
|
|
344
453
|
state.mode === 'needsWeekOne';
|
|
@@ -357,11 +466,25 @@ export const scheduleView = {
|
|
|
357
466
|
: undefined;
|
|
358
467
|
},
|
|
359
468
|
handleBack(ctx) {
|
|
469
|
+
if (state.mode === 'records') {
|
|
470
|
+
if (recordsPanel?.handleBack())
|
|
471
|
+
return true;
|
|
472
|
+
recordsPanel?.dispose();
|
|
473
|
+
recordsPanel = null;
|
|
474
|
+
if (!returnToHub())
|
|
475
|
+
void goToPublic(ctx);
|
|
476
|
+
return true;
|
|
477
|
+
}
|
|
360
478
|
if (state.mode === 'needsLoginId') {
|
|
479
|
+
pendingRecordsKind = null;
|
|
361
480
|
void goToPublic(ctx);
|
|
362
481
|
return true;
|
|
363
482
|
}
|
|
364
|
-
if (state.mode === '
|
|
483
|
+
if (state.mode === 'needsWeekOne') {
|
|
484
|
+
void goToPublic(ctx);
|
|
485
|
+
return true;
|
|
486
|
+
}
|
|
487
|
+
if (state.mode === 'needsLoginPassword') {
|
|
365
488
|
goToLoginId();
|
|
366
489
|
return true;
|
|
367
490
|
}
|
|
@@ -384,10 +507,24 @@ export const scheduleView = {
|
|
|
384
507
|
switch (state.mode) {
|
|
385
508
|
case 'public': {
|
|
386
509
|
const result = state.publicField?.handleKey(key);
|
|
387
|
-
if (result?.selected === '
|
|
388
|
-
|
|
510
|
+
if (result?.selected === 'exams' || result?.selected === 'grades') {
|
|
511
|
+
void openStudentRecords(ctx, result.selected);
|
|
512
|
+
}
|
|
513
|
+
else if (result?.selected === 'login') {
|
|
514
|
+
pendingRecordsKind = null;
|
|
515
|
+
if (session)
|
|
516
|
+
void afterAuthenticated(ctx, session, lifecycleGeneration);
|
|
517
|
+
else
|
|
518
|
+
goToLoginId();
|
|
519
|
+
}
|
|
389
520
|
return;
|
|
390
521
|
}
|
|
522
|
+
case 'records':
|
|
523
|
+
if (key === 'x' && recordsPanel?.acceptsActions())
|
|
524
|
+
logout(ctx);
|
|
525
|
+
else
|
|
526
|
+
recordsPanel?.handleKey(key, ctx);
|
|
527
|
+
return;
|
|
391
528
|
case 'needsLoginId': {
|
|
392
529
|
const result = state.idField?.handleKey(key);
|
|
393
530
|
if (result?.cancelled) {
|
|
@@ -451,7 +588,7 @@ export const scheduleView = {
|
|
|
451
588
|
case 'needsWeekOne': {
|
|
452
589
|
const result = state.weekOneField?.handleKey(key);
|
|
453
590
|
if (result?.cancelled) {
|
|
454
|
-
|
|
591
|
+
void goToPublic(ctx);
|
|
455
592
|
return;
|
|
456
593
|
}
|
|
457
594
|
if (result?.submitted !== undefined) {
|
|
@@ -504,6 +641,10 @@ export const scheduleView = {
|
|
|
504
641
|
const shortcut = hubShortcuts(tt).find((sc) => sc.key === key);
|
|
505
642
|
if (!shortcut)
|
|
506
643
|
return;
|
|
644
|
+
if (shortcut.key === 'a' || shortcut.key === 'c') {
|
|
645
|
+
void openStudentRecords(ctx, shortcut.key === 'a' ? 'exams' : 'grades');
|
|
646
|
+
return;
|
|
647
|
+
}
|
|
507
648
|
if (shortcut.key === 'w') {
|
|
508
649
|
state = { ...state, mode: 'week' };
|
|
509
650
|
return;
|
|
@@ -560,13 +701,8 @@ export const scheduleView = {
|
|
|
560
701
|
}
|
|
561
702
|
return;
|
|
562
703
|
}
|
|
563
|
-
if (shortcut.key === 'x')
|
|
564
|
-
|
|
565
|
-
createSessionStore().clear();
|
|
566
|
-
clearScheduleCache();
|
|
567
|
-
void releaseSession();
|
|
568
|
-
void goToPublic(ctx, generation);
|
|
569
|
-
}
|
|
704
|
+
if (shortcut.key === 'x')
|
|
705
|
+
logout(ctx);
|
|
570
706
|
return;
|
|
571
707
|
}
|
|
572
708
|
case 'week': {
|
|
@@ -0,0 +1,268 @@
|
|
|
1
|
+
import { campusIsoDate } from '@nbtca/nbtcal/timetable';
|
|
2
|
+
import { c, type, glyph, space, bodyEdge } from '../../core/theme.js';
|
|
3
|
+
import { clipAnsiToVisualWidth, padEndV, sanitizeTerminalLine, truncate, visualWidth, wrapAnsiToVisualWidth, } from '../../core/text.js';
|
|
4
|
+
import { spinnerFrame } from '../../core/components/spinner.js';
|
|
5
|
+
import { isoDayDifference } from '../../core/calendar-day.js';
|
|
6
|
+
import { displaySemesterLabel } from '../../features/student-timetable.js';
|
|
7
|
+
import { fmt, getCurrentLanguage, t } from '../../i18n/index.js';
|
|
8
|
+
const formatters = new Map();
|
|
9
|
+
function dateText(date, format) {
|
|
10
|
+
const language = getCurrentLanguage();
|
|
11
|
+
const key = `${language}:${format}`;
|
|
12
|
+
let formatter = formatters.get(key);
|
|
13
|
+
if (!formatter) {
|
|
14
|
+
formatter = new Intl.DateTimeFormat(language === 'zh' ? 'zh-CN' : 'en-GB', {
|
|
15
|
+
timeZone: 'Asia/Shanghai',
|
|
16
|
+
...(format !== 'clock'
|
|
17
|
+
? {
|
|
18
|
+
month: '2-digit',
|
|
19
|
+
day: '2-digit',
|
|
20
|
+
...(format === 'day' ? { weekday: 'short' } : {}),
|
|
21
|
+
}
|
|
22
|
+
: { hour: '2-digit', minute: '2-digit', hourCycle: 'h23' }),
|
|
23
|
+
});
|
|
24
|
+
formatters.set(key, formatter);
|
|
25
|
+
}
|
|
26
|
+
if (format === 'clock')
|
|
27
|
+
return formatter.format(date);
|
|
28
|
+
const parts = formatter.formatToParts(date);
|
|
29
|
+
const part = (name) => parts.find((entry) => entry.type === name)?.value ?? '';
|
|
30
|
+
if (format === 'shortDay')
|
|
31
|
+
return `${part('month')}/${part('day')}`;
|
|
32
|
+
return language === 'zh'
|
|
33
|
+
? `${part('month')}/${part('day')} ${part('weekday')}`
|
|
34
|
+
: `${part('weekday')} ${part('day')}/${part('month')}`;
|
|
35
|
+
}
|
|
36
|
+
function examStatus(exam, now) {
|
|
37
|
+
const trans = t().records;
|
|
38
|
+
if (!exam.start || !exam.end)
|
|
39
|
+
return trans.pendingTag;
|
|
40
|
+
if (exam.end <= now)
|
|
41
|
+
return trans.ended;
|
|
42
|
+
if (exam.start <= now)
|
|
43
|
+
return trans.inProgress;
|
|
44
|
+
const days = isoDayDifference(campusIsoDate(now), campusIsoDate(exam.start));
|
|
45
|
+
return days === 0 ? trans.today : days === 1 ? trans.tomorrow : fmt(trans.daysAway, { days });
|
|
46
|
+
}
|
|
47
|
+
function examTime(exam, compact = false) {
|
|
48
|
+
return exam.start && exam.end
|
|
49
|
+
? `${dateText(exam.start, compact ? 'shortDay' : 'day')} ${dateText(exam.start, 'clock')}–${dateText(exam.end, 'clock')}`
|
|
50
|
+
: (exam.timeText ?? t().records.timeUnavailable);
|
|
51
|
+
}
|
|
52
|
+
export function visibleStudentRecords(data, query, attention, now) {
|
|
53
|
+
if (!data)
|
|
54
|
+
return [];
|
|
55
|
+
const needle = query.trim().normalize('NFKC').toLowerCase();
|
|
56
|
+
return data.items.flatMap((item, index) => {
|
|
57
|
+
if (needle &&
|
|
58
|
+
!`${item.courseName} ${item.courseCode ?? ''}`
|
|
59
|
+
.normalize('NFKC')
|
|
60
|
+
.toLowerCase()
|
|
61
|
+
.includes(needle))
|
|
62
|
+
return [];
|
|
63
|
+
if (attention &&
|
|
64
|
+
('end' in item ? item.end !== null && item.end <= now : item.grade !== null && !item.voided))
|
|
65
|
+
return [];
|
|
66
|
+
return [index];
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
function pair(left, right, width) {
|
|
70
|
+
const room = width - visualWidth(right) - 2;
|
|
71
|
+
if (room < 4)
|
|
72
|
+
return truncate(left, width, '…');
|
|
73
|
+
return padEndV(truncate(left, room, '…'), width - visualWidth(right)) + right;
|
|
74
|
+
}
|
|
75
|
+
function resultText(grade) {
|
|
76
|
+
const value = grade.grade ?? t().records.notPublished;
|
|
77
|
+
return grade.voided ? `${value} ${t().records.voidTag}` : value;
|
|
78
|
+
}
|
|
79
|
+
function gradeRow(grade, width, selected, table) {
|
|
80
|
+
const marker = selected ? `${glyph.cursor()} ` : ' ';
|
|
81
|
+
if (!table) {
|
|
82
|
+
return [
|
|
83
|
+
pair(marker + grade.courseName, resultText(grade), width),
|
|
84
|
+
` ${[grade.credits === null ? '' : fmt(t().records.credits, { value: grade.credits }), grade.gradePoint === null ? '' : fmt(t().records.gradePoint, { value: grade.gradePoint })].filter(Boolean).join(' · ')}`,
|
|
85
|
+
];
|
|
86
|
+
}
|
|
87
|
+
const courseWidth = width - 31;
|
|
88
|
+
const value = padEndV(truncate(resultText(grade), 10, '…'), 10);
|
|
89
|
+
const credits = String(grade.credits ?? '—');
|
|
90
|
+
const point = String(grade.gradePoint ?? '—');
|
|
91
|
+
const plain = `${marker}${padEndV(truncate(grade.courseName, courseWidth, '…'), courseWidth)} ${value} ${padEndV(credits, 7)} ${padEndV(point, 6)}`;
|
|
92
|
+
if (selected)
|
|
93
|
+
return [type.cursor(padEndV(plain, width))];
|
|
94
|
+
return [
|
|
95
|
+
`${marker}${padEndV(truncate(grade.courseName, courseWidth, '…'), courseWidth)} ${grade.voided || grade.grade === null ? c.warn(value) : type.heading(value)} ${type.hint(padEndV(credits, 7))} ${type.hint(padEndV(point, 6))}`,
|
|
96
|
+
];
|
|
97
|
+
}
|
|
98
|
+
function examRow(exam, width, selected, now) {
|
|
99
|
+
const main = pair(`${selected ? glyph.cursor() : ' '} ${exam.courseName}`, examStatus(exam, now), width);
|
|
100
|
+
const location = [
|
|
101
|
+
exam.location,
|
|
102
|
+
width >= 54 && exam.seat ? fmt(t().records.seat, { value: exam.seat }) : null,
|
|
103
|
+
]
|
|
104
|
+
.filter(Boolean)
|
|
105
|
+
.join(' · ');
|
|
106
|
+
const meta = ` ${examTime(exam, width < 54)}${location ? ` · ${location}` : ''}`;
|
|
107
|
+
return [
|
|
108
|
+
selected
|
|
109
|
+
? type.cursor(padEndV(main, width))
|
|
110
|
+
: exam.end && exam.end <= now
|
|
111
|
+
? type.hint(main)
|
|
112
|
+
: type.heading(main),
|
|
113
|
+
type.hint(truncate(meta, width, '…')),
|
|
114
|
+
];
|
|
115
|
+
}
|
|
116
|
+
export function renderStudentRecordDetail(data, index, cols, now) {
|
|
117
|
+
const item = data.items[index];
|
|
118
|
+
if (!item)
|
|
119
|
+
return [];
|
|
120
|
+
const trans = t().records;
|
|
121
|
+
const lines = [];
|
|
122
|
+
const add = (value, style = type.body) => {
|
|
123
|
+
lines.push(...wrapAnsiToVisualWidth(style(sanitizeTerminalLine(value)), cols));
|
|
124
|
+
};
|
|
125
|
+
add(item.courseName, type.heading);
|
|
126
|
+
if (item.courseCode)
|
|
127
|
+
add(item.courseCode, type.hint);
|
|
128
|
+
lines.push('');
|
|
129
|
+
if (data.kind === 'grades') {
|
|
130
|
+
const grade = item;
|
|
131
|
+
add(fmt(trans.gradeValue, { value: grade.grade ?? trans.notPublished }), grade.voided ? c.warn : type.active);
|
|
132
|
+
if (grade.voided)
|
|
133
|
+
add(trans.voided, c.warn);
|
|
134
|
+
lines.push('');
|
|
135
|
+
if (grade.credits !== null)
|
|
136
|
+
add(fmt(trans.credits, { value: grade.credits }));
|
|
137
|
+
if (grade.gradePoint !== null)
|
|
138
|
+
add(fmt(trans.gradePoint, { value: grade.gradePoint }));
|
|
139
|
+
if (grade.courseNature)
|
|
140
|
+
add(`${trans.courseNature} ${grade.courseNature}`);
|
|
141
|
+
if (grade.examNature)
|
|
142
|
+
add(`${trans.examNature} ${grade.examNature}`);
|
|
143
|
+
if (grade.remark) {
|
|
144
|
+
lines.push('');
|
|
145
|
+
add(grade.remark, type.hint);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
else {
|
|
149
|
+
const exam = item;
|
|
150
|
+
add(examStatus(exam, now), exam.start ? type.active : c.warn);
|
|
151
|
+
add(examTime(exam));
|
|
152
|
+
lines.push('');
|
|
153
|
+
if (exam.location)
|
|
154
|
+
add(`${trans.location} ${exam.location}`);
|
|
155
|
+
if (exam.campus)
|
|
156
|
+
add(`${trans.campus} ${exam.campus}`);
|
|
157
|
+
if (exam.seat)
|
|
158
|
+
add(fmt(trans.seat, { value: exam.seat }), type.heading);
|
|
159
|
+
if (exam.examName)
|
|
160
|
+
add(`${trans.examName} ${exam.examName}`);
|
|
161
|
+
if (exam.assessment)
|
|
162
|
+
add(`${trans.assessment} ${exam.assessment}`);
|
|
163
|
+
}
|
|
164
|
+
return lines;
|
|
165
|
+
}
|
|
166
|
+
function summary(data, attention) {
|
|
167
|
+
const trans = t().records;
|
|
168
|
+
if (data.kind === 'grades') {
|
|
169
|
+
const published = data.items.filter((grade) => grade.grade !== null).length;
|
|
170
|
+
return `${fmt(trans.gradeSummary, { published, total: data.items.length })}${attention ? ` · ${trans.needsAttention}` : ''}`;
|
|
171
|
+
}
|
|
172
|
+
const pending = data.items.filter((exam) => !exam.start).length;
|
|
173
|
+
return `${fmt(trans.examSummary, { total: data.items.length, pending })}${attention ? ` · ${trans.upcomingOnly}` : ''}`;
|
|
174
|
+
}
|
|
175
|
+
export function renderStudentRecordsPanel(state, bodyRows, cols, now) {
|
|
176
|
+
const rows = Math.max(0, Math.floor(bodyRows));
|
|
177
|
+
const width = Math.max(1, bodyEdge(cols) - (cols > 6 ? space.indent.length : 0));
|
|
178
|
+
const indent = cols > 6 ? space.indent : '';
|
|
179
|
+
const trans = t().records;
|
|
180
|
+
const lines = [];
|
|
181
|
+
const tabs = ['exams', 'grades']
|
|
182
|
+
.map((kind) => (kind === state.kind ? type.active(`[${trans[kind]}]`) : type.hint(trans[kind])))
|
|
183
|
+
.join(' ');
|
|
184
|
+
lines.push(visualWidth(tabs) <= width ? tabs : type.heading(truncate(trans[state.kind], width, '…')));
|
|
185
|
+
if (rows >= 6) {
|
|
186
|
+
const term = `${state.term.academicYearLabel} · ${displaySemesterLabel(state.term)}`;
|
|
187
|
+
const updated = state.attention && rows < 10
|
|
188
|
+
? trans.filtered
|
|
189
|
+
: state.data
|
|
190
|
+
? `${trans.updated} ${dateText(state.data.fetchedAt, 'clock')}`
|
|
191
|
+
: '';
|
|
192
|
+
lines.push(type.hint(pair(term, updated, width)));
|
|
193
|
+
}
|
|
194
|
+
if (rows >= 10 && state.data)
|
|
195
|
+
lines.push(type.hint(truncate(summary(state.data, state.attention), width, '…')));
|
|
196
|
+
if (state.query || state.searching)
|
|
197
|
+
lines.push(type.active(truncate(`/ ${state.query}${state.searching ? '▏' : ''}${state.searching && !state.query ? ` ${trans.searchHint}` : ''}`, width, '…')));
|
|
198
|
+
if (state.data?.kind === 'exams' && rows >= 15 && !state.query) {
|
|
199
|
+
const next = state.data.items.find((exam) => exam.start && exam.end && exam.end > now);
|
|
200
|
+
if (next) {
|
|
201
|
+
lines.push('');
|
|
202
|
+
lines.push(type.active(truncate(`${trans.nextExam} · ${examStatus(next, now)} · ${next.courseName}`, width, '…')));
|
|
203
|
+
lines.push(type.hint(truncate(`${examTime(next)}${next.location ? ` · ${next.location}` : ''}`, width, '…')));
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
if (rows >= 10)
|
|
207
|
+
lines.push('');
|
|
208
|
+
const indices = visibleStudentRecords(state.data, state.query, state.attention, now);
|
|
209
|
+
const selected = Math.min(Math.max(0, state.selected), Math.max(0, indices.length - 1));
|
|
210
|
+
const split = width >= 94 && rows >= 12 && indices.length > 0;
|
|
211
|
+
const listWidth = split ? Math.floor(width * 0.6) : width;
|
|
212
|
+
const table = state.kind === 'grades' && listWidth >= 54 && rows >= 9 && indices.length > 0;
|
|
213
|
+
if (table) {
|
|
214
|
+
lines.push(type.hint(` ${padEndV(trans.course, listWidth - 31)} ${padEndV(trans.result, 10)} ${padEndV(trans.creditLabel, 7)} ${trans.pointLabel}`));
|
|
215
|
+
lines.push(type.hint(glyph.rule().repeat(width)));
|
|
216
|
+
}
|
|
217
|
+
const available = Math.max(0, rows - lines.length - 1);
|
|
218
|
+
const rowHeight = table || rows < 7 ? 1 : 2;
|
|
219
|
+
const pageSize = Math.max(1, Math.floor(available / rowHeight));
|
|
220
|
+
const start = Math.floor(selected / pageSize) * pageSize;
|
|
221
|
+
const list = [];
|
|
222
|
+
if (state.pending)
|
|
223
|
+
list.push(type.hint(`${spinnerFrame()} ${trans.loading}`));
|
|
224
|
+
else if (state.error)
|
|
225
|
+
list.push(...wrapAnsiToVisualWidth(c.warn(state.error), listWidth));
|
|
226
|
+
else if (indices.length === 0) {
|
|
227
|
+
const filtered = Boolean(state.query || state.attention);
|
|
228
|
+
list.push(...wrapAnsiToVisualWidth(filtered ? trans.noMatches : state.kind === 'grades' ? trans.noGrades : trans.noExams, listWidth));
|
|
229
|
+
list.push('');
|
|
230
|
+
list.push(...wrapAnsiToVisualWidth(type.hint(filtered ? trans.clearFilters : trans.emptyHint), listWidth));
|
|
231
|
+
}
|
|
232
|
+
else if (state.data) {
|
|
233
|
+
for (let offset = start; offset < Math.min(indices.length, start + pageSize); offset += 1) {
|
|
234
|
+
const index = indices[offset];
|
|
235
|
+
const item = index === undefined ? undefined : state.data.items[index];
|
|
236
|
+
if (!item)
|
|
237
|
+
continue;
|
|
238
|
+
const record = state.kind === 'grades'
|
|
239
|
+
? gradeRow(item, listWidth, offset === selected, table)
|
|
240
|
+
: examRow(item, listWidth, offset === selected, now);
|
|
241
|
+
list.push(...record
|
|
242
|
+
.slice(0, rowHeight)
|
|
243
|
+
.map((line) => offset === selected && !table && state.kind === 'grades' ? type.active(line) : line));
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
const detail = split && state.data && indices[selected] !== undefined
|
|
247
|
+
? renderStudentRecordDetail(state.data, indices[selected], width - listWidth - 3, now)
|
|
248
|
+
: [];
|
|
249
|
+
for (let row = 0; row < available; row += 1) {
|
|
250
|
+
const left = clipAnsiToVisualWidth(list[row] ?? '', listWidth);
|
|
251
|
+
lines.push(split ? `${padEndV(left, listWidth)} ${type.hint(glyph.bar())} ${detail[row] ?? ''}` : left);
|
|
252
|
+
}
|
|
253
|
+
const count = indices.length ? `${selected + 1}/${indices.length}` : '';
|
|
254
|
+
const actions = (state.searching
|
|
255
|
+
? [trans.searchFooter, 'Enter · Esc']
|
|
256
|
+
: state.pending
|
|
257
|
+
? ['←→ · x']
|
|
258
|
+
: [
|
|
259
|
+
`s ${trans.termAction} · / ${trans.searchAction} · f ${state.attention ? trans.allRecords : trans.filterAction} · r ${trans.refresh} · x ${t().timetable.hubLogout}`,
|
|
260
|
+
's · / · f · r · x',
|
|
261
|
+
's / f r x',
|
|
262
|
+
]).find((value) => visualWidth(value) + visualWidth(count) + 2 <= width) ?? '';
|
|
263
|
+
lines.push(type.hint(pair(actions, count, width)));
|
|
264
|
+
return {
|
|
265
|
+
lines: lines.slice(0, rows).map((line) => indent + clipAnsiToVisualWidth(line, width)),
|
|
266
|
+
pageSize,
|
|
267
|
+
};
|
|
268
|
+
}
|