From a6cfb9a2cdc00de4118ffe5854fce4b846dd8515 Mon Sep 17 00:00:00 2001 From: Callum Alpass Date: Mon, 2 Nov 2026 22:29:31 +1100 Subject: [PATCH] ui: share truncate and firstLine from text.ts --- src/ui/ComposerMenu.tsx | 6 +----- src/ui/DetailsPanel.tsx | 6 +----- src/ui/QueuePanel.tsx | 6 +----- src/ui/ThreadList.tsx | 6 +----- src/ui/ThreadView.tsx | 6 +----- src/ui/text.ts | 13 +++++++++++++ src/ui/usePullRequests.ts | 6 +----- test/text.test.ts | 28 ++++++++++++++++++++++++++++ 8 files changed, 47 insertions(+), 30 deletions(-) create mode 100644 src/ui/text.ts create mode 100644 test/text.test.ts diff --git a/src/ui/ComposerMenu.tsx b/src/ui/ComposerMenu.tsx index 23ff9dc..cd7b288 100644 --- a/src/ui/ComposerMenu.tsx +++ b/src/ui/ComposerMenu.tsx @@ -1,5 +1,6 @@ import type { ComposerItem } from "../model/composerMenu.ts"; import { ClickRow } from "./Button.tsx"; +import { truncate } from "./text.ts"; import { colors } from "./theme.ts"; const MAX_ROWS = 8; @@ -34,8 +35,3 @@ export function ComposerMenu(props: { readonly items: readonly ComposerItem[]; r ); } - -function truncate(text: string, width: number): string { - const single = text.replace(/\s+/g, " "); - return single.length > width ? `${single.slice(0, Math.max(0, width - 1))}…` : single; -} diff --git a/src/ui/DetailsPanel.tsx b/src/ui/DetailsPanel.tsx index 94cfe0a..e002414 100644 --- a/src/ui/DetailsPanel.tsx +++ b/src/ui/DetailsPanel.tsx @@ -2,6 +2,7 @@ import type { ReactNode } from "react"; import { MAX_DETAIL_FILES, type DetailsAction, type ThreadDetails } from "../model/details.ts"; import { ClickRow } from "./Button.tsx"; +import { truncate } from "./text.ts"; import { colors, statusColor } from "./theme.ts"; /** Rows stay readable when the panel takes the conversation's place on a narrow terminal. */ @@ -214,11 +215,6 @@ function countsText(insertions: number, deletions: number): string { return [insertions > 0 ? `+${insertions}` : "", deletions > 0 ? `−${deletions}` : ""].filter(Boolean).join(" "); } -function truncate(text: string, width: number): string { - const single = text.replace(/\s+/g, " "); - return single.length > width ? `${single.slice(0, Math.max(0, width - 1))}…` : single; -} - /** Keeps the end of a path, where the file name is, as the web's StartTruncatedPath does. */ function startTruncate(text: string, width: number): string { return text.length > width ? `…${text.slice(text.length - width + 1)}` : text; diff --git a/src/ui/QueuePanel.tsx b/src/ui/QueuePanel.tsx index f717805..f2690d0 100644 --- a/src/ui/QueuePanel.tsx +++ b/src/ui/QueuePanel.tsx @@ -2,6 +2,7 @@ import type { MouseEvent } from "@opentui/core"; import type { QueuedMessage } from "../model/queue.ts"; import { Button, Buttons, isLeftClick, useHover } from "./Button.tsx"; +import { truncate } from "./text.ts"; import { colors } from "./theme.ts"; export interface QueueActions { @@ -42,11 +43,6 @@ export function QueuePanel(props: { readonly queued: readonly QueuedMessage[]; r ); } -function truncate(text: string, width: number): string { - const single = text.replace(/\s+/g, " ").trim(); - return single.length > width ? `${single.slice(0, Math.max(0, width - 1))}…` : single; -} - function QueueRow(props: { readonly text: string; readonly width: number; readonly focused: boolean; readonly editing: boolean; readonly index: number; readonly actions: QueueActions }) { const { index, actions } = props; const [hover, hoverProps] = useHover(); diff --git a/src/ui/ThreadList.tsx b/src/ui/ThreadList.tsx index 33f1951..c4181e8 100644 --- a/src/ui/ThreadList.tsx +++ b/src/ui/ThreadList.tsx @@ -4,6 +4,7 @@ import type { ThreadId } from "@t3tools/contracts"; import { useEffect, useState } from "react"; import { ClickRow, wheelStep } from "./Button.tsx"; +import { truncate } from "./text.ts"; import { colors, statusColor } from "./theme.ts"; /** @@ -125,8 +126,3 @@ function SectionHeader({ label }: { readonly label: string }) { ); } - -function truncate(text: string, width: number): string { - const single = text.replace(/\s+/g, " "); - return single.length > width ? `${single.slice(0, Math.max(0, width - 1))}…` : single; -} diff --git a/src/ui/ThreadView.tsx b/src/ui/ThreadView.tsx index ca0a727..0043c24 100644 --- a/src/ui/ThreadView.tsx +++ b/src/ui/ThreadView.tsx @@ -13,6 +13,7 @@ import { CommandOutput } from "./CommandOutput.tsx"; import type { T3Session, ThreadHistory } from "../client/session.ts"; import { liveWorkEntryLabel, workEntryDisplayLabel, type MessagesTimelineRow, type Timeline, type WorkLogEntry } from "../model/timeline.ts"; import { blockFor } from "../model/transcript.ts"; +import { firstLine } from "./text.ts"; import { colors, getMarkdownStyle, toneColor } from "./theme.ts"; /** What a right-click in the conversation was on. */ @@ -337,8 +338,3 @@ function glyphFor(entry: WorkLogEntry): string { if (entry.itemType === "todo_list") return "☐"; return "·"; } - -function firstLine(text: string): string { - const line = (text.trim().split("\n")[0] ?? "").replace(/\s+/g, " "); - return line.length > 200 ? `${line.slice(0, 197)}…` : line; -} diff --git a/src/ui/text.ts b/src/ui/text.ts new file mode 100644 index 0000000..efcb66c --- /dev/null +++ b/src/ui/text.ts @@ -0,0 +1,13 @@ +/** Collapses text to one line and clips it to `width`, so a label never wraps its row. */ +export function truncate(text: string, width: number): string { + const single = text.replace(/\s+/g, " "); + if (single.length <= width) return single; + return `${single.slice(0, Math.max(0, width - 1))}…`; +} + +/** Takes the first line only, so multi-line text can stand in for a short label. */ +export function firstLine(text: string): string { + const line = (text.trim().split("\n")[0] ?? "").replace(/\s+/g, " "); + if (line.length <= 100) return line; + return `${line.slice(0, 99)}…`; +} diff --git a/src/ui/usePullRequests.ts b/src/ui/usePullRequests.ts index 90d70ae..b07cafe 100644 --- a/src/ui/usePullRequests.ts +++ b/src/ui/usePullRequests.ts @@ -38,6 +38,7 @@ import { } from "../model/pullRequests.ts"; import type { Confirmation } from "./ConfirmBar.tsx"; import { editInEditor } from "./editor.ts"; +import { firstLine } from "./text.ts"; import type { PullRequestViewState } from "./PullRequestView.tsx"; export interface PullRequestDeps { @@ -473,11 +474,6 @@ function preferredMergeMethod(config: ServerConfig, projectId: ProjectId) { const messageOf = (error: unknown) => (error instanceof Error ? error.message : String(error)); -function firstLine(text: string): string { - const line = (text.trim().split("\n")[0] ?? "").replace(/\s+/g, " "); - return line.length > 100 ? `${line.slice(0, 99)}…` : line; -} - /** * The composer's `#` search, debounced as the web's (180ms): the recent list (host-searched for * words), plus the exact pull request a typed number names when the list lacks it. diff --git a/test/text.test.ts b/test/text.test.ts new file mode 100644 index 0000000..1ee69da --- /dev/null +++ b/test/text.test.ts @@ -0,0 +1,28 @@ +import { describe, expect, test } from "bun:test"; + +import { firstLine, truncate } from "../src/ui/text.ts"; + +describe("truncate", () => { + test("collapses whitespace and clips with an ellipsis", () => { + expect(truncate("a b\nc", 10)).toBe("a b c"); + expect(truncate("abcdef", 4)).toBe("abc…"); + expect(truncate("abcdef", 0)).toBe("…"); + }); + + test("keeps leading whitespace collapsed to one space", () => { + // The majority of the old copies did not trim; only QueuePanel's did. + expect(truncate(" hi", 10)).toBe(" hi"); + }); +}); + +describe("firstLine", () => { + test("takes the first trimmed line, collapsed", () => { + expect(firstLine(" hello world\nsecond line")).toBe("hello world"); + expect(firstLine("")).toBe(""); + }); + + test("caps at 100 characters", () => { + const long = "x".repeat(150); + expect(firstLine(long)).toBe(`${"x".repeat(99)}…`); + }); +});