From c763a020e6bfce05216d14928845e97b78bbfdb4 Mon Sep 17 00:00:00 2001 From: m1ngsama Date: Wed, 18 Nov 2026 10:12:03 +0800 Subject: [PATCH] feat(tui): date the week, show class ranges, and list untimed courses --- src/app/views/schedule-render.test.ts | 6 +-- src/app/views/schedule-render.ts | 10 +++-- src/features/schedule-render.test.ts | 27 +++++++++++ src/features/schedule-render.ts | 65 ++++++++++++++++++++------- src/i18n/index.ts | 1 + src/i18n/locales/en.json | 3 +- src/i18n/locales/zh.json | 3 +- 7 files changed, 91 insertions(+), 24 deletions(-) diff --git a/src/app/views/schedule-render.test.ts b/src/app/views/schedule-render.test.ts index 07ea1f2..c213e0a 100644 --- a/src/app/views/schedule-render.test.ts +++ b/src/app/views/schedule-render.test.ts @@ -464,8 +464,8 @@ describe('renderSchedule', () => { ).map(stripAnsi); const out = lines.join('\n'); - expect(out).toMatch(/Mon\S? +08:00 Math/); - expect(out).toMatch(/ +10:00 Physics/); + expect(out).toMatch(/Mon 9\/7\S? +08:00\S* +Math/); + expect(out).toMatch(/ +10:00\S* +Physics/); expect(out).toContain('Sun'); expect(lines.some((line) => line.trimStart().startsWith('←'))).toBe(false); }); @@ -684,7 +684,7 @@ describe('renderSchedule', () => { lines.find((l) => /^\s*08:00.08:45 /.test(l)); expect(gridRow(narrowLines)).toBeUndefined(); - expect(narrowLines.join('\n')).toMatch(new RegExp(`08:00 ${longName}`)); + expect(narrowLines.join('\n')).toMatch(new RegExp(`08:00\\S* +${longName}`)); expect(gridRow(wideLines)).toContain(longName); }); }); diff --git a/src/app/views/schedule-render.ts b/src/app/views/schedule-render.ts index e788a81..5a26d1d 100644 --- a/src/app/views/schedule-render.ts +++ b/src/app/views/schedule-render.ts @@ -269,6 +269,7 @@ function renderHubWeek( inlineLines: string[], fallbackLines: string[], tt: Timetable, + weekOne: string | undefined, week: number, todayWd: Weekday, now: Date, @@ -278,7 +279,7 @@ function renderHubWeek( ): HubWeek { const layouts: [string[] | null, boolean][] = [ [fittingWeekGrid(tt, week, now, rows, cols, cursor), true], - [renderWeekAgenda(tt, week, now, cols, cursor), false], + [renderWeekAgenda(tt, week, now, cols, cursor, false, weekOne), false], ]; for (const [layout, grid] of layouts) { if (!layout) continue; @@ -294,6 +295,7 @@ function renderHubWeek( function renderWeekBody( tt: Timetable, + weekOne: string | undefined, week: number, todayWd: Weekday, now: Date, @@ -305,8 +307,8 @@ function renderWeekBody( const title = heading(t().timetable.hubWeek); const layouts = [ fittingWeekGrid(tt, week, now, rows - 2, cols, cursor), - renderWeekAgenda(tt, week, now, cols, cursor), - renderWeekAgenda(tt, week, now, cols, cursor, true), + renderWeekAgenda(tt, week, now, cols, cursor, false, weekOne), + renderWeekAgenda(tt, week, now, cols, cursor, true, weekOne), ]; for (const layout of layouts) { if (!layout) continue; @@ -368,6 +370,7 @@ function layoutHub( pre.inlineLines, pre.fallbackLines, pre.tt, + state.weekOne, pre.week, createTimetableSchedule(pre.tt).weekdayAt(now), now, @@ -556,6 +559,7 @@ export function renderSchedule( }); return renderWeekBody( state.timetable, + state.weekOne, Math.max(1, schedule.weekAt(now)), schedule.weekdayAt(now), now, diff --git a/src/features/schedule-render.test.ts b/src/features/schedule-render.test.ts index 641546d..0cf3fb4 100644 --- a/src/features/schedule-render.test.ts +++ b/src/features/schedule-render.test.ts @@ -1377,6 +1377,33 @@ describe('renderWeekAgenda', () => { }; const monday = campusDateTime('2026-09-07', '07:00'); + it('dates each day, shows time ranges, and notes untimed courses in their week', () => { + const withUntimed: Timetable = { + ...agendaTimetable, + untimedCourses: [ + { + sourceId: null, + courseName: 'Fitness test', + teacherNames: [], + campus: null, + location: null, + weeks: [1], + kind: 'practice', + }, + ], + }; + const lines = renderWeekAgenda(withUntimed, 1, monday, 80, undefined, false, '2026-09-07').map( + stripAnsi, + ); + const out = lines.join('\n'); + expect(out).toMatch(/Mon 9\/7\S* +08:00-08:45 Math/); + expect(out).toContain('Thu 9/10'); + expect(lines.at(-1)).toMatch(/Fitness test - time to be announced/); + expect( + renderWeekAgenda(withUntimed, 2, monday, 80, undefined, false, '2026-09-07').join('\n'), + ).not.toContain('Fitness test'); + }); + it('lists every weekday with its classes and locations', () => { const lines = renderWeekAgenda(agendaTimetable, 1, monday, 80).map(stripAnsi); expect(lines).toHaveLength(8); diff --git a/src/features/schedule-render.ts b/src/features/schedule-render.ts index d19ad7f..0258117 100644 --- a/src/features/schedule-render.ts +++ b/src/features/schedule-render.ts @@ -184,7 +184,7 @@ function renderTimeline( }; }); const nameColW = Math.max(...rows.map((row) => visualWidth(row.m.courseName))); - const statusColW = Math.max(...rows.map((row) => visualWidth(row.statusText))); + const locationColW = Math.max(...rows.map((row) => visualWidth(row.locationText))); const lines = rows.map( ({ m, startStr, isLive, isDone, minutesLeft, statusText, locationText }, i) => { @@ -200,39 +200,40 @@ function renderTimeline( const renderLine = ( name: string, status: string, - location: string, + location: string | undefined, currentTimeCol = timeCol, indent: string = space.indent, ): string => { const aligned = name === m.courseName; - const statusW = aligned && location ? statusColW : 0; - const statusCol = status || statusW > 0 ? ` ${type.hint(padEndV(status, statusW))}` : ''; - const locationCol = location ? ` ${type.hint(location)}` : ''; + const locationW = aligned && status && location !== undefined ? locationColW : 0; + const locationCol = + location || locationW > 0 ? ` ${type.hint(padEndV(location ?? '', locationW))}` : ''; + const statusCol = status ? ` ${type.hint(status)}` : ''; const shownName = aligned && (statusCol || locationCol) ? padEndV(name, nameColW) : name; - const content = `${currentTimeCol} ${styleName(shownName)}${statusCol}${locationCol}`; + const content = `${currentTimeCol} ${styleName(shownName)}${locationCol}${statusCol}`; return `${indent}${isCursor ? type.cursor(content) : content}`; }; const whole = [ renderLine(m.courseName, statusText, locationText), - renderLine(m.courseName, statusText, ''), - renderLine(m.courseName, compactStatusText, ''), + renderLine(m.courseName, statusText, undefined), + renderLine(m.courseName, compactStatusText, undefined), ].find(fits); if (whole) return whole; for (const status of compactStatusText ? [compactStatusText, ''] : ['']) { - const courseWidth = Math.floor(width - visualWidth(renderLine('', status, ''))); + const courseWidth = Math.floor(width - visualWidth(renderLine('', status, undefined))); if (courseWidth <= visualWidth(ellipsis())) continue; - return renderLine(clip(m.courseName, courseWidth), status, ''); + return renderLine(clip(m.courseName, courseWidth), status, undefined); } const compactTimeCol = `${marker}${type.hint(startStr)}`; for (const indent of [space.indent, '']) { const courseWidth = Math.floor( - width - visualWidth(renderLine('', '', '', compactTimeCol, indent)), + width - visualWidth(renderLine('', '', undefined, compactTimeCol, indent)), ); if (courseWidth <= visualWidth(ellipsis())) continue; - return renderLine(clip(m.courseName, courseWidth), '', '', compactTimeCol, indent); + return renderLine(clip(m.courseName, courseWidth), '', undefined, compactTimeCol, indent); } const timeOnly = [ @@ -270,7 +271,7 @@ export function renderTodayTimeline( periods, now, { weekday: 0, isToday: true }, - false, + true, undefined, cols, ); @@ -500,6 +501,7 @@ export function renderWeekAgenda( cols = Number.POSITIVE_INFINITY, cursor?: { weekday: number; period: number }, oneLinePerDay = false, + weekOneMonday?: string, ): string[] { const schedule = createTimetableSchedule(timetable); const todayWd = schedule.weekdayAt(now); @@ -507,9 +509,23 @@ export function renderWeekAgenda( 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 today = campusIsoDate(now); + const shortDate = (iso: string | undefined) => + iso === undefined ? '' : ` ${Number(iso.slice(5, 7))}/${Number(iso.slice(8, 10))}`; const labels = WEEKDAY_KEYS.map( - (_, i) => `${weekdayShortLabel(i + 1)}${i + 1 === todayWd ? todayMark : ''}`, + (_, i) => + `${weekdayShortLabel(i + 1)}${oneLinePerDay ? '' : shortDate(dates[i])}${i + 1 === todayWd ? todayMark : ''}`, ); + const isOver = (wd: number, m: TimetableMeeting): boolean => { + const date = dates[wd - 1]; + if (date === undefined) return false; + if (date !== today) return date < today; + const end = timetable.periods.find((p) => p.period === m.endPeriod)?.end; + return end !== undefined && campusDateTime(date, end).getTime() <= now.getTime(); + }; const labelW = Math.max(...labels.map(visualWidth)); const nameColW = Math.max( 0, @@ -537,7 +553,7 @@ export function renderWeekAgenda( }; const room = Math.max(1, width - visualWidth(head(0))); const nameStyle = (m: TimetableMeeting): ((value: string) => string) => - isSelected(m) ? type.active : type.body; + isSelected(m) ? type.active : isOver(wd, m) ? type.hint : type.body; if (meetings.length === 0) { lines.push(`${head(0)}${type.hint(dot)}`); @@ -564,7 +580,11 @@ export function renderWeekAgenda( meetings.forEach((m, row) => { const start = timetable.periods.find((p) => p.period === m.startPeriod)?.start ?? ''; - const time = `${start} `; + const ranged = `${span(m, timetable.periods)} `; + const time = + room - visualWidth(ranged) >= nameColW + 3 + visualWidth(m.location ?? '') + ? ranged + : `${start} `; const nameRoom = room - visualWidth(time); const location = m.location ?? ''; const nameW = [nameColW, visualWidth(m.courseName)].find( @@ -577,9 +597,22 @@ export function renderWeekAgenda( lines.push(`${head(row)}${type.hint(time)}${body}`); }); }); + for (const course of timetable.untimedCourses ?? []) { + if (!course.weeks.includes(weekNumber)) continue; + const mark = type.hint(pickIcon('◇', '*')); + const text = `${course.courseName} ${dot} ${t().timetable.untimedNote}`; + lines.push( + `${space.indent}${' '.repeat(visualWidth(pointer))} ${mark} ${type.body(clip(text, Math.max(1, width - visualWidth(pointer) - 4)))}`, + ); + } return lines; } +function teachingDate(weekOneMonday: string, week: number, weekday: number): string { + const monday = campusDateTime(weekOneMonday, '12:00').getTime(); + return campusIsoDate(new Date(monday + ((week - 1) * 7 + weekday - 1) * 86_400_000)); +} + /** 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); diff --git a/src/i18n/index.ts b/src/i18n/index.ts index b95b8db..9c39168 100644 --- a/src/i18n/index.ts +++ b/src/i18n/index.ts @@ -373,6 +373,7 @@ export interface Translations { detailTomorrow: string; detailAlso: string; detailWeeksValue: string; + untimedNote: string; teacherSeparator: string; noClassOnDay: string; countdownDays: string; diff --git a/src/i18n/locales/en.json b/src/i18n/locales/en.json index f32b505..ff854e8 100644 --- a/src/i18n/locales/en.json +++ b/src/i18n/locales/en.json @@ -468,7 +468,8 @@ "helpOpenClass": "Class details", "footerDay": "day", "footerClass": "class", - "footerDetail": "details" + "footerDetail": "details", + "untimedNote": "time to be announced" }, "theme": { "current": "Current theme settings", diff --git a/src/i18n/locales/zh.json b/src/i18n/locales/zh.json index 56d8b44..30d3fd5 100644 --- a/src/i18n/locales/zh.json +++ b/src/i18n/locales/zh.json @@ -468,7 +468,8 @@ "helpOpenClass": "查看课程详情", "footerDay": "星期", "footerClass": "课程", - "footerDetail": "详情" + "footerDetail": "详情", + "untimedNote": "时间待定" }, "theme": { "current": "当前主题设置",