feat: implement "clear" command

Closes #17
This commit is contained in:
yory8 2019-09-16 20:44:19 +02:00
parent 460c5080d0
commit debad600c4
3 changed files with 51 additions and 19 deletions

View file

@ -4,4 +4,8 @@
- switch from flags to subcommands: `wl-paste -t text --watch clipman store` instead than `clipman -d` and `clipman pick` instead than `clipman -s`
- switch demon from polling to event-driven: requires wl-clipboard >= 2.0
**New features**:
- primary clipboard support: `wl-paste -p -t text --watch clipman store --histpath="~/.local/share/clipman-primary.json` and `clipman pick --histpath="~/.local/share/clipman-primary.json`
- new `clear` command for removing item(s) from history

34
main.go
View file

@ -7,6 +7,7 @@ import (
"io/ioutil"
"log"
"os"
"os/exec"
"strings"
"gopkg.in/alecthomas/kingpin.v2"
@ -17,10 +18,14 @@ var (
histpath = app.Flag("histpath", "Path of history file").Default("~/.local/share/clipman.json").String()
storer = app.Command("store", "Run from `wl-paste --watch` to record clipboard events")
picker = app.Command("pick", "Pick an item from clipboard history")
clearer = app.Command("clear", "Remove an item from history")
noPersist = storer.Flag("no-persist", "Don't persist a copy buffer after a program exits").Short('P').Default("false").Bool()
maxDemon = storer.Flag("max-items", "history size").Default("15").Int()
maxPicker = picker.Flag("max-items", "scrollview length").Default("15").Int()
tool = picker.Flag("selector", "Which selector to use: dmenu/rofi/-").Default("dmenu").String()
pickTool = picker.Flag("selector", "Which selector to use: dmenu/rofi/-").Default("dmenu").String()
clearTool = clearer.Flag("selector", "Which selector to use: dmenu/rofi/-").Default("dmenu").String()
maxClearer = clearer.Flag("max-items", "scrollview length").Default("15").Int()
clearAll = clearer.Flag("all", "Remove all items").Short('a').Default("false").Bool()
)
func main() {
@ -52,7 +57,32 @@ func main() {
log.Fatal(err)
}
if err := selector(history, *maxPicker, *tool); err != nil {
selection, err := selector(history, *maxPicker, *pickTool)
if err != nil {
log.Fatal(err)
}
// serve selection to the OS
err = exec.Command("wl-copy", []string{"--", selection}...).Run()
case "clear":
histfile, history, err := getHistory()
if err != nil {
log.Fatal(err)
}
if *clearAll {
if err := os.Remove(histfile); err != nil {
log.Fatal(err)
}
os.Exit(0)
}
selection, err := selector(history, *maxClearer, *clearTool)
if err != nil {
log.Fatal(err)
}
if err := write(filter(history, selection), histfile); err != nil {
log.Fatal(err)
}
}

View file

@ -9,25 +9,23 @@ import (
"strings"
)
func selector(history []string, max int, tool string) error {
func selector(history []string, max int, tool string) (string, error) {
if len(history) == 0 {
log.Fatal("No history available")
}
// don't modify in-place!
tmp := make([]string, len(history))
copy(tmp, history)
// reverse the history
for i, j := 0, len(history)-1; i < j; i, j = i+1, j-1 {
history[i], history[j] = history[j], history[i]
tmp[i], tmp[j] = tmp[j], tmp[i]
}
selected, err := dmenu(history, max, tool)
if err != nil {
return err
}
selected, err := dmenu(tmp, max, tool)
// serve selection to the OS
err = exec.Command("wl-copy", []string{"--", selected}...).Run()
return err
return selected, err
}
func dmenu(list []string, max int, tool string) (string, error) {