mirror of
https://github.com/restic/restic.git
synced 2026-12-04 15:44:46 +00:00
ui: add helper to quote slices
This commit is contained in:
1 parent
e63cb63ba6
commit
0e50a959d5
2 files changed
+17
No files matched your search
@@ -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 {
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in new issue
Block a user