ui: add helper to quote slices

This commit is contained in:
Michael Eischer committed 2026-11-28 12:30:00 +01:00
1 parent e63cb63ba6
commit 0e50a959d5
2 files changed
+17

No files matched your search

+10
View File
@@ -146,6 +146,16 @@ func Quote(line string) string {
return line
}
// QuoteSlice quotes elements that contain terminal control characters.
// Elements that are already safe to print are left unchanged.
func QuoteSlice(vals []string) []string {
out := make([]string, len(vals))
for i, v := range vals {
out[i] = Quote(v)
}
return out
}
// Truncate s to fit in width (number of terminal cells) w.
// If w is negative, returns the empty string.
func Truncate(s string, w int) string {
+7
View File
@@ -129,6 +129,13 @@ func TestQuote(t *testing.T) {
}
}
func TestQuoteSlice(t *testing.T) {
in := []string{"ok", "a\nb", "plain"}
rtest.Equals(t, []string{"ok", strconv.Quote("a\nb"), "plain"}, QuoteSlice(in))
rtest.Equals(t, "a\nb", in[1])
rtest.Equals(t, []string{}, QuoteSlice(nil))
}
func TestTruncate(t *testing.T) {
var tests = []struct {
input string