diff --git a/demon.go b/demon.go index 05d9a39..54478dd 100644 --- a/demon.go +++ b/demon.go @@ -43,12 +43,11 @@ func filter(history []string, text string) []string { func listen(history []string, histfile string, persist bool, max int) { for { - t, err := exec.Command("wl-paste", "-n", "-t", "text").CombinedOutput() if err != nil { // wl-paste exits 1 if there's no selection (e.g., when running it at OS startup) if string(t) != "No selection\n" { - log.Printf("Error running wl-paste (demon.52): %s", t) + log.Printf("Error running wl-paste: %s", t) } time.Sleep(1 * time.Second) continue @@ -84,16 +83,15 @@ func listen(history []string, histfile string, persist bool, max int) { history = append(history, text) // dump history to file so that other apps can query it - err = write(history, histfile) - if err != nil { - log.Fatalf("Fatal error writing history (demon.83): %s", err) + if err := write(history, histfile); err != nil { + log.Fatalf("Fatal error writing history: %s", err) } if persist { // make the copy buffer available to all applications, // even when the source has disappeared if err := exec.Command("wl-copy", []string{"--", text}...).Run(); err != nil { - log.Printf("Error running wl-copy (demon.91): %s", err) + log.Printf("Error running wl-copy: %s", err) } } } diff --git a/main.go b/main.go index b814666..d622f42 100644 --- a/main.go +++ b/main.go @@ -6,7 +6,7 @@ import ( "io/ioutil" "log" "os" - "path" + "strings" "gopkg.in/alecthomas/kingpin.v2" ) @@ -16,9 +16,9 @@ var ( 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() - max = app.Flag("max-items", "items to store in history (with -d) or display before scrolling (with -s)").Default("15").Int() + max = app.Flag("max-items", "history size (with -d) or scrollview length (with -s)").Default("15").Int() tool = app.Flag("selector", "Which selector to use: dmenu/rofi").Default("dmenu").String() - histpath = app.Flag("histpath", "Directory where to save history").Default("$XDG_DATA_HOME").String() + histpath = app.Flag("histpath", "Directory where to save history").Default("~/.local/share/clipman.json").String() ) func main() { @@ -39,22 +39,24 @@ func main() { os.Exit(1) } - histpath := *histpath - if histpath == "$XDG_DATA_HOME" { + // set histfile + histfile := *histpath + if strings.HasPrefix(histfile, "~") { home, err := os.UserHomeDir() if err != nil { log.Fatal(err) } - histpath = path.Join(home, ".local/share/") + histfile = strings.Replace(histfile, "~", home, 1) } - histfile := path.Join(histpath, "clipman.json") + // read existing history var history []string b, err := ioutil.ReadFile(histfile) - if err == nil { - if err := json.Unmarshal(b, &history); err != nil { - log.Fatalf("Failure unmarshaling history (main.38): %s", err) - } + 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) } if *asDemon { diff --git a/selector.go b/selector.go index 7d2af44..e93aed7 100644 --- a/selector.go +++ b/selector.go @@ -49,8 +49,8 @@ func dmenu(list []string, max int, tool string) (string, error) { // dmenu will break if items contain newlines, so we must pass them as literals. // however, when it sends them back, we need a way to restore them + var escaped []string guide := make(map[string]string) - escaped := []string{} for _, original := range list { repr := fmt.Sprintf("%#v", original)