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 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 - 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` - 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

48
main.go
View file

@ -7,20 +7,25 @@ import (
"io/ioutil" "io/ioutil"
"log" "log"
"os" "os"
"os/exec"
"strings" "strings"
"gopkg.in/alecthomas/kingpin.v2" "gopkg.in/alecthomas/kingpin.v2"
) )
var ( var (
app = kingpin.New("clipman", "A clipboard manager for Wayland") app = kingpin.New("clipman", "A clipboard manager for Wayland")
histpath = app.Flag("histpath", "Path of history file").Default("~/.local/share/clipman.json").String() 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") storer = app.Command("store", "Run from `wl-paste --watch` to record clipboard events")
picker = app.Command("pick", "Pick an item from clipboard history") picker = app.Command("pick", "Pick an item from clipboard history")
noPersist = storer.Flag("no-persist", "Don't persist a copy buffer after a program exits").Short('P').Default("false").Bool() clearer = app.Command("clear", "Remove an item from history")
maxDemon = storer.Flag("max-items", "history size").Default("15").Int() noPersist = storer.Flag("no-persist", "Don't persist a copy buffer after a program exits").Short('P').Default("false").Bool()
maxPicker = picker.Flag("max-items", "scrollview length").Default("15").Int() maxDemon = storer.Flag("max-items", "history size").Default("15").Int()
tool = picker.Flag("selector", "Which selector to use: dmenu/rofi/-").Default("dmenu").String() maxPicker = picker.Flag("max-items", "scrollview length").Default("15").Int()
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() { func main() {
@ -52,7 +57,32 @@ func main() {
log.Fatal(err) 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) log.Fatal(err)
} }
} }

View file

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