on main help sign in
fix(tui): fix background color of file view component (#349)
commitb1b5851f233182cc0fd22cdea7edcd3c7f76db4e authorKira Kawai <66677201+ras0q@users.noreply.github.com> dateFri 1 Aug 2025, 22:39 (1 year ago) parent931832c
1 file changed+28−13
internal/tui/components/chat/messages/renderer.go+28−13
internal/tui/components/chat/messages/renderer.go+28−13
@@ -780,13 +780,21 @@ func renderPlainContent(v *toolCallCmp, content string) string {
780780 return strings.Join(out, "\n")
781781}
782782
783−func pad(v any, width int) string {
784− s := fmt.Sprintf("%v", v)
785− w := ansi.StringWidth(s)
786− if w >= width {
787− return s
783+func getDigits(n int) int {
784+ if n == 0 {
785+ return 1
788786 }
789− return strings.Repeat(" ", width-w) + s
787+ if n < 0 {
788+ n = -n
789+ }
790+
791+ digits := 0
792+ for n > 0 {
793+ n /= 10
794+ digits++
795+ }
796+
797+ return digits
790798}
791799
792800func renderCodeContent(v *toolCallCmp, path, content string, offset int) string {
@@ -800,30 +808,37 @@ func renderCodeContent(v *toolCallCmp, path, content string, offset int) string
800808 lines[i] = ansiext.Escape(ln)
801809 }
802810
803− highlighted, _ := highlight.SyntaxHighlight(strings.Join(lines, "\n"), path, t.BgBase)
811+ bg := t.BgBase
812+ highlighted, _ := highlight.SyntaxHighlight(strings.Join(lines, "\n"), path, bg)
804813 lines = strings.Split(highlighted, "\n")
805814
806815 if len(strings.Split(content, "\n")) > responseContextHeight {
807816 lines = append(lines, t.S().Muted.
808− Background(t.BgBase).
817+ Background(bg).
809818 Render(fmt.Sprintf(" …(%d lines)", len(strings.Split(content, "\n"))-responseContextHeight)))
810819 }
811820
812821 maxLineNumber := len(lines) + offset
813− padding := lipgloss.Width(fmt.Sprintf("%d", maxLineNumber))
822+ maxDigits := getDigits(maxLineNumber)
823+ numFmt := fmt.Sprintf("%%%dd", maxDigits)
824+ const numPR, numPL, codePR, codePL = 1, 1, 1, 2
825+ w := v.textWidth() - maxDigits - numPL - numPR - 2 // -2 for left padding
814826 for i, ln := range lines {
815827 num := t.S().Base.
816828 Foreground(t.FgMuted).
817829 Background(t.BgBase).
818830 PaddingRight(1).
819831 PaddingLeft(1).
820− Render(pad(i+1+offset, padding))
821− w := v.textWidth() - 10 - lipgloss.Width(num) // -4 for left padding
832+ Render(fmt.Sprintf(numFmt, i+1+offset))
822833 lines[i] = lipgloss.JoinHorizontal(lipgloss.Left,
823834 num,
824835 t.S().Base.
825− PaddingLeft(1).
826− Render(v.fit(ln, w-1)))
836+ Width(w).
837+ Background(bg).
838+ PaddingRight(1).
839+ PaddingLeft(2).
840+ Render(v.fit(ln, w-codePL-codePR)),
841+ )
827842 }
828843
829844 return lipgloss.JoinVertical(lipgloss.Left, lines...)