on main help sign in
fix(diffview): fix left pad for ansi stuff
c8e653eAndrey Nering
internal/exp/diffview/util.go+5−2
@@ -3,12 +3,15 @@ package diffview
3import (
3import (
4 "fmt"
4 "fmt"
5 "strings"
5 "strings"
6+
7+ "github.com/charmbracelet/x/ansi"
6)
8)
7
9
8func pad(v any, width int) string {
10func pad(v any, width int) string {
9 s := fmt.Sprintf("%v", v)
11 s := fmt.Sprintf("%v", v)
12+ w := ansi.StringWidth(s)
10− if len(s) >= width {
13+ if w >= width {
11 return s
14 return s
12 }
15 }
13− return strings.Repeat(" ", width-len(s)) + s
16+ return strings.Repeat(" ", width-w) + s
14}
17}
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+}