@@ -3,12 +3,15 @@ package diffview
import (
"fmt"
"strings"
+
+ "github.com/charmbracelet/x/ansi"
)
func pad(v any, width int) string {
s := fmt.Sprintf("%v", v)
- if len(s) >= width {
+ w := ansi.StringWidth(s)
+ if w >= width {
return s
}
- return strings.Repeat(" ", width-len(s)) + s
+ return strings.Repeat(" ", width-w) + s
}
@@ -0,0 +1,27 @@
+package diffview
+
+import (
+ "testing"
+)
+
+func TestPad(t *testing.T) {
+ tests := []struct {
+ input any
+ width int
+ expected string
+ }{
+ {7, 2, " 7"},
+ {7, 3, " 7"},
+ {"a", 2, " a"},
+ {"a", 3, " a"},
+ {"…", 2, " …"},
+ {"…", 3, " …"},
+ }
+
+ for _, tt := range tests {
+ result := pad(tt.input, tt.width)
+ if result != tt.expected {
+ t.Errorf("expected %q, got %q", tt.expected, result)
+ }
+ }
+}