Compare commits

...
5 changed files with 119 additions and 22 deletions
+1 -1
View File
@@ -1 +1 @@
sha256-nWaU16CvKjT2bhQ/Q0gxZOv2NGxODBwn7lZ/EQgE+u0=
sha256-+JWe/7lan6F71xfaMMDMAKU0/qW84gaXvGxm+MRKWrk=
+56
View File
@@ -22440,6 +22440,18 @@
"node": "6.* || 8.* || >= 10.*"
}
},
"node_modules/get-east-asian-width": {
"version": "1.7.0",
"resolved": "https://registry.npmjs.org/get-east-asian-width/-/get-east-asian-width-1.7.0.tgz",
"integrity": "sha512-XjH1AECxf0giL2V1aU8vKyRR2ppRUb5c0EvT7zuJTokQ74bNo52zOtghqdWIqrhUD79fo3x0WfKZdOqxF6LG1Q==",
"license": "MIT",
"engines": {
"node": ">=18"
},
"funding": {
"url": "https://github.com/sponsors/sindresorhus"
}
},
"node_modules/get-intrinsic": {
"version": "1.3.0",
"resolved": "https://registry.npmjs.org/get-intrinsic/-/get-intrinsic-1.3.0.tgz",
@@ -39823,6 +39835,7 @@
"chalk": "5.6.2",
"commander": "12.1.0",
"mime-types": "2.1.35",
"string-width": "9.0.0",
"tree-kill": "1.2.2",
"ws": "8.20.0",
"yaml": "2.8.4",
@@ -39929,6 +39942,18 @@
"url": "https://opencollective.com/vitest"
}
},
"packages/cli/node_modules/ansi-regex": {
"version": "6.6.0",
"resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-6.6.0.tgz",
"integrity": "sha512-XZ5apbzreCbXSKwPjJstu8R0zOi247w3XYwWxtwBXgpZrCyc1Ka7p9/xY0A9/diMewmHbEKz+FIraWaEWjlN5g==",
"license": "MIT",
"engines": {
"node": ">=12"
},
"funding": {
"url": "https://github.com/chalk/ansi-regex?sponsor=1"
}
},
"packages/cli/node_modules/chalk": {
"version": "5.6.2",
"resolved": "https://registry.npmjs.org/chalk/-/chalk-5.6.2.tgz",
@@ -39950,6 +39975,37 @@
"node": ">=18"
}
},
"packages/cli/node_modules/string-width": {
"version": "9.0.0",
"resolved": "https://registry.npmjs.org/string-width/-/string-width-9.0.0.tgz",
"integrity": "sha512-2MOboAIRIszVGZVPA2Zqwj0ujd0o+xAZ5y2ft4OcD4kuVQ4phZxq0Sv7YTY8yaDA2CSMXch1EWTkJw+tTJZTzA==",
"license": "MIT",
"dependencies": {
"get-east-asian-width": "^1.7.0",
"strip-ansi": "^7.2.0"
},
"engines": {
"node": ">=22"
},
"funding": {
"url": "https://github.com/sponsors/sindresorhus"
}
},
"packages/cli/node_modules/strip-ansi": {
"version": "7.2.0",
"resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-7.2.0.tgz",
"integrity": "sha512-yDPMNjp4WyfYBkHnjIRLfca1i6KMyGCtsVgoKe/z1+6vukgaENdgGBZt+ZmKPc4gavvEZ5OgHfHdrazhgNyG7w==",
"license": "MIT",
"dependencies": {
"ansi-regex": "^6.2.2"
},
"engines": {
"node": ">=12"
},
"funding": {
"url": "https://github.com/chalk/strip-ansi?sponsor=1"
}
},
"packages/cli/node_modules/vitest": {
"version": "4.1.6",
"resolved": "https://registry.npmjs.org/vitest/-/vitest-4.1.6.tgz",
+1
View File
@@ -40,6 +40,7 @@
"chalk": "5.6.2",
"commander": "12.1.0",
"mime-types": "2.1.35",
"string-width": "9.0.0",
"tree-kill": "1.2.2",
"ws": "8.20.0",
"yaml": "2.8.4",
+55 -1
View File
@@ -1,5 +1,5 @@
import { expect, it } from "vitest";
import { renderError, toCommandError } from "./render.js";
import { render, renderError, toCommandError } from "./render.js";
it("preserves the message of an Error with an RPC code in JSON output", () => {
const error = Object.assign(new Error("Plugin requires Paseo >=0.8.0. Your daemon is 0.7.2."), {
@@ -14,3 +14,57 @@ it("preserves the message of an Error with an RPC code in JSON output", () => {
},
});
});
it("aligns columns by terminal cells for wide and combining characters", () => {
const output = render(
{
type: "list",
data: [{ name: "开发" }, { name: "e\u0301" }, { name: "ok" }],
schema: {
idField: "name",
columns: [
{ header: "名", field: "name" },
{ header: "ID", field: () => "42" },
],
},
},
{ format: "table", noColor: true },
);
expect(output.split("\n")).toEqual(["名 ID", "开发 42", "e\u0301 42", "ok 42"]);
});
it("aligns columns after Arabic combining marks and newer emoji", () => {
const output = render(
{
type: "list",
data: [{ name: "مُحَمَّد" }, { name: "🫠" }],
schema: {
idField: "name",
columns: [
{ header: "Name", field: "name" },
{ header: "ID", field: () => "42" },
],
},
},
{ format: "table", noColor: true },
);
expect(output.split("\n")).toEqual(["Name ID", "مُحَمَّد 42", "🫠 42"]);
});
it("distinguishes text symbols from emoji and measures less common wide characters", () => {
const output = render(
{
type: "list",
data: [{ name: "©" }, { name: "𖿠" }, { name: "ok" }],
schema: {
idField: "name",
columns: [
{ header: "Name", field: "name" },
{ header: "ID", field: () => "42" },
],
},
},
{ format: "table", noColor: true },
);
expect(output.split("\n")).toEqual(["Name ID", "© 42", "𖿠 42", "ok 42"]);
});
+6 -20
View File
@@ -5,26 +5,12 @@
*/
import chalk, { type ChalkInstance } from "chalk";
import stringWidth from "string-width";
import type { AnyCommandResult, ColumnDef, OutputOptions, OutputSchema } from "./types.js";
// ANSI escape code regex for stripping colors when measuring width
const ANSI_REGEX =
// eslint-disable-next-line no-control-regex
/[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g;
/** Strip ANSI escape codes from a string */
function stripAnsi(str: string): string {
return str.replace(ANSI_REGEX, "");
}
/** Get visible string length (excluding ANSI codes) */
function visibleLength(str: string): number {
return stripAnsi(str).length;
}
/** Pad a cell to the specified width with alignment */
function padCell(cell: string, width: number, align: "left" | "right" | "center"): string {
const visible = visibleLength(cell);
const visible = stringWidth(cell);
const padding = Math.max(0, width - visible);
switch (align) {
@@ -110,13 +96,13 @@ function renderHeader<T>(
function calculateWidths<T>(data: T[], columns: ColumnDef<T>[], includeHeaders: boolean): number[] {
return columns.map((col) => {
// Start with header width if including headers
let maxWidth = includeHeaders ? col.header.length : 0;
let maxWidth = includeHeaders ? stringWidth(col.header) : 0;
// Check all data values
for (const item of data) {
const value = getValue(item, col.field);
const str = String(value ?? "");
maxWidth = Math.max(maxWidth, visibleLength(str));
maxWidth = Math.max(maxWidth, stringWidth(str));
}
// Apply width hint if specified (minimum width)
@@ -163,7 +149,7 @@ export function renderTableHeader<T>(
widths?: number[],
): string {
const columns = schema.columns;
const actualWidths = widths ?? columns.map((col) => col.width ?? col.header.length);
const actualWidths = widths ?? columns.map((col) => col.width ?? stringWidth(col.header));
return renderHeader(columns, actualWidths, options);
}
@@ -175,6 +161,6 @@ export function renderTableRow<T>(
widths?: number[],
): string {
const columns = schema.columns;
const actualWidths = widths ?? columns.map((col) => col.width ?? col.header.length);
const actualWidths = widths ?? columns.map((col) => col.width ?? stringWidth(col.header));
return renderRow(item, columns, actualWidths, options);
}