on main help sign in
fix(tui): fix background color of file view component (#349)
b1b5851Kira Kawai
internal/tui/components/chat/messages/renderer.go+28−13
@@ -780,13 +780,21 @@ func renderPlainContent(v *toolCallCmp, content string) string {
780 return strings.Join(out, "\n")
780 return strings.Join(out, "\n")
781}
781}
782
782
783−func pad(v any, width int) string {
783+func getDigits(n int) int {
784− s := fmt.Sprintf("%v", v)
784+ if n == 0 {
785− w := ansi.StringWidth(s)
786− if w >= width {
787− return s
785+ return 1
788 }
786 }
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
790}
798}
791
799
792func renderCodeContent(v *toolCallCmp, path, content string, offset int) string {
800func renderCodeContent(v *toolCallCmp, path, content string, offset int) string {
@@ -800,30 +808,37 @@ func renderCodeContent(v *toolCallCmp, path, content string, offset int) string
800 lines[i] = ansiext.Escape(ln)
808 lines[i] = ansiext.Escape(ln)
801 }
809 }
802
810
811+ bg := t.BgBase
803− highlighted, _ := highlight.SyntaxHighlight(strings.Join(lines, "\n"), path, t.BgBase)
812+ highlighted, _ := highlight.SyntaxHighlight(strings.Join(lines, "\n"), path, bg)
804 lines = strings.Split(highlighted, "\n")
813 lines = strings.Split(highlighted, "\n")
805
814
806 if len(strings.Split(content, "\n")) > responseContextHeight {
815 if len(strings.Split(content, "\n")) > responseContextHeight {
807 lines = append(lines, t.S().Muted.
816 lines = append(lines, t.S().Muted.
808− Background(t.BgBase).
817+ Background(bg).
809 Render(fmt.Sprintf(" …(%d lines)", len(strings.Split(content, "\n"))-responseContextHeight)))
818 Render(fmt.Sprintf(" …(%d lines)", len(strings.Split(content, "\n"))-responseContextHeight)))
810 }
819 }
811
820
812 maxLineNumber := len(lines) + offset
821 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
814 for i, ln := range lines {
826 for i, ln := range lines {
815 num := t.S().Base.
827 num := t.S().Base.
816 Foreground(t.FgMuted).
828 Foreground(t.FgMuted).
817 Background(t.BgBase).
829 Background(t.BgBase).
818 PaddingRight(1).
830 PaddingRight(1).
819 PaddingLeft(1).
831 PaddingLeft(1).
820− Render(pad(i+1+offset, padding))
832+ Render(fmt.Sprintf(numFmt, i+1+offset))
821− w := v.textWidth() - 10 - lipgloss.Width(num) // -4 for left padding
822 lines[i] = lipgloss.JoinHorizontal(lipgloss.Left,
833 lines[i] = lipgloss.JoinHorizontal(lipgloss.Left,
823 num,
834 num,
824 t.S().Base.
835 t.S().Base.
836+ Width(w).
837+ Background(bg).
838+ PaddingRight(1).
825− PaddingLeft(1).
839+ PaddingLeft(2).
826− Render(v.fit(ln, w-1)))
840+ Render(v.fit(ln, w-codePL-codePR)),
841+ )
827 }
842 }
828
843
829 return lipgloss.JoinVertical(lipgloss.Left, lines...)
844 return lipgloss.JoinVertical(lipgloss.Left, lines...)