2019-03-22 15:13:41 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"path"
|
2019-03-22 16:25:09 +01:00
|
|
|
|
|
|
|
"gopkg.in/alecthomas/kingpin.v2"
|
2019-03-22 15:13:41 +01:00
|
|
|
)
|
|
|
|
|
2019-03-22 16:25:09 +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()
|
2019-03-23 11:20:58 +01:00
|
|
|
noPersist = app.Flag("no-persist", "Don't persist a copy buffer after a program exits").Short('P').Default("false").Bool()
|
2019-03-23 12:49:17 +01:00
|
|
|
max = app.Flag("max-items", "items to store in history (with -d) or display before scrolling (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-03-22 16:25:09 +01:00
|
|
|
)
|
2019-03-22 15:13:41 +01:00
|
|
|
|
2019-03-22 16:25:09 +01:00
|
|
|
func main() {
|
|
|
|
app.HelpFlag.Short('h')
|
|
|
|
kingpin.MustParse(app.Parse(os.Args[1:]))
|
2019-08-01 18:54:31 +02:00
|
|
|
modeCount := 0
|
|
|
|
if *asDemon {
|
|
|
|
modeCount++
|
|
|
|
}
|
|
|
|
if *asSelector {
|
|
|
|
modeCount++
|
|
|
|
}
|
|
|
|
if modeCount != 1 {
|
|
|
|
log.Print("Missing or incompatible options. You must provide exactly one of these:")
|
|
|
|
log.Print(" -d, --demon")
|
|
|
|
log.Print(" -s, --select")
|
|
|
|
log.Fatal("See -h/--help for info")
|
2019-03-22 15:13:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
h, err := os.UserHomeDir()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2019-03-25 19:13:19 +01:00
|
|
|
histfile := path.Join(h, ".local/share/clipman.json")
|
2019-03-22 15:13:41 +01:00
|
|
|
|
2019-03-25 19:13:19 +01:00
|
|
|
var history []string
|
2019-03-22 16:25:09 +01:00
|
|
|
b, err := ioutil.ReadFile(histfile)
|
2019-03-22 15:13:41 +01:00
|
|
|
if err == nil {
|
|
|
|
if err := json.Unmarshal(b, &history); err != nil {
|
2019-04-16 14:21:36 +02:00
|
|
|
log.Fatalf("Failure unmarshaling history (main.38): %s", err)
|
2019-03-22 15:13:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-22 16:25:09 +01:00
|
|
|
if *asDemon {
|
2019-03-23 11:20:58 +01:00
|
|
|
persist := !*noPersist
|
2019-07-17 09:22:22 +02:00
|
|
|
listen(history, histfile, persist, *max)
|
2019-03-22 16:25:09 +01:00
|
|
|
} else if *asSelector {
|
2019-07-17 10:03:41 +02:00
|
|
|
if len(history) == 0 {
|
|
|
|
log.Fatal("No history available")
|
|
|
|
}
|
2019-05-07 11:59:09 +02:00
|
|
|
if err := selector(history, *max, *tool); err != nil {
|
2019-03-22 15:13:41 +01:00
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|