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
22 changes: 22 additions & 0 deletions src/tab-label.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* Visible label used on editor tabs for a session. Tabs that carry the full
* session title grow without bound, pushing sibling tabs out of view, so the
* displayed label is capped deterministically. The cap is applied in exactly
* the same way for every tab state so activation never changes the length.
*/
export const DEFAULT_TAB_LABEL_MAX = 28;

function sliceCodePoints(value: string, maxLength: number): string {
const codePoints = Array.from(value);
return codePoints.slice(0, maxLength).join("");
}

/** Collapse whitespace, cap by code points, and add an ellipsis when cut. */
export function limitTabLabel(
label: string,
maxLength: number = DEFAULT_TAB_LABEL_MAX,
): string {
const compact = label.replace(/\s+/g, " ").trim();
if (Array.from(compact).length <= maxLength) { return compact; }
return `${sliceCodePoints(compact, maxLength).replace(/[\s.…]+$/u, "")}…`;
}
49 changes: 49 additions & 0 deletions src/test/tab-label.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import * as assert from "node:assert";
import { readFileSync } from "node:fs";
import { limitTabLabel } from "../tab-label.js";

suite("Session tab label limits", () => {
test("keeps short labels unchanged", () => {
assert.strictEqual(limitTabLabel("Fix sidebar refresh"), "Fix sidebar refresh");
assert.strictEqual(limitTabLabel("Pi"), "Pi");
assert.strictEqual(limitTabLabel(""), "");
});

test("truncates long labels to the configured maximum with an ellipsis", () => {
const label = "a very long session title that would push sibling tabs out of view";
const limited = limitTabLabel(label, 28);
assert.strictEqual(Array.from(limited).length, 29);
assert.ok(limited.endsWith("…"));
assert.strictEqual(Array.from(limited.slice(0, -1)).length, 28);
});

test("counts code points instead of UTF-16 code units", () => {
const exactLengthLabel = "🤖".repeat(28);
assert.strictEqual(limitTabLabel(exactLengthLabel, 28), exactLengthLabel);

const label = "🤖".repeat(40) + " title";
const limited = limitTabLabel(label, 28);
assert.strictEqual(Array.from(limited).length, 29);
assert.ok(!limited.includes("\uFFFD"), "must not split surrogate pairs");
});

test("collapses whitespace before applying the cap", () => {
const label = "标题 很 长 占用 空间 ".repeat(4) + "标题栏";
const limited = limitTabLabel(label, 28);
assert.ok(!limited.includes(" "), "consecutive spaces must be collapsed");
assert.ok(limited.endsWith("…"));
});

test("caps every tab state through the shared title builder", () => {
const source = readFileSync(
new URL("../../src/webview-panel.ts", import.meta.url),
"utf8",
);
assert.match(source, /const label = limitTabLabel\(this\._tabSummary \?\? "Pi"\);/);
assert.match(
source,
/this\.panel\.title = \(this\._tabStreaming \? "\\u25CF " : "\\u25CB "\) \+ label;/,
);
assert.match(source, /import \{ limitTabLabel \} from "\.\/tab-label\.js";/);
});
});
6 changes: 4 additions & 2 deletions src/webview-panel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { mergeInitialHistoryEvents } from "./history-event-sync.js";
import { SessionCapabilitySnapshot } from "./capability-snapshot.js";
import { piError } from "./logger.js";
import { getWorkspaceCwd } from "./workspace-context.js";
import { limitTabLabel } from "./tab-label.js";
import type { PiService } from "./pi-service.js";
import type { PiServiceEvent } from "./types.js";
import {
Expand Down Expand Up @@ -860,8 +861,9 @@ export class PiWebviewPanel {
return;
}

const label = this._tabSummary ?? "Pi";
// Bullet prefix: ● busy, ○ idle — consistent with status bar
const label = limitTabLabel(this._tabSummary ?? "Pi");
// Bullet prefix: ● busy, ○ idle — consistent with status bar. The cap is
// applied before the prefix so streaming and idle tabs keep equal length.
this.panel.title = (this._tabStreaming ? "\u25CF " : "\u25CB ") + label;
}

Expand Down
Loading