From e0e1c47c0a8004cd7fe9196caf20f7233a4a7057 Mon Sep 17 00:00:00 2001 From: yory8 <> Date: Tue, 17 Sep 2019 11:27:37 +0200 Subject: [PATCH] fix(selector): only escape newlines Previously we were also escaping unneeded stuff like tabs, etc. --- selector.go | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/selector.go b/selector.go index cb89965..54d6d09 100644 --- a/selector.go +++ b/selector.go @@ -65,8 +65,8 @@ func selector(data []string, max int, tool string) (string, error) { // preprocessData: // - reverses the data -// - escapes special characters (like newlines) that would break external selectors; -// - optionally it cuts items longer than 400 bytes (dmenu doesn't allow more than ~1200). +// - escapes \n (it would break external selectors) +// - optionally it cuts items longer than 400 bytes (dmenu doesn't allow more than ~1200) // A guide is created to allow restoring the selected item. func preprocessData(data []string, cutting bool) ([]string, map[string]string) { var escaped []string @@ -75,15 +75,15 @@ func preprocessData(data []string, cutting bool) ([]string, map[string]string) { for i := len(data) - 1; i >= 0; i-- { // reverse slice original := data[i] - repr := fmt.Sprintf("%#v", original) - size := len(repr) - 1 - if cutting { - const maxChars = 400 - if size > maxChars { - size = maxChars - } + // escape newlines + repr := strings.ReplaceAll(original, "\\n", "\\\\n") // preserve literal \n + repr = strings.ReplaceAll(repr, "\n", "\\n") + + // optionally cut to maxChars + const maxChars = 400 + if cutting && len(repr) > maxChars { + repr = repr[:maxChars] } - repr = repr[1:size] // drop left and right quotes guide[repr] = original escaped = append(escaped, repr)