2019-03-22 16:25:09 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2019-09-15 12:36:53 +02:00
|
|
|
"log"
|
2019-09-15 09:30:31 +02:00
|
|
|
"os"
|
2019-03-22 16:25:09 +01:00
|
|
|
"os/exec"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2019-09-16 20:44:19 +02:00
|
|
|
func selector(history []string, max int, tool string) (string, error) {
|
2019-09-15 12:36:53 +02:00
|
|
|
if len(history) == 0 {
|
|
|
|
log.Fatal("No history available")
|
|
|
|
}
|
|
|
|
|
2019-09-16 20:44:19 +02:00
|
|
|
// don't modify in-place!
|
|
|
|
tmp := make([]string, len(history))
|
|
|
|
copy(tmp, history)
|
|
|
|
|
2019-03-22 16:25:09 +01:00
|
|
|
// reverse the history
|
|
|
|
for i, j := 0, len(history)-1; i < j; i, j = i+1, j-1 {
|
2019-09-16 20:44:19 +02:00
|
|
|
tmp[i], tmp[j] = tmp[j], tmp[i]
|
2019-03-22 16:25:09 +01:00
|
|
|
}
|
|
|
|
|
2019-09-16 20:44:19 +02:00
|
|
|
selected, err := dmenu(tmp, max, tool)
|
2019-03-22 16:25:09 +01:00
|
|
|
|
2019-09-16 20:44:19 +02:00
|
|
|
return selected, err
|
2019-03-22 16:25:09 +01:00
|
|
|
}
|
|
|
|
|
2019-05-07 11:59:09 +02:00
|
|
|
func dmenu(list []string, max int, tool string) (string, error) {
|
2019-09-15 09:30:31 +02:00
|
|
|
if tool == "-" {
|
2019-09-15 09:34:04 +02:00
|
|
|
escaped, _ := preprocessHistory(list, false)
|
2019-09-15 09:30:31 +02:00
|
|
|
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
|
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-09-15 09:30:31 +02:00
|
|
|
case "rofi":
|
2019-05-09 08:42:43 +02:00
|
|
|
args = []string{"rofi", "-dmenu",
|
|
|
|
"-lines",
|
|
|
|
strconv.Itoa(max)}
|
2019-09-15 09:30:31 +02:00
|
|
|
default:
|
|
|
|
return "", fmt.Errorf("Unsupported tool")
|
2019-05-09 08:42:43 +02:00
|
|
|
}
|
2019-03-22 16:25:09 +01:00
|
|
|
|
2019-09-15 09:34:04 +02:00
|
|
|
escaped, guide := preprocessHistory(list, true)
|
2019-05-10 09:20:43 +02:00
|
|
|
input := strings.NewReader(strings.Join(escaped, "\n"))
|
2019-03-22 16:25:09 +01:00
|
|
|
|
2019-05-09 18:46:16 +02:00
|
|
|
cmd := exec.Cmd{Path: bin, Args: args, Stdin: input}
|
2019-03-22 16:25:09 +01:00
|
|
|
selected, err := cmd.Output()
|
|
|
|
if err != nil {
|
2019-05-10 09:20:43 +02:00
|
|
|
if err.Error() == "exit status 1" {
|
|
|
|
// dmenu exits with this error when no selection done
|
|
|
|
return "", nil
|
|
|
|
}
|
2019-03-22 16:25:09 +01:00
|
|
|
return "", err
|
|
|
|
}
|
2019-05-10 09:20:43 +02:00
|
|
|
trimmed := selected[:len(selected)-1] // drop newline added by dmenu
|
2019-03-22 16:25:09 +01:00
|
|
|
|
2019-04-04 09:04:57 +02:00
|
|
|
sel, ok := guide[string(trimmed)]
|
|
|
|
if !ok {
|
|
|
|
return "", fmt.Errorf("couldn't recover original string; please report this bug along with a copy of your clipman.json")
|
|
|
|
}
|
|
|
|
|
|
|
|
return sel, nil
|
2019-03-22 16:25:09 +01:00
|
|
|
}
|
2019-09-15 09:30:31 +02:00
|
|
|
|
2019-09-15 09:34:04 +02:00
|
|
|
func preprocessHistory(list []string, cutting bool) ([]string, map[string]string) {
|
|
|
|
// 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
|
|
|
|
var escaped []string
|
|
|
|
guide := make(map[string]string)
|
|
|
|
|
|
|
|
for _, original := range list {
|
|
|
|
repr := fmt.Sprintf("%#v", original)
|
|
|
|
max := len(repr) - 1 // drop right quote
|
|
|
|
|
|
|
|
// dmenu will split lines longer than 1200 something; we cut at 400 to spare memory
|
|
|
|
if cutting {
|
|
|
|
maxChars := 400
|
|
|
|
if max > maxChars {
|
|
|
|
max = maxChars
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
repr = repr[1:max] // drop left quote
|
|
|
|
guide[repr] = original
|
|
|
|
escaped = append(escaped, repr)
|
|
|
|
}
|
|
|
|
|
|
|
|
return escaped, guide
|
2019-09-15 09:30:31 +02:00
|
|
|
}
|