clipman/main.go

74 lines
2 KiB
Go
Raw Normal View History

2019-03-22 15:13:41 +01:00
package main
import (
"encoding/json"
2019-08-02 13:51:13 +02:00
"fmt"
2019-03-22 15:13:41 +01:00
"io/ioutil"
"log"
"os"
2019-09-07 19:41:41 +02:00
"strings"
"gopkg.in/alecthomas/kingpin.v2"
2019-03-22 15:13:41 +01:00
)
var (
app = kingpin.New("clipman", "A clipboard manager for Wayland")
asDemon = app.Flag("demon", "Run as a demon to record clipboard events").Short('d').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()
2019-09-07 19:41:41 +02:00
max = app.Flag("max-items", "history size (with -d) or scrollview length (with -s)").Default("15").Int()
2019-05-09 18:46:16 +02:00
tool = app.Flag("selector", "Which selector to use: dmenu/rofi").Default("dmenu").String()
2019-09-07 19:41:41 +02:00
histpath = app.Flag("histpath", "Directory where to save history").Default("~/.local/share/clipman.json").String()
)
2019-03-22 15:13:41 +01:00
func main() {
app.HelpFlag.Short('h')
kingpin.MustParse(app.Parse(os.Args[1:]))
modeCount := 0
if *asDemon {
modeCount++
}
if *asSelector {
modeCount++
}
if modeCount != 1 {
2019-08-02 13:51:13 +02:00
fmt.Println("Missing or incompatible options. You must provide exactly one of these:")
fmt.Println(" -d, --demon")
fmt.Println(" -s, --select")
fmt.Println("See -h/--help for info")
os.Exit(1)
2019-03-22 15:13:41 +01:00
}
2019-09-07 19:41:41 +02:00
// set histfile
histfile := *histpath
if strings.HasPrefix(histfile, "~") {
2019-08-24 21:08:38 +02:00
home, err := os.UserHomeDir()
if err != nil {
log.Fatal(err)
}
2019-09-07 19:41:41 +02:00
histfile = strings.Replace(histfile, "~", home, 1)
2019-03-22 15:13:41 +01:00
}
2019-09-07 19:41:41 +02:00
// read existing history
2019-03-25 19:13:19 +01:00
var history []string
b, err := ioutil.ReadFile(histfile)
2019-09-07 19:41:41 +02:00
if err != nil {
log.Fatalf("Failure reading history file: %s", err)
}
if err := json.Unmarshal(b, &history); err != nil {
log.Fatalf("Failure parsing history: %s", err)
2019-03-22 15:13:41 +01:00
}
if *asDemon {
persist := !*noPersist
listen(history, histfile, persist, *max)
} else if *asSelector {
2019-07-17 10:03:41 +02:00
if len(history) == 0 {
log.Fatal("No history available")
}
if err := selector(history, *max, *tool); err != nil {
2019-03-22 15:13:41 +01:00
log.Fatal(err)
}
}
}