From 9819a717777292fe54a7bef614c4bb915cd769bc Mon Sep 17 00:00:00 2001 From: yory8 <> Date: Fri, 10 May 2019 09:20:43 +0200 Subject: [PATCH] clean dmenu code --- selector.go | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/selector.go b/selector.go index 5934887..6ad97e0 100644 --- a/selector.go +++ b/selector.go @@ -15,10 +15,6 @@ func selector(history []string, max int, tool string) error { selected, err := dmenu(history, max, tool) if err != nil { - if err.Error() == "exit status 1" { - // dmenu exits with this error when no selection done - return nil - } return err } @@ -52,30 +48,36 @@ func dmenu(list []string, max int, tool string) (string, error) { } // dmenu will break if items contain newlines, so we must pass them as literals. - // however, when it sends them back, we need a way to restore them to non literals + // however, when it sends them back, we need a way to restore them guide := make(map[string]string) - reprList := []string{} + escaped := []string{} for _, original := range list { repr := fmt.Sprintf("%#v", original) + + // dmenu will split lines longer than 1200 something; we cut at 400 to spare memory max := len(repr) - 1 // drop right quote maxChars := 400 - // dmenu will split lines longer than 1200 something; we cut at 400 to spare memory if max > maxChars { max = maxChars } repr = repr[1:max] // drop left quote + guide[repr] = original - reprList = append(reprList, repr) + escaped = append(escaped, repr) } - input := strings.NewReader(strings.Join(reprList, "\n")) + input := strings.NewReader(strings.Join(escaped, "\n")) cmd := exec.Cmd{Path: bin, Args: args, Stdin: input} selected, err := cmd.Output() if err != nil { + if err.Error() == "exit status 1" { + // dmenu exits with this error when no selection done + return "", nil + } return "", err } - trimmed := selected[:len(selected)-1] // drop newline + trimmed := selected[:len(selected)-1] // drop newline added by dmenu sel, ok := guide[string(trimmed)] if !ok {