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
8 changes: 6 additions & 2 deletions src/app/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,13 @@ export async function runApp(): Promise<void> {
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 = {
Expand Down Expand Up @@ -119,7 +123,7 @@ export async function runApp(): Promise<void> {
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);
Expand Down
13 changes: 12 additions & 1 deletion src/app/frame.test.ts
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand Down Expand Up @@ -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);
});
});
5 changes: 3 additions & 2 deletions src/app/frame.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
2 changes: 2 additions & 0 deletions src/app/view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
35 changes: 35 additions & 0 deletions src/app/views/schedule-render.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
4 changes: 2 additions & 2 deletions src/app/views/schedule-render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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;
}
Expand Down
4 changes: 4 additions & 0 deletions src/app/views/schedule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
},
Expand Down
10 changes: 6 additions & 4 deletions src/core/theme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
49 changes: 39 additions & 10 deletions src/features/schedule-render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
Expand All @@ -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
);
Expand All @@ -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
Expand Down