forked from mewbak/clipman
-
Notifications
You must be signed in to change notification settings - Fork 5
/
selector.go
154 lines (136 loc) · 3.62 KB
/
selector.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package main
import (
"errors"
"fmt"
"os"
"os/exec"
"strconv"
"strings"
"github.com/kballard/go-shellquote"
)
func selector(data []string, maxChar int, tool, prompt, toolArgs string, null, errorOnNoSelection bool) (string, error) {
if len(data) == 0 {
return "", errors.New("nothing to show: no data available")
}
// output to stdout and return
if tool == "STDOUT" {
escaped, _ := preprocessData(data, 0, !null)
sep := "\n"
if null {
sep = "\000"
}
os.Stdout.WriteString(strings.Join(escaped, sep))
return "", nil
}
var (
args []string
err error
)
switch tool {
case "dmenu":
args = []string{
"dmenu", "-b",
"-fn",
"-misc-dejavu sans mono-medium-r-normal--17-120-100-100-m-0-iso8859-16",
"-l",
strconv.Itoa(maxChar),
}
case "bemenu":
args = []string{"bemenu", "--prompt", prompt, "--list", strconv.Itoa(maxChar)}
case "rofi":
args = []string{
"rofi", "-p", prompt, "-dmenu",
"-lines",
strconv.Itoa(maxChar),
}
case "wofi":
args = []string{"wofi", "-p", prompt, "--cache-file", "/dev/null", "--dmenu"}
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)
}
default:
return "", fmt.Errorf("unsupported tool: %s", tool)
}
if tool == "CUSTOM" {
tool = args[0]
} else if len(toolArgs) > 0 {
targs, err := shellquote.Split(toolArgs)
if err != nil {
return "", fmt.Errorf("selector: %w", err)
}
args = append(args, targs...)
}
bin, err := exec.LookPath(tool)
if err != nil {
return "", fmt.Errorf("%s is not installed", tool)
}
processed, guide := preprocessData(data, 1000, !null)
sep := "\n"
if null {
sep = "\000"
}
cmd := exec.Cmd{Path: bin, Args: args, Stdin: strings.NewReader(strings.Join(processed, sep))}
cmd.Stderr = os.Stderr // let stderr pass to console
b, err := cmd.Output()
if err != nil {
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
if errorOnNoSelection {
os.Exit(1)
}
return "", nil
}
return "", err
}
// we received no selection; wofi doesn't error in this case
if len(b) == 0 {
if errorOnNoSelection {
os.Exit(1)
}
return "", nil
}
// drop newline added by proper unix tools
if b[len(b)-1] == '\n' {
b = b[:len(b)-1]
}
sel, ok := guide[string(b)]
if !ok {
return "", errors.New("couldn't recover original string")
}
return sel, nil
}
// preprocessData:
// - reverses the data
// - optionally escapes \n, \r and \t (it would break some external selectors)
// - optionally it cuts items longer than maxChars bytes (dmenu doesn't allow more than ~1200)
// A guide is created to allow restoring the selected item.
func preprocessData(data []string, maxChars int, escape bool) ([]string, map[string]string) {
var escaped []string
guide := make(map[string]string)
for i := len(data) - 1; i >= 0; i-- { // reverse slice
original := data[i]
repr := original
// escape newlines
if escape {
repr = strings.ReplaceAll(repr, "\\n", "\\\\n") // preserve literal \n
repr = strings.ReplaceAll(repr, "\n", "\\n")
repr = strings.ReplaceAll(repr, "\\t", "\\\\t")
repr = strings.ReplaceAll(repr, "\t", "\\t")
repr = strings.ReplaceAll(repr, "\\r", "\\\\r")
repr = strings.ReplaceAll(repr, "\r", "\\r")
}
// optionally cut to maxChars
if maxChars > 0 && len(repr) > maxChars {
repr = repr[:maxChars]
}
guide[repr] = original
escaped = append(escaped, repr)
}
return escaped, guide
}