From 1ed0c4071525ae338ee0832d1d11bc3620595140 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartosz=20Tacza=C5=82a?= Date: Tue, 7 May 2019 11:59:09 +0200 Subject: [PATCH] Provide a selector option This allows to switch between using dmenu/rofi --- main.go | 3 ++- selector.go | 26 +++++++++++++++++--------- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/main.go b/main.go index 7136a00..bb7180d 100644 --- a/main.go +++ b/main.go @@ -16,6 +16,7 @@ var ( asSelector = app.Flag("select", "Select an item from clipboard history").Short('s').Default("false").Bool() noPersist = app.Flag("no-persist", "Don't persist a copy buffer after a program exits").Short('P').Default("false").Bool() max = app.Flag("max-items", "items to store in history (with -d) or display before scrolling (with -s)").Default("15").Int() + tool = app.Flag("selector", "Which selector to use: dmenu/rofi").Default("dmenu").String() ) func main() { @@ -45,7 +46,7 @@ func main() { log.Fatal(err) } } else if *asSelector { - if err := selector(history, *max); err != nil { + if err := selector(history, *max, *tool); err != nil { log.Fatal(err) } } diff --git a/selector.go b/selector.go index 70e68bc..7d77c4c 100644 --- a/selector.go +++ b/selector.go @@ -7,13 +7,13 @@ import ( "strings" ) -func selector(history []string, max int) error { +func selector(history []string, max int, tool string) error { // reverse the history for i, j := 0, len(history)-1; i < j; i, j = i+1, j-1 { history[i], history[j] = history[j], history[i] } - selected, err := dmenu(history, max) + selected, err := dmenu(history, max, tool) if err != nil { // dmenu exits with error when no selection done return nil @@ -25,16 +25,23 @@ func selector(history []string, max int) error { return err } -func dmenu(list []string, max int) (string, error) { +func dmenu(list []string, max int, tool string) (string, error) { if len(list) == 0 { return "", nil } - args := []string{"dmenu", "-b", - "-fn", - "-misc-dejavu sans mono-medium-r-normal--17-120-100-100-m-0-iso8859-16", - "-l", - strconv.Itoa(max)} + var args []string; + if tool == "dmenu" { + args = []string{"dmenu", "-b", + "-fn", + "-misc-dejavu sans mono-medium-r-normal--17-120-100-100-m-0-iso8859-16", + "-l", + strconv.Itoa(max)} + } else { + args = []string{"rofi", "-dmenu", + "-lines", + strconv.Itoa(max)} + } // 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 @@ -55,9 +62,10 @@ func dmenu(list []string, max int) (string, error) { input := strings.NewReader(strings.Join(reprList, "\n")) - cmd := exec.Cmd{Path: "/usr/bin/dmenu", Args: args, Stdin: input} + cmd := exec.Cmd{Path: "/usr/bin/" + tool, Args: args, Stdin: input} selected, err := cmd.Output() if err != nil { + fmt.Printf("%s", err) return "", err } trimmed := selected[:len(selected)-1] // drop newline