2019-03-22 16:25:09 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2019-09-17 09:46:30 +02:00
|
|
|
"errors"
|
2019-03-22 16:25:09 +01:00
|
|
|
"fmt"
|
2019-09-15 09:30:31 +02:00
|
|
|
"os"
|
2019-03-22 16:25:09 +01:00
|
|
|
"os/exec"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2020-02-25 10:08:02 +01:00
|
|
|
|
|
|
|
"github.com/kballard/go-shellquote"
|
2019-03-22 16:25:09 +01:00
|
|
|
)
|
|
|
|
|
2020-05-03 11:00:35 +02:00
|
|
|
func selector(data []string, max int, tool, prompt, toolArgs string, null bool) (string, error) {
|
2019-09-17 09:46:30 +02:00
|
|
|
if len(data) == 0 {
|
2019-09-30 22:12:25 +02:00
|
|
|
return "", errors.New("nothing to show: no data available")
|
2019-09-15 12:36:53 +02:00
|
|
|
}
|
|
|
|
|
2019-09-17 09:46:30 +02:00
|
|
|
// output to stdout and return
|
2019-09-17 12:53:35 +02:00
|
|
|
if tool == "STDOUT" {
|
2020-05-03 11:00:35 +02:00
|
|
|
escaped, _ := preprocessData(data, 0, !null)
|
|
|
|
sep := "\n"
|
|
|
|
if null {
|
|
|
|
sep = "\000"
|
|
|
|
}
|
|
|
|
os.Stdout.WriteString(strings.Join(escaped, sep))
|
2019-09-15 09:30:31 +02:00
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
2020-05-03 11:00:35 +02:00
|
|
|
var (
|
|
|
|
args []string
|
|
|
|
err error
|
|
|
|
)
|
2019-05-09 18:46:16 +02:00
|
|
|
|
2019-09-15 09:30:31 +02:00
|
|
|
switch tool {
|
|
|
|
case "dmenu":
|
2019-05-09 08:42:43 +02:00
|
|
|
args = []string{"dmenu", "-b",
|
|
|
|
"-fn",
|
|
|
|
"-misc-dejavu sans mono-medium-r-normal--17-120-100-100-m-0-iso8859-16",
|
|
|
|
"-l",
|
|
|
|
strconv.Itoa(max)}
|
2019-12-10 09:36:35 +01:00
|
|
|
case "bemenu":
|
|
|
|
args = []string{"bemenu", "--bottom", "--prompt", prompt, "--list", strconv.Itoa(max)}
|
2019-09-15 09:30:31 +02:00
|
|
|
case "rofi":
|
2019-10-13 20:28:46 +02:00
|
|
|
args = []string{"rofi", "-p", prompt, "-dmenu",
|
2019-05-09 08:42:43 +02:00
|
|
|
"-lines",
|
|
|
|
strconv.Itoa(max)}
|
2019-09-29 10:06:01 +02:00
|
|
|
case "wofi":
|
2019-10-13 20:28:46 +02:00
|
|
|
args = []string{"wofi", "-p", prompt, "--cache-file", "/dev/null", "--dmenu"}
|
2020-05-03 11:00:35 +02:00
|
|
|
case "CUSTOM":
|
|
|
|
if len(toolArgs) == 0 {
|
|
|
|
return "", fmt.Errorf("missing tool args for CUSTOM tool")
|
|
|
|
}
|
|
|
|
args, err = shellquote.Split(toolArgs)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("selector: %w", err)
|
|
|
|
}
|
2019-09-15 09:30:31 +02:00
|
|
|
default:
|
2019-11-08 13:22:36 +01:00
|
|
|
return "", fmt.Errorf("unsupported tool: %s", tool)
|
2019-05-09 08:42:43 +02:00
|
|
|
}
|
2019-03-22 16:25:09 +01:00
|
|
|
|
2020-05-03 11:00:35 +02:00
|
|
|
if tool == "CUSTOM" {
|
|
|
|
tool = args[0]
|
|
|
|
} else if len(toolArgs) > 0 {
|
2020-02-25 10:08:02 +01:00
|
|
|
targs, err := shellquote.Split(toolArgs)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("selector: %w", err)
|
|
|
|
}
|
|
|
|
args = append(args, targs...)
|
|
|
|
}
|
2019-10-14 09:40:00 +02:00
|
|
|
|
2020-05-03 11:00:35 +02:00
|
|
|
bin, err := exec.LookPath(tool)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("%s is not installed", tool)
|
|
|
|
}
|
2019-03-22 16:25:09 +01:00
|
|
|
|
2020-05-03 11:00:35 +02:00
|
|
|
processed, guide := preprocessData(data, 1000, !null)
|
|
|
|
sep := "\n"
|
|
|
|
if null {
|
|
|
|
sep = "\000"
|
|
|
|
}
|
|
|
|
|
2020-05-10 22:16:53 +02:00
|
|
|
cmd := exec.Cmd{Path: bin, Args: args, Stdin: strings.NewReader(strings.Join(processed, sep))}
|
2019-09-30 19:14:54 +02:00
|
|
|
cmd.Stderr = os.Stderr // let stderr pass to console
|
2019-09-30 19:09:15 +02:00
|
|
|
b, err := cmd.Output()
|
2019-03-22 16:25:09 +01:00
|
|
|
if err != nil {
|
2020-05-03 11:00:35 +02:00
|
|
|
if err.Error() == "exit status 1" || err.Error() == "exit status 130" {
|
|
|
|
// dmenu/rofi exits with 1 when no selection done
|
|
|
|
// fzf exits with 1 when no match, 130 when no selection done
|
2019-05-10 09:20:43 +02:00
|
|
|
return "", nil
|
|
|
|
}
|
2019-03-22 16:25:09 +01:00
|
|
|
return "", err
|
|
|
|
}
|
2019-09-29 10:06:01 +02:00
|
|
|
|
2020-05-03 11:00:35 +02:00
|
|
|
// we received no selection; wofi doesn't error in this case
|
2019-09-29 10:06:01 +02:00
|
|
|
if len(b) == 0 {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
2020-05-03 11:00:35 +02:00
|
|
|
// drop newline added by proper unix tools
|
|
|
|
if b[len(b)-1] == '\n' {
|
|
|
|
b = b[:len(b)-1]
|
|
|
|
}
|
|
|
|
sel, ok := guide[string(b)]
|
2019-04-04 09:04:57 +02:00
|
|
|
if !ok {
|
2019-09-17 09:46:30 +02:00
|
|
|
return "", errors.New("couldn't recover original string")
|
2019-04-04 09:04:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return sel, nil
|
2019-03-22 16:25:09 +01:00
|
|
|
}
|
2019-09-15 09:30:31 +02:00
|
|
|
|
2019-09-17 09:46:30 +02:00
|
|
|
// preprocessData:
|
|
|
|
// - reverses the data
|
2020-05-03 11:00:35 +02:00
|
|
|
// - optionally escapes \n and \t (it would break some external selectors)
|
|
|
|
// - optionally it cuts items longer than maxChars bytes (dmenu doesn't allow more than ~1200)
|
2019-09-17 09:46:30 +02:00
|
|
|
// A guide is created to allow restoring the selected item.
|
2020-05-03 11:00:35 +02:00
|
|
|
func preprocessData(data []string, maxChars int, escape bool) ([]string, map[string]string) {
|
2019-09-15 09:34:04 +02:00
|
|
|
var escaped []string
|
|
|
|
guide := make(map[string]string)
|
|
|
|
|
2019-09-17 09:46:30 +02:00
|
|
|
for i := len(data) - 1; i >= 0; i-- { // reverse slice
|
|
|
|
original := data[i]
|
2020-05-03 11:00:35 +02:00
|
|
|
repr := original
|
2019-09-15 09:34:04 +02:00
|
|
|
|
2019-09-17 11:27:37 +02:00
|
|
|
// escape newlines
|
2020-05-03 11:00:35 +02:00
|
|
|
if escape {
|
|
|
|
repr = strings.ReplaceAll(repr, "\\n", "\\\\n") // preserve literal \n
|
|
|
|
repr = strings.ReplaceAll(repr, "\n", "\\n")
|
2019-09-27 09:24:55 +02:00
|
|
|
repr = strings.ReplaceAll(repr, "\\t", "\\\\t")
|
|
|
|
repr = strings.ReplaceAll(repr, "\t", "\\t")
|
|
|
|
}
|
2019-09-17 11:27:37 +02:00
|
|
|
// optionally cut to maxChars
|
2020-05-03 11:00:35 +02:00
|
|
|
if maxChars > 0 && len(repr) > maxChars {
|
2019-09-17 11:27:37 +02:00
|
|
|
repr = repr[:maxChars]
|
2019-09-15 09:34:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
guide[repr] = original
|
|
|
|
escaped = append(escaped, repr)
|
|
|
|
}
|
|
|
|
|
|
|
|
return escaped, guide
|
2019-09-15 09:30:31 +02:00
|
|
|
}
|