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
10 changes: 9 additions & 1 deletion src/app/views/schedule-render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
import type { AcademicWindow, OnBreak } from '@nbtca/nbtcal';
import type { GridCursor } from './schedule-grid-cursor.js';
import { renderRecapCard, type RecapCard } from './term-recap-render.js';
import { courseProgress } from '../../features/course-progress.js';
import type { TermRecap } from '../../features/term-recap.js';
import { getCapabilities } from '../../core/capabilities.js';
import {
Expand Down Expand Up @@ -598,7 +599,14 @@ export function renderSchedule(
}
case 'meetingDetail':
return state.detailMeeting && state.timetable
? renderMeetingDetail(state.detailMeeting, state.timetable.periods, cols).split('\n')
? renderMeetingDetail(
state.detailMeeting,
state.timetable.periods,
cols,
state.weekOne
? courseProgress(state.timetable, state.weekOne, state.detailMeeting, now)
: undefined,
).split('\n')
: [hint(trans.timetable.genericError)];
case 'error':
return hintLines(state.errorMessage ?? trans.timetable.genericError, cols);
Expand Down
56 changes: 56 additions & 0 deletions src/features/course-progress.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { describe, it, expect } from 'vitest';
import { campusDateTime, type Timetable, type TimetableMeeting } from '@nbtca/nbtcal/timetable';
import { courseProgress } from './course-progress.js';

const meeting = (o: Partial<TimetableMeeting>): TimetableMeeting => ({
sourceId: null,
courseName: 'Accounting',
teacherNames: [],
location: 'SF104',
weekday: 4,
startPeriod: 1,
endPeriod: 2,
weeks: [1, 2, 3],
kind: 'regular',
...o,
});

const thursday = meeting({});
const makeUp = meeting({ weekday: 1, weeks: [4] });
const timetable: Timetable = {
term: { academicYear: '2026', semester: '3' },
meetings: [thursday, makeUp, meeting({ courseName: 'Other', weekday: 2, weeks: [1, 2, 3, 4] })],
unresolvedItems: [],
periods: [
{ period: 1, label: null, start: '08:00', end: '08:45' },
{ period: 2, label: null, start: '08:50', end: '09:35' },
],
calendarDays: [],
warnings: [],
fetchedAt: new Date('2026-09-01T00:00:00Z'),
};
const WEEK_ONE = '2026-09-14';

describe('courseProgress', () => {
it('counts every slot of the course and finds the next session', () => {
const now = campusDateTime('2026-09-25', '12:00');
const progress = courseProgress(timetable, WEEK_ONE, thursday, now);
expect(progress).toMatchObject({ held: 2, total: 4, termWeeks: 4, currentWeek: 2 });
expect(progress?.next?.date).toBe('2026-10-01');
expect(progress?.live).toBeUndefined();
expect(progress?.others).toEqual([makeUp]);
});

it('reports the session in progress', () => {
const now = campusDateTime('2026-09-17', '08:30');
const progress = courseProgress(timetable, WEEK_ONE, thursday, now);
expect(progress?.live?.date).toBe('2026-09-17');
expect(progress?.held).toBe(0);
});

it('has no next session once the course is over', () => {
const progress = courseProgress(timetable, WEEK_ONE, thursday, new Date('2026-12-01'));
expect(progress).toMatchObject({ held: 4, total: 4 });
expect(progress?.next).toBeUndefined();
});
});
56 changes: 56 additions & 0 deletions src/features/course-progress.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import {
createTimetableSchedule,
type Timetable,
type TimetableMeeting,
type TimetableOccurrence,
} from '@nbtca/nbtcal/timetable';

export interface CourseProgress {
now: Date;
held: number;
total: number;
live?: TimetableOccurrence;
next?: TimetableOccurrence;
/** The course's other weekly slots, such as a second weekday or a make-up week. */
others: TimetableMeeting[];
termWeeks: number;
currentWeek: number;
}

const sameSlot = (a: TimetableMeeting, b: TimetableMeeting): boolean =>
a.courseName === b.courseName &&
a.weekday === b.weekday &&
a.startPeriod === b.startPeriod &&
a.endPeriod === b.endPeriod;

/** Where a course stands this term, counted over every slot it has; undefined when the timetable cannot be scheduled. */
export function courseProgress(
timetable: Timetable,
weekOneMonday: string,
meeting: TimetableMeeting,
now: Date,
): CourseProgress | undefined {
try {
const schedule = createTimetableSchedule(timetable, { weekOneMonday });
const at = now.getTime();
const sessions = schedule
.occurrences()
.filter((o) => o.meeting.courseName === meeting.courseName);
const live = sessions.find((o) => o.start.getTime() <= at && at < o.end.getTime());
const next = sessions.find((o) => o.start.getTime() > at);
return {
now,
held: sessions.filter((o) => o.end.getTime() <= at).length,
total: sessions.length,
...(live ? { live } : {}),
...(next ? { next } : {}),
others: timetable.meetings.filter(
(m) => m.courseName === meeting.courseName && !sameSlot(m, meeting),
),
termWeeks: Math.max(0, ...timetable.meetings.flatMap((m) => m.weeks)),
currentWeek: schedule.weekAt(now),
};
} catch {
return undefined;
}
}
45 changes: 45 additions & 0 deletions src/features/schedule-render.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
weekdayShortLabel,
} from './schedule-render.js';
import { setLanguage } from '../i18n/index.js';
import type { CourseProgress } from './course-progress.js';
import { resetIconCache } from '../core/icons.js';
import { campusDateTime } from '@nbtca/nbtcal/timetable';
import { stripAnsi, visualWidth } from '../core/text.js';
Expand Down Expand Up @@ -778,6 +779,50 @@ describe('renderMeetingDetail', () => {
expect(out).toContain('1, 3, 5');
});

it('collapses runs of three or more weeks inside a list', () => {
const out = stripAnsi(renderMeetingDetail(mk({ weeks: [1, 3, 5, 6, 7, 8, 10, 11] }), periods));
expect(out).toContain('1, 3, 5-8, 10, 11');
});

it('adds periods, length, progress, the next session, and other slots', () => {
const meeting = mk({ weekday: 4, weeks: [1, 2, 3] });
const other = mk({ weekday: 1, weeks: [4] });
const progress = {
now: campusDateTime('2026-09-16', '12:00'),
held: 1,
total: 4,
next: {
meeting,
week: 1,
date: '2026-09-17',
startTime: '08:00',
endTime: '09:40',
start: campusDateTime('2026-09-17', '08:00'),
end: campusDateTime('2026-09-17', '09:40'),
},
others: [other],
termWeeks: 4,
currentWeek: 1,
};
const out = stripAnsi(renderMeetingDetail(meeting, periods, 100, progress));
expect(out).toMatch(/Time\s+Thu 08:00-09:40 - periods 1-2 - 100 min/);
expect(out).toContain('now week 1');
expect(out).toContain('# # # .');
expect(out).toContain('1 done, 3 to go');
expect(out).toMatch(/Next\s+Tomorrow 08:00 - in 20h/);
expect(out).toMatch(/Also\s+Mon 08:00-09:40 - weeks 4/);

const live = { ...progress, live: progress.next, now: campusDateTime('2026-09-17', '09:00') };
expect(stripAnsi(renderMeetingDetail(meeting, periods, 100, live))).toContain(
'In class now, 40 min left',
);
const ended: CourseProgress = { ...progress };
delete ended.next;
expect(stripAnsi(renderMeetingDetail(meeting, periods, 100, ended))).toContain(
'Finished for this term',
);
});

it('never collapses into one array entry when split on newlines', () => {
const out = renderMeetingDetail(mk({}), periods);
expect(out.split('\n').length).toBeGreaterThan(1);
Expand Down
Loading
Loading