small cleanup

This commit is contained in:
yory8 2019-09-07 19:41:41 +02:00
parent ef3ebf0135
commit 9a28ad2c85
3 changed files with 18 additions and 18 deletions

View file

@ -43,12 +43,11 @@ func filter(history []string, text string) []string {
func listen(history []string, histfile string, persist bool, max int) { func listen(history []string, histfile string, persist bool, max int) {
for { for {
t, err := exec.Command("wl-paste", "-n", "-t", "text").CombinedOutput() t, err := exec.Command("wl-paste", "-n", "-t", "text").CombinedOutput()
if err != nil { if err != nil {
// wl-paste exits 1 if there's no selection (e.g., when running it at OS startup) // wl-paste exits 1 if there's no selection (e.g., when running it at OS startup)
if string(t) != "No selection\n" { 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) time.Sleep(1 * time.Second)
continue continue
@ -84,16 +83,15 @@ func listen(history []string, histfile string, persist bool, max int) {
history = append(history, text) history = append(history, text)
// dump history to file so that other apps can query it // dump history to file so that other apps can query it
err = write(history, histfile) if err := write(history, histfile); err != nil {
if err != nil { log.Fatalf("Fatal error writing history: %s", err)
log.Fatalf("Fatal error writing history (demon.83): %s", err)
} }
if persist { if persist {
// make the copy buffer available to all applications, // make the copy buffer available to all applications,
// even when the source has disappeared // even when the source has disappeared
if err := exec.Command("wl-copy", []string{"--", text}...).Run(); err != nil { 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)
} }
} }
} }

24
main.go
View file

@ -6,7 +6,7 @@ import (
"io/ioutil" "io/ioutil"
"log" "log"
"os" "os"
"path" "strings"
"gopkg.in/alecthomas/kingpin.v2" "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() 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() 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() 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() 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() { func main() {
@ -39,22 +39,24 @@ func main() {
os.Exit(1) os.Exit(1)
} }
histpath := *histpath // set histfile
if histpath == "$XDG_DATA_HOME" { histfile := *histpath
if strings.HasPrefix(histfile, "~") {
home, err := os.UserHomeDir() home, err := os.UserHomeDir()
if err != nil { if err != nil {
log.Fatal(err) 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 var history []string
b, err := ioutil.ReadFile(histfile) b, err := ioutil.ReadFile(histfile)
if err == nil { if err != nil {
if err := json.Unmarshal(b, &history); err != nil { log.Fatalf("Failure reading history file: %s", err)
log.Fatalf("Failure unmarshaling history (main.38): %s", err) }
} if err := json.Unmarshal(b, &history); err != nil {
log.Fatalf("Failure parsing history: %s", err)
} }
if *asDemon { if *asDemon {

View file

@ -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. // 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 // however, when it sends them back, we need a way to restore them
var escaped []string
guide := make(map[string]string) guide := make(map[string]string)
escaped := []string{}
for _, original := range list { for _, original := range list {
repr := fmt.Sprintf("%#v", original) repr := fmt.Sprintf("%#v", original)