From 19c234b6fe9b227c0923bf894759551e4d12444f Mon Sep 17 00:00:00 2001 From: m1ngsama Date: Thu, 19 Nov 2026 10:51:27 +0800 Subject: [PATCH] feat(tui): widen the full timetable on large terminals --- src/app/app.ts | 8 +++-- src/app/frame.test.ts | 13 ++++++- src/app/frame.ts | 5 +-- src/app/view.ts | 2 ++ src/app/views/schedule-render.test.ts | 35 +++++++++++++++++++ src/app/views/schedule-render.ts | 4 +-- src/app/views/schedule.ts | 4 +++ src/core/theme.ts | 10 +++--- src/features/schedule-render.ts | 49 +++++++++++++++++++++------ 9 files changed, 109 insertions(+), 21 deletions(-) diff --git a/src/app/app.ts b/src/app/app.ts index 0ca2cd9..e27e9e9 100644 --- a/src/app/app.ts +++ b/src/app/app.ts @@ -67,9 +67,13 @@ export async function runApp(): Promise { return { rows: process.stdout.rows || 24, cols: process.stdout.columns || 80 }; } + function wide(): boolean { + return !helpOpen && nativeViews[view]?.wideFrame?.() === true; + } + function size(): AppSize { const terminal = terminalSize(); - return { rows: terminal.rows, cols: frameWidth(terminal.cols) }; + return { rows: terminal.rows, cols: frameWidth(terminal.cols, wide()) }; } const ctx: AppContext = { @@ -119,7 +123,7 @@ export async function runApp(): Promise { chrome.footerLines, helpOpen || active?.scrollsBody?.() === true ? scrollPercent() : undefined, ); - const lines = composeFrameLines(header, body, footer, rows, terminalCols, bodyScroll); + const lines = composeFrameLines(header, body, footer, rows, terminalCols, bodyScroll, wide()); const patch = diffFrame(painted?.cols === terminalCols ? painted.lines : undefined, lines); painted = { cols: terminalCols, lines }; if (patch) process.stdout.write(patch); diff --git a/src/app/frame.test.ts b/src/app/frame.test.ts index fec0851..bbaeb5f 100644 --- a/src/app/frame.test.ts +++ b/src/app/frame.test.ts @@ -1,6 +1,6 @@ import { describe, it, expect } from 'vitest'; import { fitLine, fitBody, composeFrameLines, computeBodyRows, diffFrame } from './frame.js'; -import { visualWidth } from '../core/text.js'; +import { stripAnsi, visualWidth } from '../core/text.js'; import { frameWidth, MAX_FRAME_COLS } from '../core/theme.js'; describe('fitLine', () => { @@ -88,3 +88,14 @@ describe('computeBodyRows', () => { expect(computeBodyRows(4, 3, 2)).toBe(0); }); }); + +describe('composeFrameLines width', () => { + it('keeps 120 columns by default and allows 160 for a wide page', () => { + const body = ['x'.repeat(200)]; + const normal = composeFrameLines([], body, [], 1, 200, 0); + const wide = composeFrameLines([], body, [], 1, 200, 0, true); + expect(stripAnsi(normal[0] ?? '').trim()).toHaveLength(120); + expect(stripAnsi(wide[0] ?? '').trim()).toHaveLength(160); + expect(stripAnsi(wide[0] ?? '').indexOf('x')).toBe(20); + }); +}); diff --git a/src/app/frame.ts b/src/app/frame.ts index 3bc692f..170ba36 100644 --- a/src/app/frame.ts +++ b/src/app/frame.ts @@ -28,9 +28,10 @@ export function composeFrameLines( rows: number, cols: number, scroll: number, + wide = false, ): string[] { - const width = frameWidth(cols); - const left = ' '.repeat(frameLeft(cols)); + const width = frameWidth(cols, wide); + const left = ' '.repeat(frameLeft(cols, wide)); const right = ' '.repeat(cols - width - left.length); const h = header.map((l) => fitLine(l, width)); const f = footer.map((l) => fitLine(l, width)); diff --git a/src/app/view.ts b/src/app/view.ts index d878430..c38e72e 100644 --- a/src/app/view.ts +++ b/src/app/view.ts @@ -30,6 +30,8 @@ export interface View { /** A picture for the space the page's content leaves empty; omitted while the page is busy with a form or a list. */ ambient?(): AmbientSpec | undefined; isBusy?(): boolean; + /** The page is a wide table right now and may use more than the usual 120 columns. */ + wideFrame?(): boolean; /** The body is a plain scrollable document right now, with no cursor of its own. */ scrollsBody?(): boolean; capturesInput?(): boolean; diff --git a/src/app/views/schedule-render.test.ts b/src/app/views/schedule-render.test.ts index c213e0a..f931c34 100644 --- a/src/app/views/schedule-render.test.ts +++ b/src/app/views/schedule-render.test.ts @@ -640,6 +640,41 @@ describe('renderSchedule', () => { } }); + it('widens the full grid past 120 columns so names and dated headers fit', () => { + const lines = renderSchedule( + { + mode: 'week', + key: '2026-3', + weekOne: '2026-09-07', + timetable: { + ...timetable, + meetings: [ + { + sourceId: null, + courseName: 'Distributed Systems Architecture', + teacherNames: [], + location: 'Room 105', + weekday: 1, + startPeriod: 1, + endPeriod: 1, + weeks: [1], + kind: 'regular', + }, + ], + }, + }, + campusDateTime('2026-09-07', '07:00'), + 45, + 160, + ).map((l) => stripAnsi(l)); + const out = lines.join('\n'); + expect(lines.some((line) => /^\s*08:00.08:45 /.test(line))).toBe(true); + expect(out).toContain('Distributed Systems Architecture'); + for (const day of ['Fri 9/11', 'Sun 9/13']) expect(out).toContain(day); + expect(out).not.toMatch(/…|\.\.\./); + expect(lines.every((line) => visualWidth(line) <= 160)).toBe(true); + }); + it('prefers the agenda over a grid that would clip names, until the frame cannot grow', () => { const longName = '习近平新时代中国特色社会主义'; const longNameTimetable: Timetable = { diff --git a/src/app/views/schedule-render.ts b/src/app/views/schedule-render.ts index 20a27ae..5856e96 100644 --- a/src/app/views/schedule-render.ts +++ b/src/app/views/schedule-render.ts @@ -6,7 +6,7 @@ import { type TimetableMeeting, type Weekday, } from '@nbtca/nbtcal/timetable'; -import { c, type, space, glyph, MAX_FRAME_COLS } from '../../core/theme.js'; +import { c, type, space, glyph, MAX_WIDE_FRAME_COLS } from '../../core/theme.js'; import { pickIcon } from '../../core/icons.js'; import { t, fmt } from '../../i18n/index.js'; import { type ListField, renderListFieldWithContext } from '../fields/list-field.js'; @@ -237,7 +237,7 @@ function fittingWeekGrid( cols: number, cursor: GridCursor | undefined, ): string[] | null { - if (cols < Math.min(MAX_FRAME_COLS, weekGridFullWidth(tt, week, now))) return null; + if (cols < Math.min(MAX_WIDE_FRAME_COLS, weekGridFullWidth(tt, week, now, weekOne))) return null; const grid = renderWeekGrid(tt, week, now, cols, cursor, weekOne).split('\n'); return grid.length <= rows ? grid : null; } diff --git a/src/app/views/schedule.ts b/src/app/views/schedule.ts index 84fef13..1d891a4 100644 --- a/src/app/views/schedule.ts +++ b/src/app/views/schedule.ts @@ -527,6 +527,10 @@ export const scheduleView = { return state.mode === 'loading'; }, + wideFrame(): boolean { + return state.mode === 'week' || state.mode === 'hub'; + }, + scrollsBody(): boolean { return state.mode === 'records' && !recordsPanel?.capturesPageKeys(); }, diff --git a/src/core/theme.ts b/src/core/theme.ts index a040dcd..2a4a6cb 100644 --- a/src/core/theme.ts +++ b/src/core/theme.ts @@ -75,17 +75,19 @@ export const glyph = { }; export const MAX_FRAME_COLS = 120; +/** For pages, like the full timetable, whose content is a table rather than reading text. */ +export const MAX_WIDE_FRAME_COLS = 160; export const space = { indent: ' ', } as const; -export function frameWidth(cols: number): number { - return Math.min(cols, MAX_FRAME_COLS); +export function frameWidth(cols: number, wide = false): number { + return Math.min(cols, wide ? MAX_WIDE_FRAME_COLS : MAX_FRAME_COLS); } -export function frameLeft(cols: number): number { - return Math.floor((cols - frameWidth(cols)) / 2); +export function frameLeft(cols: number, wide = false): number { + return Math.floor((cols - frameWidth(cols, wide)) / 2); } const RIGHT_MARGIN_MIN_COLS = 40; diff --git a/src/features/schedule-render.ts b/src/features/schedule-render.ts index ef52fb1..72ccf8f 100644 --- a/src/features/schedule-render.ts +++ b/src/features/schedule-render.ts @@ -9,7 +9,15 @@ import type { import { campusDateTime, campusIsoDate, createTimetableSchedule } from '@nbtca/nbtcal/timetable'; import { countdownParts, isCountdownUrgent, type Countdown } from './calendar-query.js'; import type { CourseProgress } from './course-progress.js'; -import { c, type, space, glyph, MAX_FRAME_COLS, bodyWidth } from '../core/theme.js'; +import { + c, + type, + space, + glyph, + MAX_FRAME_COLS, + MAX_WIDE_FRAME_COLS, + bodyWidth, +} from '../core/theme.js'; import { pickIcon } from '../core/icons.js'; import { padEndV, truncate, visualWidth, wrapAnsiToVisualWidth } from '../core/text.js'; import { addLocalDays, parseLocalMonday } from '../core/calendar-day.js'; @@ -120,9 +128,9 @@ function weekdayName(wd: number, weekday: 'short' | 'long'): string { }).format(Date.UTC(2024, 0, wd)); } -export function lineBudget(cols: number): number { +export function lineBudget(cols: number, maxCols: number = MAX_FRAME_COLS): number { return Number.isFinite(cols) - ? Math.max(1, Math.min(Math.floor(cols), MAX_FRAME_COLS) - visualWidth(space.indent)) + ? Math.max(1, Math.min(Math.floor(cols), maxCols) - visualWidth(space.indent)) : Number.POSITIVE_INFINITY; } @@ -368,10 +376,16 @@ interface GridMetrics { totalIdealColW: number; } -function weekGridMetrics(timetable: Timetable, weekNumber: number, now: Date): GridMetrics { +function weekGridMetrics( + timetable: Timetable, + weekNumber: number, + now: Date, + weekOneMonday?: string, +): GridMetrics { 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 rowHeadW = timetable.periods.reduce((w, p) => Math.max(w, visualWidth(rangeLabel(p.start, p.end))), 0) + GRID_SEP_W; @@ -384,14 +398,20 @@ function weekGridMetrics(timetable: Timetable, weekNumber: number, now: Date): G 0, ); const headerW = - visualWidth(weekdayShortLabel(wd)) + (wd === todayWd ? visualWidth(pickIcon('•', '*')) : 0); + visualWidth(`${weekdayShortLabel(wd)}${shortDate(dates[i])}`) + + (wd === todayWd ? visualWidth(pickIcon('•', '*')) : 0); return Math.max(nameW, locW, headerW, MIN_COL_WIDTH); }); return { rowHeadW, idealColWidths, totalIdealColW: idealColWidths.reduce((a, b) => a + b, 0) }; } -export function weekGridFullWidth(timetable: Timetable, weekNumber: number, now: Date): number { - const { rowHeadW, totalIdealColW } = weekGridMetrics(timetable, weekNumber, now); +export function weekGridFullWidth( + timetable: Timetable, + weekNumber: number, + now: Date, + weekOneMonday?: string, +): number { + const { rowHeadW, totalIdealColW } = weekGridMetrics(timetable, weekNumber, now, weekOneMonday); return ( visualWidth(space.indent) * 2 + rowHeadW + totalIdealColW + 6 * GRID_SEP_W + GRID_CURSOR_TAIL_W ); @@ -416,10 +436,19 @@ export function renderWeekGrid( const sepGlyph = pickIcon('│', '|'); const gap = ` ${sepGlyph} `; const sep = type.muted(gap); - const { rowHeadW, idealColWidths, totalIdealColW } = weekGridMetrics(timetable, weekNumber, now); + const { rowHeadW, idealColWidths, totalIdealColW } = weekGridMetrics( + timetable, + weekNumber, + now, + weekOneMonday, + ); const availableForCols = Math.max( 0, - lineBudget(cols) - visualWidth(space.indent) - rowHeadW - 6 * GRID_SEP_W - GRID_CURSOR_TAIL_W, + lineBudget(cols, MAX_WIDE_FRAME_COLS) - + visualWidth(space.indent) - + rowHeadW - + 6 * GRID_SEP_W - + GRID_CURSOR_TAIL_W, ); const colWidths = totalIdealColW <= availableForCols