ui: share truncate and firstLine from text.ts

This commit is contained in:
Callum Alpass committed 2026-11-02 22:29:31 +11:00
1 parent 12e1e0ba7a
commit a6cfb9a2cd
8 files changed
+47 -30

No files matched your search

+1 -5
View File
@@ -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
</box>
);
}
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;
}
+1 -5
View File
@@ -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;
+1 -5
View File
@@ -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();
+1 -5
View File
@@ -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 }) {
</text>
);
}
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;
}
+1 -5
View File
@@ -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;
}
+13
View File
@@ -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)}…`;
}
+1 -5
View File
@@ -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.