feat: add STDOUT selector (#20)
Note that the output is a repr string, so for example newlines and tabs are escaped as literals.
This commit is contained in:
parent
5a2ed95aee
commit
bcfcd493fc
2 changed files with 40 additions and 22 deletions
2
main.go
2
main.go
|
@ -17,7 +17,7 @@ var (
|
||||||
asSelector = app.Flag("select", "Select an item from clipboard history").Short('s').Default("false").Bool()
|
asSelector = app.Flag("select", "Select an item from clipboard history").Short('s').Default("false").Bool()
|
||||||
noPersist = app.Flag("no-persist", "Don't persist a copy buffer after a program exits").Short('P').Default("false").Bool()
|
noPersist = app.Flag("no-persist", "Don't persist a copy buffer after a program exits").Short('P').Default("false").Bool()
|
||||||
max = app.Flag("max-items", "history size (with -d) or scrollview length (with -s)").Default("15").Int()
|
max = app.Flag("max-items", "history size (with -d) or scrollview length (with -s)").Default("15").Int()
|
||||||
tool = app.Flag("selector", "Which selector to use: dmenu/rofi").Default("dmenu").String()
|
tool = app.Flag("selector", "Which selector to use: dmenu/rofi/-").Default("dmenu").String()
|
||||||
histpath = app.Flag("histpath", "Directory where to save history").Default("~/.local/share/clipman.json").String()
|
histpath = app.Flag("histpath", "Directory where to save history").Default("~/.local/share/clipman.json").String()
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
60
selector.go
60
selector.go
|
@ -2,6 +2,7 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
@ -29,43 +30,34 @@ func dmenu(list []string, max int, tool string) (string, error) {
|
||||||
return "", nil
|
return "", nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if tool == "-" {
|
||||||
|
escaped, _ := preprocess_history(list, false)
|
||||||
|
os.Stdout.WriteString(strings.Join(escaped, "\n"))
|
||||||
|
return "", nil
|
||||||
|
}
|
||||||
|
|
||||||
bin, err := exec.LookPath(tool)
|
bin, err := exec.LookPath(tool)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", fmt.Errorf("%s is not installed", tool)
|
return "", fmt.Errorf("%s is not installed", tool)
|
||||||
}
|
}
|
||||||
|
|
||||||
var args []string
|
var args []string
|
||||||
if tool == "dmenu" {
|
switch tool {
|
||||||
|
case "dmenu":
|
||||||
args = []string{"dmenu", "-b",
|
args = []string{"dmenu", "-b",
|
||||||
"-fn",
|
"-fn",
|
||||||
"-misc-dejavu sans mono-medium-r-normal--17-120-100-100-m-0-iso8859-16",
|
"-misc-dejavu sans mono-medium-r-normal--17-120-100-100-m-0-iso8859-16",
|
||||||
"-l",
|
"-l",
|
||||||
strconv.Itoa(max)}
|
strconv.Itoa(max)}
|
||||||
} else {
|
case "rofi":
|
||||||
args = []string{"rofi", "-dmenu",
|
args = []string{"rofi", "-dmenu",
|
||||||
"-lines",
|
"-lines",
|
||||||
strconv.Itoa(max)}
|
strconv.Itoa(max)}
|
||||||
|
default:
|
||||||
|
return "", fmt.Errorf("Unsupported tool")
|
||||||
}
|
}
|
||||||
|
|
||||||
// dmenu will break if items contain newlines, so we must pass them as literals.
|
escaped, guide := preprocess_history(list, true)
|
||||||
// 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)
|
|
||||||
|
|
||||||
// dmenu will split lines longer than 1200 something; we cut at 400 to spare memory
|
|
||||||
max := len(repr) - 1 // drop right quote
|
|
||||||
maxChars := 400
|
|
||||||
if max > maxChars {
|
|
||||||
max = maxChars
|
|
||||||
}
|
|
||||||
repr = repr[1:max] // drop left quote
|
|
||||||
|
|
||||||
guide[repr] = original
|
|
||||||
escaped = append(escaped, repr)
|
|
||||||
}
|
|
||||||
|
|
||||||
input := strings.NewReader(strings.Join(escaped, "\n"))
|
input := strings.NewReader(strings.Join(escaped, "\n"))
|
||||||
|
|
||||||
cmd := exec.Cmd{Path: bin, Args: args, Stdin: input}
|
cmd := exec.Cmd{Path: bin, Args: args, Stdin: input}
|
||||||
|
@ -86,3 +78,29 @@ func dmenu(list []string, max int, tool string) (string, error) {
|
||||||
|
|
||||||
return sel, nil
|
return sel, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func preprocess_history(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
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue