From 2b44dbdbf105f1c5d1cd34b7ae86925ff41e4c79 Mon Sep 17 00:00:00 2001 From: Tom Date: Sat, 19 Jul 2025 21:28:40 +0700 Subject: fix: prevent sparse spacing in hyphenated words (#1102) --- packages/tui/internal/util/file.go | 12 +++++++--- packages/tui/internal/util/text.go | 47 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 3 deletions(-) create mode 100644 packages/tui/internal/util/text.go (limited to 'packages/tui/internal/util') diff --git a/packages/tui/internal/util/file.go b/packages/tui/internal/util/file.go index b079f24cd..841f2b519 100644 --- a/packages/tui/internal/util/file.go +++ b/packages/tui/internal/util/file.go @@ -83,11 +83,17 @@ func Extension(path string) string { } func ToMarkdown(content string, width int, backgroundColor compat.AdaptiveColor) string { - r := styles.GetMarkdownRenderer(width-6, backgroundColor) + renderWidth := width - GetMarkdownContainerFrame() + r := styles.GetMarkdownRenderer(renderWidth, backgroundColor) content = strings.ReplaceAll(content, RootPath+"/", "") - rendered, _ := r.Render(content) - lines := strings.Split(rendered, "\n") + // Apply hyphen preservation during markdown rendering + rendered := ProcessTextWithHyphens(content, func(t string) string { + result, _ := r.Render(t) + return result + }) + lines := strings.Split(rendered, "\n") + // Clean up empty lines at start/end if len(lines) > 0 { firstLine := lines[0] cleaned := ansi.Strip(firstLine) diff --git a/packages/tui/internal/util/text.go b/packages/tui/internal/util/text.go new file mode 100644 index 000000000..6a1e81bb8 --- /dev/null +++ b/packages/tui/internal/util/text.go @@ -0,0 +1,47 @@ +package util + +import ( + "strings" + + "github.com/charmbracelet/lipgloss/v2" +) + +// PreventHyphenBreaks replaces regular hyphens with non-breaking hyphens to prevent +// sparse word breaks in hyphenated terms like "claude-code-action". +// This improves readability by keeping hyphenated words together. +func PreventHyphenBreaks(text string) string { + return strings.ReplaceAll(text, "-", "\u2011") +} + +// RestoreHyphens converts non-breaking hyphens back to regular hyphens. +// This should be called after text processing (like word wrapping) is complete. +func RestoreHyphens(text string) string { + return strings.ReplaceAll(text, "\u2011", "-") +} + +// ProcessTextWithHyphens applies hyphen preservation to text during processing. +// It wraps the provided processFunc with hyphen handling. +func ProcessTextWithHyphens(text string, processFunc func(string) string) string { + preserved := PreventHyphenBreaks(text) + processed := processFunc(preserved) + return RestoreHyphens(processed) +} + +// GetMessageContainerFrame calculates the actual horizontal frame size +// (padding + borders) for message containers based on current theme. +func GetMessageContainerFrame() int { + style := lipgloss.NewStyle(). + BorderStyle(lipgloss.ThickBorder()). + BorderLeft(true). + BorderRight(true). + PaddingLeft(2). + PaddingRight(2) + return style.GetHorizontalFrameSize() +} + +// GetMarkdownContainerFrame calculates the actual horizontal frame size +// for markdown containers based on current theme. +func GetMarkdownContainerFrame() int { + // Markdown containers use the same styling as message containers + return GetMessageContainerFrame() +} -- cgit v1.2.3