ui: share truncate and firstLine from text.ts
This commit is contained in:
1 parent
12e1e0ba7a
commit
a6cfb9a2cd
8 files changed
+47
-30
No files matched your search
@@ -1,5 +1,6 @@
|
|||||||
import type { ComposerItem } from "../model/composerMenu.ts";
|
import type { ComposerItem } from "../model/composerMenu.ts";
|
||||||
import { ClickRow } from "./Button.tsx";
|
import { ClickRow } from "./Button.tsx";
|
||||||
|
import { truncate } from "./text.ts";
|
||||||
import { colors } from "./theme.ts";
|
import { colors } from "./theme.ts";
|
||||||
|
|
||||||
const MAX_ROWS = 8;
|
const MAX_ROWS = 8;
|
||||||
@@ -34,8 +35,3 @@ export function ComposerMenu(props: { readonly items: readonly ComposerItem[]; r
|
|||||||
</box>
|
</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;
|
|
||||||
}
|
|
||||||
@@ -2,6 +2,7 @@ import type { ReactNode } from "react";
|
|||||||
|
|
||||||
import { MAX_DETAIL_FILES, type DetailsAction, type ThreadDetails } from "../model/details.ts";
|
import { MAX_DETAIL_FILES, type DetailsAction, type ThreadDetails } from "../model/details.ts";
|
||||||
import { ClickRow } from "./Button.tsx";
|
import { ClickRow } from "./Button.tsx";
|
||||||
|
import { truncate } from "./text.ts";
|
||||||
import { colors, statusColor } from "./theme.ts";
|
import { colors, statusColor } from "./theme.ts";
|
||||||
|
|
||||||
/** Rows stay readable when the panel takes the conversation's place on a narrow terminal. */
|
/** 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(" ");
|
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. */
|
/** Keeps the end of a path, where the file name is, as the web's StartTruncatedPath does. */
|
||||||
function startTruncate(text: string, width: number): string {
|
function startTruncate(text: string, width: number): string {
|
||||||
return text.length > width ? `…${text.slice(text.length - width + 1)}` : text;
|
return text.length > width ? `…${text.slice(text.length - width + 1)}` : text;
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ import type { MouseEvent } from "@opentui/core";
|
|||||||
|
|
||||||
import type { QueuedMessage } from "../model/queue.ts";
|
import type { QueuedMessage } from "../model/queue.ts";
|
||||||
import { Button, Buttons, isLeftClick, useHover } from "./Button.tsx";
|
import { Button, Buttons, isLeftClick, useHover } from "./Button.tsx";
|
||||||
|
import { truncate } from "./text.ts";
|
||||||
import { colors } from "./theme.ts";
|
import { colors } from "./theme.ts";
|
||||||
|
|
||||||
export interface QueueActions {
|
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 }) {
|
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 { index, actions } = props;
|
||||||
const [hover, hoverProps] = useHover();
|
const [hover, hoverProps] = useHover();
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import type { ThreadId } from "@t3tools/contracts";
|
|||||||
import { useEffect, useState } from "react";
|
import { useEffect, useState } from "react";
|
||||||
|
|
||||||
import { ClickRow, wheelStep } from "./Button.tsx";
|
import { ClickRow, wheelStep } from "./Button.tsx";
|
||||||
|
import { truncate } from "./text.ts";
|
||||||
import { colors, statusColor } from "./theme.ts";
|
import { colors, statusColor } from "./theme.ts";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -125,8 +126,3 @@ function SectionHeader({ label }: { readonly label: string }) {
|
|||||||
</text>
|
</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;
|
|
||||||
}
|
|
||||||
@@ -13,6 +13,7 @@ import { CommandOutput } from "./CommandOutput.tsx";
|
|||||||
import type { T3Session, ThreadHistory } from "../client/session.ts";
|
import type { T3Session, ThreadHistory } from "../client/session.ts";
|
||||||
import { liveWorkEntryLabel, workEntryDisplayLabel, type MessagesTimelineRow, type Timeline, type WorkLogEntry } from "../model/timeline.ts";
|
import { liveWorkEntryLabel, workEntryDisplayLabel, type MessagesTimelineRow, type Timeline, type WorkLogEntry } from "../model/timeline.ts";
|
||||||
import { blockFor } from "../model/transcript.ts";
|
import { blockFor } from "../model/transcript.ts";
|
||||||
|
import { firstLine } from "./text.ts";
|
||||||
import { colors, getMarkdownStyle, toneColor } from "./theme.ts";
|
import { colors, getMarkdownStyle, toneColor } from "./theme.ts";
|
||||||
|
|
||||||
/** What a right-click in the conversation was on. */
|
/** What a right-click in the conversation was on. */
|
||||||
@@ -337,8 +338,3 @@ function glyphFor(entry: WorkLogEntry): string {
|
|||||||
if (entry.itemType === "todo_list") return "☐";
|
if (entry.itemType === "todo_list") return "☐";
|
||||||
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;
|
|
||||||
}
|
|
||||||
@@ -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)}…`;
|
||||||
|
}
|
||||||
@@ -38,6 +38,7 @@ import {
|
|||||||
} from "../model/pullRequests.ts";
|
} from "../model/pullRequests.ts";
|
||||||
import type { Confirmation } from "./ConfirmBar.tsx";
|
import type { Confirmation } from "./ConfirmBar.tsx";
|
||||||
import { editInEditor } from "./editor.ts";
|
import { editInEditor } from "./editor.ts";
|
||||||
|
import { firstLine } from "./text.ts";
|
||||||
import type { PullRequestViewState } from "./PullRequestView.tsx";
|
import type { PullRequestViewState } from "./PullRequestView.tsx";
|
||||||
|
|
||||||
export interface PullRequestDeps {
|
export interface PullRequestDeps {
|
||||||
@@ -473,11 +474,6 @@ function preferredMergeMethod(config: ServerConfig, projectId: ProjectId) {
|
|||||||
|
|
||||||
const messageOf = (error: unknown) => (error instanceof Error ? error.message : String(error));
|
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
|
* 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.
|
* words), plus the exact pull request a typed number names when the list lacks it.
|
||||||
|
|||||||
@@ -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)}…`);
|
||||||
|
});
|
||||||
|
});
|
||||||
Reference in new issue
Block a user