File: selector.go

package info (click to toggle)
clipman 1.2.0%2Bgit20200218.39fd4fe-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 136 kB
  • sloc: makefile: 4
file content (109 lines) | stat: -rw-r--r-- 2,795 bytes parent folder | download | duplicates (2)
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
package main

import (
	"errors"
	"fmt"
	"os"
	"os/exec"
	"strconv"
	"strings"
)

func selector(data []string, max int, tool string, prompt string, toolsArgs string) (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, false, true)
		os.Stdout.WriteString(strings.Join(escaped, "\n"))
		return "", nil
	}

	bin, err := exec.LookPath(tool)
	if err != nil {
		return "", fmt.Errorf("%s is not installed", tool)
	}

	var args []string
	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(max)}
	case "bemenu":
		args = []string{"bemenu", "--bottom", "--prompt", prompt, "--list", strconv.Itoa(max)}
	case "rofi":
		args = []string{"rofi", "-p", prompt, "-dmenu",
			"-lines",
			strconv.Itoa(max)}
	case "wofi":
		args = []string{"wofi", "-p", prompt, "--cache-file", "/dev/null", "--dmenu"}
	default:
		return "", fmt.Errorf("unsupported tool: %s", tool)
	}

	args = append(args, strings.Fields(toolsArgs)...)

	processed, guide := preprocessData(data, true, false)

	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 {
		if err.Error() == "exit status 1" {
			// dmenu/rofi exits with this error when no selection done
			return "", nil
		}
		return "", err
	}

	// Wofi however does not error when no selection is done
	if len(b) == 0 {
		return "", nil
	}

	sel, ok := guide[string(b[:len(b)-1])] // drop newline added by dmenu/roi/wofi
	if !ok {
		return "", errors.New("couldn't recover original string")
	}

	return sel, nil
}

// 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)
// A guide is created to allow restoring the selected item.
func preprocessData(data []string, cutting bool, allowTabs 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]

		// 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]
		}

		guide[repr] = original
		escaped = append(escaped, repr)
	}

	return escaped, guide
}