fix(diffview): fix left pad for ansi stuff
commitc8e653e2787ef602a0d536f7c4cec717e1cfe60a
authorAndrey Nering <andreynering@users.noreply.github.com>
dateThu 5 Jun 2025, 13:38 (1 year ago)
parentab0c4ff
internal/exp/diffview/util.go+5−2
@@ -3,12 +3,15 @@ package diffview
33import (
44 "fmt"
55 "strings"
6+
7+ "github.com/charmbracelet/x/ansi"
68)
79
810func pad(v any, width int) string {
911 s := fmt.Sprintf("%v", v)
10− if len(s) >= width {
12+ w := ansi.StringWidth(s)
13+ if w >= width {
1114 return s
1215 }
13− return strings.Repeat(" ", width-len(s)) + s
16+ return strings.Repeat(" ", width-w) + s
1417}
internal/exp/diffview/util_test.gonew file+27
@@ -0,0 +1,27 @@
1+package diffview
2+
3+import (
4+ "testing"
5+)
6+
7+func TestPad(t *testing.T) {
8+ tests := []struct {
9+ input any
10+ width int
11+ expected string
12+ }{
13+ {7, 2, " 7"},
14+ {7, 3, " 7"},
15+ {"a", 2, " a"},
16+ {"a", 3, " a"},
17+ {"…", 2, " …"},
18+ {"…", 3, " …"},
19+ }
20+
21+ for _, tt := range tests {
22+ result := pad(tt.input, tt.width)
23+ if result != tt.expected {
24+ t.Errorf("expected %q, got %q", tt.expected, result)
25+ }
26+ }
27+}