2019-03-22 16:25:09 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os/exec"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
func selector(history []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)
|
|
|
|
if err != nil {
|
|
|
|
// dmenu exits with error when no selection done
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := exec.Command("wl-copy", selected).Run(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func dmenu(list []string, max int) (string, error) {
|
|
|
|
args := []string{"dmenu", "-b",
|
|
|
|
"-fn",
|
|
|
|
"-misc-dejavu sans mono-medium-r-normal--17-120-100-100-m-0-iso8859-16",
|
|
|
|
"-l",
|
|
|
|
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
|
2019-03-22 22:05:37 +01:00
|
|
|
guide := make(map[string]string)
|
2019-03-22 16:25:09 +01:00
|
|
|
reprList := []string{}
|
2019-03-22 22:05:37 +01:00
|
|
|
for _, l := range list {
|
2019-03-22 16:25:09 +01:00
|
|
|
repr := fmt.Sprintf("%#v", l)
|
2019-03-22 22:05:37 +01:00
|
|
|
repr = repr[1 : len(repr)-1] // drop quotes
|
|
|
|
guide[repr] = l
|
2019-03-22 16:25:09 +01:00
|
|
|
reprList = append(reprList, repr)
|
|
|
|
}
|
|
|
|
|
|
|
|
input := strings.NewReader(strings.Join(reprList, "\n"))
|
|
|
|
|
|
|
|
cmd := exec.Cmd{Path: "/usr/bin/dmenu", Args: args, Stdin: input}
|
|
|
|
selected, err := cmd.Output()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2019-03-22 22:05:37 +01:00
|
|
|
return guide[string(selected)], nil
|
2019-03-22 16:25:09 +01:00
|
|
|
}
|