refac!: switch to subcommands

Breaking change: requires users to change how they call the command

Closes #18
This commit is contained in:
yory8 2019-09-15 12:36:53 +02:00
parent 59334482bb
commit 3d62f25c7a
2 changed files with 37 additions and 40 deletions

68
main.go
View file

@ -12,39 +12,45 @@ import (
) )
var ( var (
app = kingpin.New("clipman", "A clipboard manager for Wayland") 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() histpath = app.Flag("histpath", "Path of history file").Default("~/.local/share/clipman.json").String()
asSelector = app.Flag("select", "Select an item from clipboard history").Short('s').Default("false").Bool() demon = app.Command("listen", "Run as a demon to record clipboard events")
noPersist = app.Flag("no-persist", "Don't persist a copy buffer after a program exits").Short('P').Default("false").Bool() picker = app.Command("pick", "Pick an item from clipboard history")
max = app.Flag("max-items", "history size (with -d) or scrollview length (with -s)").Default("15").Int() noPersist = demon.Flag("no-persist", "Don't persist a copy buffer after a program exits").Short('P').Default("false").Bool()
tool = app.Flag("selector", "Which selector to use: dmenu/rofi/-").Default("dmenu").String() maxDemon = demon.Flag("max-items", "history size").Default("15").Int()
histpath = app.Flag("histpath", "Directory where to save history").Default("~/.local/share/clipman.json").String() maxPicker = picker.Flag("max-items", "scrollview length").Default("15").Int()
tool = picker.Flag("selector", "Which selector to use: dmenu/rofi/-").Default("dmenu").String()
) )
func main() { func main() {
app.HelpFlag.Short('h') app.HelpFlag.Short('h')
kingpin.MustParse(app.Parse(os.Args[1:])) switch kingpin.MustParse(app.Parse(os.Args[1:])) {
modeCount := 0 case "listen":
if *asDemon { persist := !*noPersist
modeCount++ histfile, history, err := getHistory()
} if err != nil {
if *asSelector { log.Fatal(err)
modeCount++ }
} listen(history, histfile, persist, *maxDemon)
if modeCount != 1 { case "pick":
fmt.Println("Missing or incompatible options. You must provide exactly one of these:") _, history, err := getHistory()
fmt.Println(" -d, --demon") if err != nil {
fmt.Println(" -s, --select") log.Fatal(err)
fmt.Println("See -h/--help for info") }
os.Exit(1)
}
if err := selector(history, *maxPicker, *tool); err != nil {
log.Fatal(err)
}
}
}
func getHistory() (string, []string, error) {
// set histfile // set histfile
histfile := *histpath histfile := *histpath
if strings.HasPrefix(histfile, "~") { if strings.HasPrefix(histfile, "~") {
home, err := os.UserHomeDir() home, err := os.UserHomeDir()
if err != nil { if err != nil {
log.Fatal(err) return "", nil, err
} }
histfile = strings.Replace(histfile, "~", home, 1) histfile = strings.Replace(histfile, "~", home, 1)
} }
@ -54,23 +60,13 @@ func main() {
b, err := ioutil.ReadFile(histfile) b, err := ioutil.ReadFile(histfile)
if err != nil { if err != nil {
if !os.IsNotExist(err) { if !os.IsNotExist(err) {
log.Fatalf("Failure reading history file: %s", err) return "", nil, fmt.Errorf("Failure reading history file: %s", err)
} }
} else { } else {
if err := json.Unmarshal(b, &history); err != nil { if err := json.Unmarshal(b, &history); err != nil {
log.Fatalf("Failure parsing history: %s", err) return "", nil, fmt.Errorf("Failure parsing history: %s", err)
} }
} }
if *asDemon { return histfile, history, nil
persist := !*noPersist
listen(history, histfile, persist, *max)
} else if *asSelector {
if len(history) == 0 {
log.Fatal("No history available")
}
if err := selector(history, *max, *tool); err != nil {
log.Fatal(err)
}
}
} }

View file

@ -2,6 +2,7 @@ package main
import ( import (
"fmt" "fmt"
"log"
"os" "os"
"os/exec" "os/exec"
"strconv" "strconv"
@ -9,6 +10,10 @@ import (
) )
func selector(history []string, max int, tool string) error { func selector(history []string, max int, tool string) error {
if len(history) == 0 {
log.Fatal("No history available")
}
// 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] history[i], history[j] = history[j], history[i]
@ -26,10 +31,6 @@ func selector(history []string, max int, tool string) error {
} }
func dmenu(list []string, max int, tool string) (string, error) { func dmenu(list []string, max int, tool string) (string, error) {
if len(list) == 0 {
return "", nil
}
if tool == "-" { if tool == "-" {
escaped, _ := preprocessHistory(list, false) escaped, _ := preprocessHistory(list, false)
os.Stdout.WriteString(strings.Join(escaped, "\n")) os.Stdout.WriteString(strings.Join(escaped, "\n"))