fix(selector): only escape newlines

Previously we were also escaping unneeded stuff like tabs, etc.
This commit is contained in:
yory8 2019-09-17 11:27:37 +02:00
parent 1d9de528ed
commit e0e1c47c0a

View file

@ -65,8 +65,8 @@ func selector(data []string, max int, tool string) (string, error) {
// preprocessData: // preprocessData:
// - reverses the data // - reverses the data
// - escapes special characters (like newlines) that would break external selectors; // - escapes \n (it would break external selectors)
// - optionally it cuts items longer than 400 bytes (dmenu doesn't allow more than ~1200). // - 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. // A guide is created to allow restoring the selected item.
func preprocessData(data []string, cutting bool) ([]string, map[string]string) { func preprocessData(data []string, cutting bool) ([]string, map[string]string) {
var escaped []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 for i := len(data) - 1; i >= 0; i-- { // reverse slice
original := data[i] original := data[i]
repr := fmt.Sprintf("%#v", original) // escape newlines
size := len(repr) - 1 repr := strings.ReplaceAll(original, "\\n", "\\\\n") // preserve literal \n
if cutting { repr = strings.ReplaceAll(repr, "\n", "\\n")
const maxChars = 400
if size > maxChars { // optionally cut to maxChars
size = maxChars const maxChars = 400
} if cutting && len(repr) > maxChars {
repr = repr[:maxChars]
} }
repr = repr[1:size] // drop left and right quotes
guide[repr] = original guide[repr] = original
escaped = append(escaped, repr) escaped = append(escaped, repr)