@nbtca/prompt 1.5.18 → 1.5.19
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 -5
- package/dist/app/views/schedule.js +3 -3
- package/dist/features/calendar-store.js +11 -13
- package/dist/features/calendar.js +61 -43
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -22,20 +22,47 @@ Run a command without installing:
|
|
|
22
22
|
npx @nbtca/prompt --help
|
|
23
23
|
```
|
|
24
24
|
|
|
25
|
+
## Interactive mode
|
|
26
|
+
|
|
27
|
+
Run `nbtca` with no arguments to open the full-screen client. Press `1`–`5` or
|
|
28
|
+
`Tab` to switch tabs, and `?` to list every key.
|
|
29
|
+
|
|
30
|
+
- **Home**: today's classes and the next club events.
|
|
31
|
+
- **Schedule**: the current term, week, and next break from the school
|
|
32
|
+
calendar. Log in to the campus system to see your timetable and export it as
|
|
33
|
+
an `.ics` file.
|
|
34
|
+
- **Events**: the next event with a countdown, plus week, month, search,
|
|
35
|
+
history, and a one-year activity heatmap.
|
|
36
|
+
- **Docs**: the NBTCA knowledge base, readable and searchable in the terminal.
|
|
37
|
+
It is cached locally and served from a mirror when GitHub is unreachable.
|
|
38
|
+
- **Settings**: language, icon and color modes, and version info.
|
|
39
|
+
|
|
40
|
+
Events, docs, and the school calendar stay readable offline from the last
|
|
41
|
+
successful fetch.
|
|
42
|
+
|
|
25
43
|
## Commands
|
|
26
44
|
|
|
27
45
|
```text
|
|
28
|
-
nbtca events [--json]
|
|
46
|
+
nbtca events [--today|--week|--month] [--search=<q>] [--next=<n>] [--json]
|
|
47
|
+
nbtca events --heatmap
|
|
29
48
|
nbtca docs
|
|
30
|
-
nbtca status
|
|
49
|
+
nbtca status [--watch] [--json]
|
|
31
50
|
nbtca schedule login
|
|
51
|
+
nbtca schedule status
|
|
32
52
|
nbtca schedule terms
|
|
33
|
-
nbtca schedule export --term=2026:3 --week-one=YYYY-MM-DD
|
|
53
|
+
nbtca schedule export --term=2026:3 --week-one=YYYY-MM-DD [--output=<path>]
|
|
34
54
|
nbtca schedule logout
|
|
55
|
+
nbtca website|github|roadmap|repair [--open]
|
|
56
|
+
nbtca theme icon auto|ascii|unicode
|
|
57
|
+
nbtca theme color auto|on|off
|
|
58
|
+
nbtca theme reset
|
|
59
|
+
nbtca lang zh|en
|
|
60
|
+
nbtca update
|
|
35
61
|
```
|
|
36
62
|
|
|
37
|
-
Use `--plain` for stable output without color
|
|
38
|
-
|
|
63
|
+
`nbtca --help` lists every flag. Use `--plain` for stable output without color
|
|
64
|
+
and `--no-logo` to skip the startup logo. `schedule export --one-shot` avoids
|
|
65
|
+
reading or saving a campus session.
|
|
39
66
|
|
|
40
67
|
Prompt never stores passwords. Persisted sessions contain only a masked account
|
|
41
68
|
hint and a CookieJar, protected with user-only permissions on POSIX systems.
|
|
@@ -14,7 +14,7 @@ import { loginWithStudentPassword, restoreNbtSession, } from '../../auth/nbt-aut
|
|
|
14
14
|
import { createSessionStore } from '../../auth/session-store.js';
|
|
15
15
|
import { resolveTerm, relevantTerms, writePrivateIcs, isSessionExpired, JWXT_ORIGIN, safeMessage, } from '../../features/student-timetable.js';
|
|
16
16
|
import { termKey, loadWeekOne, saveWeekOne, saveTimetableCache, saveCurrentPointer, loadCurrentPointer, loadTimetableCache, clearScheduleCache, } from '../../features/schedule-store.js';
|
|
17
|
-
import {
|
|
17
|
+
import { loadSchoolCalendar } 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';
|
|
@@ -121,7 +121,7 @@ async function goToPublic(ctx, generation = lifecycleGeneration) {
|
|
|
121
121
|
state = { mode: 'public', publicField: buildPublicField() };
|
|
122
122
|
ctx.rerender();
|
|
123
123
|
try {
|
|
124
|
-
const cal = await
|
|
124
|
+
const cal = await loadSchoolCalendar(ctx.signal);
|
|
125
125
|
if (!isLifecycleActive(ctx, generation))
|
|
126
126
|
return;
|
|
127
127
|
const now = new Date();
|
|
@@ -139,7 +139,7 @@ async function goToPublic(ctx, generation = lifecycleGeneration) {
|
|
|
139
139
|
}
|
|
140
140
|
async function tryInferWeekOne(ctx, generation) {
|
|
141
141
|
try {
|
|
142
|
-
const cal = await
|
|
142
|
+
const cal = await loadSchoolCalendar(ctx.signal);
|
|
143
143
|
if (!isLifecycleActive(ctx, generation))
|
|
144
144
|
return null;
|
|
145
145
|
const now = new Date();
|
|
@@ -1,16 +1,14 @@
|
|
|
1
1
|
import fs from 'fs';
|
|
2
2
|
import path from 'path';
|
|
3
3
|
import { getStateDir, getWritableStateDir } from '../config/paths.js';
|
|
4
|
-
const FEED_FILE = 'calendar-feed.ics';
|
|
5
|
-
const VALIDATORS_FILE = 'calendar-feed.json';
|
|
6
4
|
const MAX_AGE_MS = 7 * 24 * 60 * 60 * 1000;
|
|
7
5
|
const HEADER_VALUE = /^[\x21-\x7e][\x20-\x7e]*$/;
|
|
8
6
|
function headerValue(value) {
|
|
9
7
|
return typeof value === 'string' && HEADER_VALUE.test(value) ? value : undefined;
|
|
10
8
|
}
|
|
11
|
-
function readValidators(dir) {
|
|
9
|
+
function readValidators(name, dir) {
|
|
12
10
|
try {
|
|
13
|
-
const data = JSON.parse(fs.readFileSync(path.join(dir,
|
|
11
|
+
const data = JSON.parse(fs.readFileSync(path.join(dir, `${name}.json`), 'utf8'));
|
|
14
12
|
const etag = headerValue(data['etag']);
|
|
15
13
|
const lastModified = headerValue(data['lastModified']);
|
|
16
14
|
return { ...(etag ? { etag } : {}), ...(lastModified ? { lastModified } : {}) };
|
|
@@ -20,22 +18,22 @@ function readValidators(dir) {
|
|
|
20
18
|
}
|
|
21
19
|
}
|
|
22
20
|
// Validators are written after the text and read before it, so they can never be newer than the text.
|
|
23
|
-
export function saveFeedCache({ text, validators }, dir) {
|
|
21
|
+
export function saveFeedCache(name, { text, validators }, dir) {
|
|
24
22
|
try {
|
|
25
23
|
const target = dir ?? getWritableStateDir();
|
|
26
|
-
fs.writeFileSync(path.join(target,
|
|
27
|
-
touchFeedCache(validators, target);
|
|
24
|
+
fs.writeFileSync(path.join(target, `${name}.ics`), text, { encoding: 'utf8', mode: 0o600 });
|
|
25
|
+
touchFeedCache(name, validators, target);
|
|
28
26
|
}
|
|
29
27
|
catch {
|
|
30
28
|
/* best effort */
|
|
31
29
|
}
|
|
32
30
|
}
|
|
33
|
-
export function touchFeedCache(validators, dir) {
|
|
31
|
+
export function touchFeedCache(name, validators, dir) {
|
|
34
32
|
try {
|
|
35
33
|
const target = dir ?? getWritableStateDir();
|
|
36
34
|
const now = new Date();
|
|
37
|
-
fs.utimesSync(path.join(target,
|
|
38
|
-
fs.writeFileSync(path.join(target,
|
|
35
|
+
fs.utimesSync(path.join(target, `${name}.ics`), now, now);
|
|
36
|
+
fs.writeFileSync(path.join(target, `${name}.json`), JSON.stringify(validators), {
|
|
39
37
|
encoding: 'utf8',
|
|
40
38
|
mode: 0o600,
|
|
41
39
|
});
|
|
@@ -44,11 +42,11 @@ export function touchFeedCache(validators, dir) {
|
|
|
44
42
|
/* best effort */
|
|
45
43
|
}
|
|
46
44
|
}
|
|
47
|
-
export function loadFeedCache(dir, maxAgeMs = MAX_AGE_MS) {
|
|
45
|
+
export function loadFeedCache(name, dir, maxAgeMs = MAX_AGE_MS) {
|
|
48
46
|
try {
|
|
49
47
|
const source = dir ?? getStateDir();
|
|
50
|
-
const validators = readValidators(source);
|
|
51
|
-
const file = path.join(source,
|
|
48
|
+
const validators = readValidators(name, source);
|
|
49
|
+
const file = path.join(source, `${name}.ics`);
|
|
52
50
|
if (Date.now() - fs.statSync(file).mtimeMs > maxAgeMs)
|
|
53
51
|
return null;
|
|
54
52
|
return { text: fs.readFileSync(file, 'utf8'), validators };
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { fetchFeedConditional, parseCalendar, createCalendar, FeedFetchError, FeedParseError, eventToICS, } from '@nbtca/nbtcal';
|
|
1
|
+
import { fetchFeedConditional, parseCalendar, createCalendar, FeedFetchError, FeedParseError, eventToICS, SCHOOL_FEED_URL, } from '@nbtca/nbtcal';
|
|
2
2
|
import chalk from 'chalk';
|
|
3
3
|
import { c, type, space, glyph, bodyWidth } from '../core/theme.js';
|
|
4
4
|
import { pickIcon } from '../core/icons.js';
|
|
@@ -24,57 +24,75 @@ function formatTime(date) {
|
|
|
24
24
|
return `${hours}:${minutes}`;
|
|
25
25
|
}
|
|
26
26
|
const MEMO_TTL_MS = 5 * 60 * 1000;
|
|
27
|
-
|
|
28
|
-
let
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
}
|
|
33
|
-
export function peekCalendar() {
|
|
34
|
-
if (memo)
|
|
27
|
+
function feedLoader(url, name) {
|
|
28
|
+
let memo;
|
|
29
|
+
let inFlight;
|
|
30
|
+
function remember(text, validators, fetchedAt) {
|
|
31
|
+
memo = { calendar: createCalendar(parseCalendar(text)), validators, fetchedAt };
|
|
35
32
|
return memo.calendar;
|
|
36
|
-
const cache = loadFeedCache();
|
|
37
|
-
if (cache === null)
|
|
38
|
-
return undefined;
|
|
39
|
-
try {
|
|
40
|
-
return remember(cache.text, cache.validators, 0);
|
|
41
33
|
}
|
|
42
|
-
|
|
43
|
-
|
|
34
|
+
function peek() {
|
|
35
|
+
if (memo)
|
|
36
|
+
return memo.calendar;
|
|
37
|
+
const cache = loadFeedCache(name);
|
|
38
|
+
if (cache === null)
|
|
39
|
+
return undefined;
|
|
40
|
+
try {
|
|
41
|
+
return remember(cache.text, cache.validators, 0);
|
|
42
|
+
}
|
|
43
|
+
catch {
|
|
44
|
+
return undefined;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
async function refetch(signal) {
|
|
48
|
+
try {
|
|
49
|
+
peek();
|
|
50
|
+
const result = await fetchFeedConditional(url, {
|
|
51
|
+
timeoutMs: 15000,
|
|
52
|
+
...(signal === undefined ? {} : { signal }),
|
|
53
|
+
validators: memo?.validators ?? {},
|
|
54
|
+
});
|
|
55
|
+
if (result.status === 'modified') {
|
|
56
|
+
const calendar = remember(result.text, result.validators, Date.now());
|
|
57
|
+
saveFeedCache(name, result);
|
|
58
|
+
return calendar;
|
|
59
|
+
}
|
|
60
|
+
if (!memo)
|
|
61
|
+
throw new FeedFetchError('Feed request failed: HTTP 304');
|
|
62
|
+
memo = { ...memo, validators: result.validators, fetchedAt: Date.now() };
|
|
63
|
+
touchFeedCache(name, result.validators);
|
|
64
|
+
return memo.calendar;
|
|
65
|
+
}
|
|
66
|
+
catch (err) {
|
|
67
|
+
const detail = sanitizeTerminalLine(err instanceof FeedFetchError || err instanceof FeedParseError ? err.message : String(err));
|
|
68
|
+
throw new Error(`${t().calendar.error}: ${detail}`);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
async function load(signal) {
|
|
72
|
+
if (memo && Date.now() - memo.fetchedAt < MEMO_TTL_MS)
|
|
73
|
+
return memo.calendar;
|
|
74
|
+
inFlight ??= refetch(signal).finally(() => {
|
|
75
|
+
inFlight = undefined;
|
|
76
|
+
});
|
|
77
|
+
return inFlight;
|
|
44
78
|
}
|
|
79
|
+
return { peek, load };
|
|
45
80
|
}
|
|
46
|
-
|
|
81
|
+
const eventsFeed = feedLoader(undefined, 'calendar-feed');
|
|
82
|
+
const schoolFeed = feedLoader(SCHOOL_FEED_URL, 'school-feed');
|
|
83
|
+
export const peekCalendar = eventsFeed.peek;
|
|
84
|
+
export const loadCalendarOrThrow = eventsFeed.load;
|
|
85
|
+
export async function loadSchoolCalendar(signal) {
|
|
47
86
|
try {
|
|
48
|
-
|
|
49
|
-
const result = await fetchFeedConditional(undefined, {
|
|
50
|
-
timeoutMs: 15000,
|
|
51
|
-
...(signal === undefined ? {} : { signal }),
|
|
52
|
-
validators: memo?.validators ?? {},
|
|
53
|
-
});
|
|
54
|
-
if (result.status === 'modified') {
|
|
55
|
-
const calendar = remember(result.text, result.validators, Date.now());
|
|
56
|
-
saveFeedCache(result);
|
|
57
|
-
return calendar;
|
|
58
|
-
}
|
|
59
|
-
if (!memo)
|
|
60
|
-
throw new FeedFetchError('Feed request failed: HTTP 304');
|
|
61
|
-
memo = { ...memo, validators: result.validators, fetchedAt: Date.now() };
|
|
62
|
-
touchFeedCache(result.validators);
|
|
63
|
-
return memo.calendar;
|
|
87
|
+
return await schoolFeed.load(signal);
|
|
64
88
|
}
|
|
65
89
|
catch (err) {
|
|
66
|
-
const
|
|
67
|
-
|
|
90
|
+
const cached = schoolFeed.peek();
|
|
91
|
+
if (!cached)
|
|
92
|
+
throw err;
|
|
93
|
+
return cached;
|
|
68
94
|
}
|
|
69
95
|
}
|
|
70
|
-
export async function loadCalendarOrThrow(signal) {
|
|
71
|
-
if (memo && Date.now() - memo.fetchedAt < MEMO_TTL_MS)
|
|
72
|
-
return memo.calendar;
|
|
73
|
-
inFlight ??= refetchCalendar(signal).finally(() => {
|
|
74
|
-
inFlight = undefined;
|
|
75
|
-
});
|
|
76
|
-
return inFlight;
|
|
77
|
-
}
|
|
78
96
|
export function toDisplayEvent(e) {
|
|
79
97
|
const trans = t();
|
|
80
98
|
return {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nbtca/prompt",
|
|
3
|
-
"version": "1.5.
|
|
3
|
+
"version": "1.5.19",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"exports": {
|
|
@@ -47,7 +47,7 @@
|
|
|
47
47
|
],
|
|
48
48
|
"dependencies": {
|
|
49
49
|
"@nbtca/docs": "^0.3.4",
|
|
50
|
-
"@nbtca/nbtcal": "^0.4.
|
|
50
|
+
"@nbtca/nbtcal": "^0.4.4",
|
|
51
51
|
"chalk": "^5.6.2",
|
|
52
52
|
"marked": "^15.0.12"
|
|
53
53
|
},
|