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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ All notable changes to `sustech-cli` are documented in this file.

### Fixed

- Corrected terminal-table alignment for Unicode Roman numerals, ellipses,
and combining characters, and kept grapheme clusters intact during truncation.
- Made `auth status` use a metadata-only macOS Keychain lookup instead of
reading the stored password, and bounded credential-helper subprocesses to
five seconds with a structured `CREDENTIAL_STORE_TIMEOUT` status.
Expand Down
63 changes: 63 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
"@modelcontextprotocol/server": "^2.0.0",
"@napi-rs/keyring": "1.3.0",
"playwright-core": "^1.62.1",
"string-width": "^7.2.0",
"zod": "^4.5.2"
}
}
8 changes: 6 additions & 2 deletions src/core/text.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import stringWidth from "string-width";
import type { Semester } from "./semester.js";
import type { StudentProfileReport } from "../profile/report.js";
import type { TimetableResult } from "../tis/planner.js";
Expand Down Expand Up @@ -581,16 +582,19 @@ function padCell(value: string, width: number): string {
return `${truncated}${" ".repeat(Math.max(0, width - displayWidth(truncated)))}`;
}

const graphemes = new Intl.Segmenter("en", { granularity: "grapheme" });

function truncateDisplay(value: string, width: number): string {
if (displayWidth(value) <= width) return value;
let output = "";
for (const character of value) {
for (const { segment: character } of graphemes.segment(value)) {
if (displayWidth(`${output}${character}…`) > width) break;
output += character;
}
return `${output}…`;
}

function displayWidth(value: string): number {
return [...value].reduce((width, character) => width + (/[^\u0000-\u00ff]/.test(character) ? 2 : 1), 0);
// Terminals normally render ambiguous-width characters such as Ⅴ and … in one cell.
return stringWidth(value, { ambiguousIsNarrow: true });
}
42 changes: 42 additions & 0 deletions src/test/text.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import assert from "node:assert/strict";
import test from "node:test";
import { formatGrades } from "../core/text.js";

function gradeRow(name: string): string {
const output = formatGrades([{
code: "TEST101",
name,
nameEn: "",
semester: "2026秋季",
credits: 3,
letterGrade: "B",
numericScore: 80,
nature: "",
department: "",
}], { gpa: 0, credits: 0, courseCount: 0 });
return output.split("\n").find((line) => line.includes("TEST101"))!;
}

// Expected padding uses known terminal-cell widths, independent of the renderer.
function expectedRow(name: string, nameWidth: number): string {
return `2026秋季${" ".repeat(8)}TEST101${" ".repeat(7)}${name}${" ".repeat(32 - nameWidth)}B${" ".repeat(8)}80${" ".repeat(7)}3${" ".repeat(7)}`;
}

test("grade columns align for ASCII and Unicode Roman numerals", () => {
assert.equal(gradeRow("体育V"), expectedRow("体育V", 5));
assert.equal(gradeRow("体育Ⅴ"), expectedRow("体育Ⅴ", 5));
assert.equal(gradeRow("体育Ⅳ"), expectedRow("体育Ⅳ", 5));
});

test("truncated grade names reserve one cell for the ellipsis", () => {
assert.equal(gradeRow("中".repeat(16)), expectedRow(`${"中".repeat(14)}…`, 29));
assert.equal(gradeRow("A".repeat(31)), expectedRow(`${"A".repeat(29)}…`, 30));
assert.equal(gradeRow("中".repeat(15)), expectedRow("中".repeat(15), 30));
});

test("grade names preserve combining and emoji graphemes when truncating", () => {
assert.equal(gradeRow("Cafe\u0301"), expectedRow("Cafe\u0301", 4));
assert.equal(gradeRow("AB"), expectedRow("AB", 4));
assert.equal(gradeRow(`${"A".repeat(28)}👩‍💻BC`), expectedRow(`${"A".repeat(28)}…`, 29));
assert.equal(gradeRow(`${"A".repeat(27)}👩‍💻BC`), expectedRow(`${"A".repeat(27)}👩‍💻…`, 30));
});