Provide a selector option

This allows to switch between using dmenu/rofi
This commit is contained in:
Bartosz Taczała 2019-05-07 11:59:09 +02:00
parent 112f478070
commit 1ed0c40715
2 changed files with 19 additions and 10 deletions

View file

@ -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)
}
}

View file

@ -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",
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