clipman/selector.go

118 lines
2.9 KiB
Go
Raw Normal View History

package main
import (
2019-09-17 09:46:30 +02:00
"errors"
"fmt"
"os"
"os/exec"
"strconv"
"strings"
2020-02-25 10:08:02 +01:00
"github.com/kballard/go-shellquote"
)
2020-02-25 10:08:02 +01:00
func selector(data []string, max int, tool string, prompt string, toolArgs string) (string, error) {
2019-09-17 09:46:30 +02:00
if len(data) == 0 {
return "", errors.New("nothing to show: no data available")
}
2019-09-17 09:46:30 +02:00
// output to stdout and return
if tool == "STDOUT" {
escaped, _ := preprocessData(data, false, true)
os.Stdout.WriteString(strings.Join(escaped, "\n"))
return "", nil
}
2019-06-17 08:40:45 +02:00
bin, err := exec.LookPath(tool)
2019-05-09 18:46:16 +02:00
if err != nil {
return "", fmt.Errorf("%s is not installed", tool)
}
2019-05-09 08:42:43 +02:00
var args []string
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)}
case "rofi":
args = []string{"rofi", "-p", prompt, "-dmenu",
2019-05-09 08:42:43 +02:00
"-lines",
strconv.Itoa(max)}
case "wofi":
args = []string{"wofi", "-p", prompt, "--cache-file", "/dev/null", "--dmenu"}
default:
2019-11-08 13:22:36 +01:00
return "", fmt.Errorf("unsupported tool: %s", tool)
2019-05-09 08:42:43 +02:00
}
2020-02-25 10:08:02 +01:00
if len(toolArgs) > 0 {
targs, err := shellquote.Split(toolArgs)
if err != nil {
return "", fmt.Errorf("selector: %w", err)
}
args = append(args, targs...)
}
processed, guide := preprocessData(data, true, false)
2019-09-17 09:46:30 +02:00
cmd := exec.Cmd{Path: bin, Args: args, Stdin: strings.NewReader(strings.Join(processed, "\n"))}
cmd.Stderr = os.Stderr // let stderr pass to console
b, err := cmd.Output()
if err != nil {
2019-05-10 09:20:43 +02:00
if err.Error() == "exit status 1" {
2019-09-17 09:46:30 +02:00
// dmenu/rofi exits with this error when no selection done
2019-05-10 09:20:43 +02:00
return "", nil
}
return "", err
}
// Wofi however does not error when no selection is done
if len(b) == 0 {
return "", nil
}
2019-11-08 13:22:36 +01:00
sel, ok := guide[string(b[:len(b)-1])] // drop newline added by dmenu/roi/wofi
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-09-17 09:46:30 +02:00
// preprocessData:
// - reverses the data
// - escapes \n (it would break external selectors)
// - optionally it cuts items longer than 400 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.
func preprocessData(data []string, cutting bool, allowTabs 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]
2019-09-15 09:34:04 +02:00
// escape newlines
repr := strings.ReplaceAll(original, "\\n", "\\\\n") // preserve literal \n
repr = strings.ReplaceAll(repr, "\n", "\\n")
if !allowTabs {
repr = strings.ReplaceAll(repr, "\\t", "\\\\t")
repr = strings.ReplaceAll(repr, "\t", "\\t")
}
// optionally cut to maxChars
const maxChars = 400
if cutting && len(repr) > maxChars {
repr = repr[:maxChars]
2019-09-15 09:34:04 +02:00
}
guide[repr] = original
escaped = append(escaped, repr)
}
return escaped, guide
}