2019-03-22 15:13:41 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2019-09-16 19:39:53 +02:00
|
|
|
"bufio"
|
2019-03-22 15:13:41 +01:00
|
|
|
"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"
|
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 (
|
2019-09-15 12:36:53 +02:00
|
|
|
app = kingpin.New("clipman", "A clipboard manager for Wayland")
|
|
|
|
histpath = app.Flag("histpath", "Path of history file").Default("~/.local/share/clipman.json").String()
|
2019-09-16 19:39:53 +02:00
|
|
|
storer = app.Command("store", "Run from `wl-paste --watch` to record clipboard events")
|
2019-09-15 12:36:53 +02:00
|
|
|
picker = app.Command("pick", "Pick an item from clipboard history")
|
2019-09-16 19:39:53 +02:00
|
|
|
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()
|
2019-09-15 12:36:53 +02:00
|
|
|
maxPicker = picker.Flag("max-items", "scrollview length").Default("15").Int()
|
|
|
|
tool = picker.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')
|
2019-09-15 12:36:53 +02:00
|
|
|
switch kingpin.MustParse(app.Parse(os.Args[1:])) {
|
2019-09-16 19:39:53 +02:00
|
|
|
case "store":
|
2019-09-15 12:36:53 +02:00
|
|
|
persist := !*noPersist
|
2019-09-16 19:39:53 +02:00
|
|
|
|
2019-09-15 12:36:53 +02:00
|
|
|
histfile, history, err := getHistory()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2019-09-16 19:39:53 +02:00
|
|
|
|
|
|
|
// read copy from stdin
|
|
|
|
var stdin []string
|
|
|
|
scanner := bufio.NewScanner(os.Stdin)
|
|
|
|
for scanner.Scan() {
|
|
|
|
stdin = append(stdin, scanner.Text())
|
|
|
|
}
|
|
|
|
if err := scanner.Err(); err != nil {
|
|
|
|
log.Fatal("Error getting input from stdin.")
|
|
|
|
}
|
|
|
|
text := strings.Join(stdin, "\n")
|
|
|
|
|
|
|
|
store(text, history, histfile, *maxDemon, persist)
|
2019-09-15 12:36:53 +02:00
|
|
|
case "pick":
|
|
|
|
_, history, err := getHistory()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := selector(history, *maxPicker, *tool); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2019-03-22 15:13:41 +01:00
|
|
|
}
|
2019-09-15 12:36:53 +02:00
|
|
|
}
|
2019-03-22 15:13:41 +01:00
|
|
|
|
2019-09-15 12:36:53 +02:00
|
|
|
func getHistory() (string, []string, error) {
|
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 {
|
2019-09-15 12:36:53 +02:00
|
|
|
return "", nil, err
|
2019-08-24 21:08:38 +02:00
|
|
|
}
|
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
|
2019-03-22 16:25:09 +01:00
|
|
|
b, err := ioutil.ReadFile(histfile)
|
2019-09-07 19:41:41 +02:00
|
|
|
if err != nil {
|
2019-09-16 12:51:33 +02:00
|
|
|
if !os.IsNotExist(err) {
|
2019-09-15 12:36:53 +02:00
|
|
|
return "", nil, fmt.Errorf("Failure reading history file: %s", err)
|
2019-09-16 12:51:33 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if err := json.Unmarshal(b, &history); err != nil {
|
2019-09-15 12:36:53 +02:00
|
|
|
return "", nil, fmt.Errorf("Failure parsing history: %s", err)
|
2019-09-16 12:51:33 +02:00
|
|
|
}
|
2019-03-22 15:13:41 +01:00
|
|
|
}
|
|
|
|
|
2019-09-15 12:36:53 +02:00
|
|
|
return histfile, history, nil
|
2019-03-22 15:13:41 +01:00
|
|
|
}
|