Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ Or try it without installing: `npx @nbtca/prompt`.
`Enter` to read full details. Press `/` to search by course name or code, `f`
to filter upcoming exams or unpublished and void grades, and `s` to change
terms. Wide terminals show details beside the list.
Press `w` for the full week; `←` on Monday and `→` on Sunday turn to the
previous or next week, and `t` returns to this one. `Enter` on a class shows
its periods, progress through the term, and next session.
Press `r` for a term recap: how many classes you had, early starts, your
fullest day, the course and room you saw most, and club events held in the
same weeks. Near the end of term, Home tells you the recap is ready.
Expand Down
17 changes: 12 additions & 5 deletions src/app/views/schedule-render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
weekdayShortLabel,
renderTermDensity,
renderMeetingDetail,
weekTitle,
renderDayTimeline,
renderDaySwitcher,
renderWeekAgenda,
Expand Down Expand Up @@ -80,6 +81,8 @@ export interface ScheduleViewState {
gridCursor?: GridCursor;
detailMeeting?: TimetableMeeting;
detailFrom?: 'hub' | 'week';
/** The week shown by the full grid; the current week when unset. */
viewWeek?: number;
recap?: { recap: TermRecap; cards: RecapCard[]; index: number; shownAt: number };
}

Expand Down Expand Up @@ -227,14 +230,15 @@ function hubPreGridLines(

function fittingWeekGrid(
tt: Timetable,
weekOne: string | undefined,
week: number,
now: Date,
rows: number,
cols: number,
cursor: GridCursor | undefined,
): string[] | null {
if (cols < Math.min(MAX_FRAME_COLS, weekGridFullWidth(tt, week, now))) return null;
const grid = renderWeekGrid(tt, week, now, cols, cursor).split('\n');
const grid = renderWeekGrid(tt, week, now, cols, cursor, weekOne).split('\n');
return grid.length <= rows ? grid : null;
}

Expand Down Expand Up @@ -278,7 +282,7 @@ function renderHubWeek(
cursor: GridCursor | undefined,
): HubWeek {
const layouts: [string[] | null, boolean][] = [
[fittingWeekGrid(tt, week, now, rows, cols, cursor), true],
[fittingWeekGrid(tt, weekOne, week, now, rows, cols, cursor), true],
[renderWeekAgenda(tt, week, now, cols, cursor, false, weekOne), false],
];
for (const [layout, grid] of layouts) {
Expand All @@ -296,6 +300,7 @@ function renderHubWeek(
function renderWeekBody(
tt: Timetable,
weekOne: string | undefined,
currentWeek: number,
week: number,
todayWd: Weekday,
now: Date,
Expand All @@ -304,9 +309,9 @@ function renderWeekBody(
cursor: GridCursor | undefined,
): string[] {
const rows = Math.max(0, Math.floor(bodyRows));
const title = heading(t().timetable.hubWeek);
const title = heading(weekTitle(weekOne, week, currentWeek));
const layouts = [
fittingWeekGrid(tt, week, now, rows - 2, cols, cursor),
fittingWeekGrid(tt, weekOne, week, now, rows - 2, cols, cursor),
renderWeekAgenda(tt, week, now, cols, cursor, false, weekOne),
renderWeekAgenda(tt, week, now, cols, cursor, true, weekOne),
];
Expand Down Expand Up @@ -557,10 +562,12 @@ export function renderSchedule(
const schedule = createTimetableSchedule(state.timetable, {
weekOneMonday: state.weekOne,
});
const currentWeek = Math.max(1, schedule.weekAt(now));
return renderWeekBody(
state.timetable,
state.weekOne,
Math.max(1, schedule.weekAt(now)),
currentWeek,
state.viewWeek ?? currentWeek,
schedule.weekdayAt(now),
now,
bodyRows,
Expand Down
31 changes: 31 additions & 0 deletions src/app/views/schedule.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,37 @@ describe('scheduleView — hub navigation', () => {
expect(out).toContain(t().timetable.hubLogout);
});

it('turns to the next week past Sunday, back past Monday, and home with "t"', async () => {
const ctx = await loadIntoHub({
meetings: [
{
sourceId: null,
courseName: 'Week two only',
teacherNames: [],
location: null,
weekday: 1,
startPeriod: 1,
endPeriod: 1,
weeks: [2],
kind: 'regular',
},
],
});
const view = () => stripAnsi(scheduleView.render(ctx).join('\n'));
scheduleView.handleKey('w', ctx);
expect(view()).not.toContain('Week two only');
for (let day = 1; day <= 7; day++) scheduleView.handleKey('\x1b[C', ctx);
expect(view()).toContain(t().timetable.weekNext);
expect(view()).toContain('Week two only');
expect(view()).toContain('9/14');
scheduleView.handleKey('\x1b[D', ctx);
expect(view()).toContain(t().timetable.hubWeek);
scheduleView.handleKey('\x1b[C', ctx);
scheduleView.handleKey('t', ctx);
expect(view()).toContain(t().timetable.hubWeek);
expect(view()).not.toContain('Week two only');
});

it('stays in the week grid when Enter lands on an empty cell', async () => {
const ctx = await loadIntoHub();
scheduleView.handleKey('w', ctx);
Expand Down
25 changes: 23 additions & 2 deletions src/app/views/schedule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -545,7 +545,7 @@ export const scheduleView = {
{ key: keys.periods, label: trans.timetable.helpPickClass },
{ key: keys.enter, label: trans.timetable.helpOpenClass },
];
if (state.mode === 'week') return grid;
if (state.mode === 'week') return [...grid, { key: 't', label: trans.timetable.weekToday }];
return [...grid, ...hubShortcuts(state.timetable).map(({ key, label }) => ({ key, label }))];
},

Expand Down Expand Up @@ -851,7 +851,28 @@ export const scheduleView = {
const schedule = createTimetableSchedule(tt, { weekOneMonday: weekOne });
const now = new Date();
const cursor = state.gridCursor ?? todayGridCursor(schedule, tt.periods, now);
const week = Math.max(1, schedule.weekAt(now));
const currentWeek = Math.max(1, schedule.weekAt(now));
const week = state.viewWeek ?? currentWeek;
const lastWeek = Math.max(currentWeek, ...tt.meetings.flatMap((m) => m.weeks));
if (key === 't') {
state = { ...state, gridCursor: todayGridCursor(schedule, tt.periods, now) };
delete state.viewWeek;
return;
}
const turn =
key === KEY_ARROW_LEFT && cursor.weekday === 1 && week > 1
? { week: week - 1, weekday: 7 as const }
: key === KEY_ARROW_RIGHT && cursor.weekday === 7 && week < lastWeek
? { week: week + 1, weekday: 1 as const }
: undefined;
if (turn) {
state = {
...state,
viewWeek: turn.week,
gridCursor: { ...cursor, weekday: turn.weekday },
};
return;
}
const nav = handleGridKey(key, cursor, tt, week);
if (nav.kind === 'moveCursor') {
state = { ...state, gridCursor: nav.cursor };
Expand Down
19 changes: 19 additions & 0 deletions src/features/schedule-render.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
renderWeekAgenda,
formatClassCountdown,
weekdayShortLabel,
weekTitle,
} from './schedule-render.js';
import { setLanguage } from '../i18n/index.js';
import type { CourseProgress } from './course-progress.js';
Expand Down Expand Up @@ -1404,6 +1405,24 @@ describe('renderWeekAgenda', () => {
).not.toContain('Fitness test');
});

it('names the week relative to now and marks today only in the current week', () => {
expect(weekTitle('2026-09-07', 1, 1)).toBe('This week - Week 1 - 9/7-9/13');
expect(weekTitle('2026-09-07', 2, 1)).toContain('Next week');
expect(weekTitle('2026-09-07', 4, 1)).toBe('Week 4 - 9/28-10/4');
const current = renderWeekAgenda(
agendaTimetable,
1,
monday,
80,
undefined,
false,
'2026-09-07',
);
const later = renderWeekAgenda(agendaTimetable, 2, monday, 80, undefined, false, '2026-09-07');
expect(stripAnsi(current.join('\n'))).toContain('Mon 9/7*');
expect(stripAnsi(later.join('\n'))).not.toContain('*');
});

it('lists every weekday with its classes and locations', () => {
const lines = renderWeekAgenda(agendaTimetable, 1, monday, 80).map(stripAnsi);
expect(lines).toHaveLength(8);
Expand Down
55 changes: 47 additions & 8 deletions src/features/schedule-render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -402,10 +402,12 @@ export function renderWeekGrid(
now: Date,
cols = 80,
cursor?: { weekday: number; period: number },
weekOneMonday?: string,
): string {
const schedule = createTimetableSchedule(timetable);
const week = schedule.meetingsInWeek(weekNumber);
const todayWd = schedule.weekdayAt(now);
const dates = weekDates(weekOneMonday, weekNumber);
const todayWd = todayInWeek(schedule.weekdayAt(now), dates, now);
const periods = timetable.periods;
const todayMark = pickIcon('•', '*');
const connector = pickIcon('│', '|');
Expand Down Expand Up @@ -433,7 +435,7 @@ export function renderWeekGrid(

const headerCells = WEEKDAY_KEYS.map((_, i) => {
const wd = i + 1;
const d = weekdayShortLabel(wd);
const d = `${weekdayShortLabel(wd)}${shortDate(dates[i])}`;
const label = wd === todayWd ? `${d}${todayMark}` : d;
const colWidth = colWidths[i] ?? 3;
const padded = centerInWidth(clip(label, colWidth), colWidth);
Expand Down Expand Up @@ -504,17 +506,13 @@ export function renderWeekAgenda(
weekOneMonday?: string,
): string[] {
const schedule = createTimetableSchedule(timetable);
const todayWd = schedule.weekdayAt(now);
const width = lineBudget(cols);
const todayMark = pickIcon('•', '*');
const dot = glyph.sep();
const pointer = glyph.cursor();
const dates = WEEKDAY_KEYS.map((_, i) =>
weekOneMonday === undefined ? undefined : teachingDate(weekOneMonday, weekNumber, i + 1),
);
const dates = weekDates(weekOneMonday, weekNumber);
const today = campusIsoDate(now);
const shortDate = (iso: string | undefined) =>
iso === undefined ? '' : ` ${Number(iso.slice(5, 7))}/${Number(iso.slice(8, 10))}`;
const todayWd = todayInWeek(schedule.weekdayAt(now), dates, now);
const labels = WEEKDAY_KEYS.map(
(_, i) =>
`${weekdayShortLabel(i + 1)}${oneLinePerDay ? '' : shortDate(dates[i])}${i + 1 === todayWd ? todayMark : ''}`,
Expand Down Expand Up @@ -613,6 +611,47 @@ function teachingDate(weekOneMonday: string, week: number, weekday: number): str
return campusIsoDate(new Date(monday + ((week - 1) * 7 + weekday - 1) * 86_400_000));
}

function weekDates(weekOneMonday: string | undefined, week: number): (string | undefined)[] {
return WEEKDAY_KEYS.map((_, i) =>
weekOneMonday === undefined ? undefined : teachingDate(weekOneMonday, week, i + 1),
);
}

/** Today's weekday when the shown week contains today; 0 when another week is shown. */
function todayInWeek(weekday: number, dates: readonly (string | undefined)[], now: Date): number {
if (dates.every((date) => date === undefined)) return weekday;
return dates.indexOf(campusIsoDate(now)) + 1;
}

function shortDate(iso: string | undefined): string {
return iso === undefined ? '' : ` ${Number(iso.slice(5, 7))}/${Number(iso.slice(8, 10))}`;
}

/** "本周 · 第 10 周 · 11/16–11/22", naming the week relative to now when it is adjacent. */
export function weekTitle(
weekOneMonday: string | undefined,
week: number,
currentWeek: number,
): string {
const trans = t().timetable;
const relative =
week === currentWeek
? trans.hubWeek
: week === currentWeek + 1
? trans.weekNext
: week === currentWeek - 1
? trans.weekPrevious
: undefined;
const dates = weekDates(weekOneMonday, week);
const first = dates[0];
const last = dates[6];
const range =
first && last ? rangeLabel(shortDate(first).trim(), shortDate(last).trim()) : undefined;
return [relative, fmt(trans.weekLabel2, { week: String(week) }), range]
.filter((part) => part !== undefined)
.join(` ${glyph.sep()} `);
}

/** Lists weeks with runs of three or more collapsed, such as "1, 3, 5–17". */
function formatWeekRange(weeks: readonly number[]): string {
const sorted = [...new Set(weeks)].sort((a, b) => a - b);
Expand Down
3 changes: 3 additions & 0 deletions src/i18n/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,9 @@ export interface Translations {
detailAlso: string;
detailWeeksValue: string;
untimedNote: string;
weekNext: string;
weekPrevious: string;
weekToday: string;
teacherSeparator: string;
noClassOnDay: string;
countdownDays: string;
Expand Down
5 changes: 4 additions & 1 deletion src/i18n/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,10 @@
"footerDay": "day",
"footerClass": "class",
"footerDetail": "details",
"untimedNote": "time to be announced"
"untimedNote": "time to be announced",
"weekNext": "Next week",
"weekPrevious": "Last week",
"weekToday": "this week"
},
"theme": {
"current": "Current theme settings",
Expand Down
5 changes: 4 additions & 1 deletion src/i18n/locales/zh.json
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,10 @@
"footerDay": "星期",
"footerClass": "课程",
"footerDetail": "详情",
"untimedNote": "时间待定"
"untimedNote": "时间待定",
"weekNext": "下周",
"weekPrevious": "上周",
"weekToday": "回到本周"
},
"theme": {
"current": "当前主题设置",
Expand Down
Loading