Skip to content

Commit eed711b

Browse files
committed
fix: clamp branch preview width
1 parent 951ed71 commit eed711b

File tree

1 file changed

+30
-3
lines changed

1 file changed

+30
-3
lines changed

cli/src/components/branch-item.tsx

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { TextAttributes, type BorderCharacters } from '@opentui/core'
2+
import stringWidth from 'string-width'
23
import React, { type ReactNode } from 'react'
34

45
const containerBorderChars: BorderCharacters = {
@@ -48,6 +49,7 @@ export const BranchItem = ({
4849
statusColor,
4950
statusIndicator = '●',
5051
theme,
52+
availableWidth,
5153
onToggle,
5254
}: BranchItemProps) => {
5355
const resolveFg = (
@@ -90,9 +92,34 @@ export const BranchItem = ({
9092
? `${statusLabel} ${statusIndicator}`
9193
: `${statusIndicator} ${statusLabel}`
9294
: null
95+
const normalizePreview = (value: string): string =>
96+
value.replace(/\s+/g, ' ').trim()
97+
const ellipsisChar = '…'
98+
const ellipsisWidth = stringWidth(ellipsisChar)
99+
const maxPreviewWidth = Math.max(1, availableWidth - 4)
100+
const clampPreview = (value: string): string => {
101+
const normalized = normalizePreview(value)
102+
if (!normalized) return ''
103+
if (stringWidth(normalized) <= maxPreviewWidth) {
104+
return normalized
105+
}
106+
if (maxPreviewWidth <= ellipsisWidth) {
107+
return ellipsisChar
108+
}
109+
let truncated = ''
110+
for (const char of normalized) {
111+
if (stringWidth(truncated + char) > maxPreviewWidth - ellipsisWidth) {
112+
break
113+
}
114+
truncated += char
115+
}
116+
return `${truncated.trimEnd()}${ellipsisChar}`
117+
}
118+
const collapsedPreviewText = clampPreview(
119+
isStreaming ? streamingPreview : finishedPreview,
120+
)
93121
const showCollapsedPreview =
94-
(isStreaming && !!streamingPreview) ||
95-
(!isStreaming && !!finishedPreview)
122+
isCollapsed && collapsedPreviewText.length > 0
96123

97124
const isTextRenderable = (value: ReactNode): boolean => {
98125
if (value === null || value === undefined || typeof value === 'boolean') {
@@ -282,7 +309,7 @@ export const BranchItem = ({
282309
)}
283310
attributes={getAttributes(TextAttributes.ITALIC)}
284311
>
285-
{isStreaming ? streamingPreview : finishedPreview}
312+
{collapsedPreviewText}
286313
</text>
287314
</box>
288315
) : null

0 commit comments

Comments
 (0)