| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "flag" |
| 6 | "fmt" |
| 7 | "io" |
| 8 | "os" |
| 9 | "time" |
| 10 | |
| 11 | "github.com/gopasspw/clipboard" |
| 12 | ) |
| 13 | |
| 14 | func main() { |
| 15 | ctx := context.Background() |
| 16 | timeout := flag.Duration("t", 0, "Erase clipboard after timeout. Durations are specified like \"20s\" or \"2h45m\". 0 (default) means never erase.") |
| 17 | flag.Parse() |
| 18 | |
| 19 | out, err := io.ReadAll(os.Stdin) |
| 20 | if err != nil { |
| 21 | panic(err) |
| 22 | } |
| 23 | |
| 24 | fmt.Println("Copying to clipboard...") |
| 25 | if err := clipboard.WriteAll(ctx, out); err != nil { |
| 26 | panic(err) |
| 27 | } |
| 28 | |
| 29 | fmt.Println("Waiting for clipboard to be cleared...") |
| 30 | if timeout != nil && *timeout > 0 { |
| 31 | <-time.After(*timeout) |
| 32 | var text string |
| 33 | text, err = clipboard.ReadAllString(ctx) |
| 34 | if err != nil { |
| 35 | os.Exit(1) |
| 36 | } |
| 37 | if text == string(out) { |
| 38 | err = clipboard.WriteAllString(ctx, "") |
| 39 | } |
| 40 | } |
| 41 | if err != nil { |
| 42 | os.Exit(1) |
| 43 | } |
| 44 | fmt.Println("Clipboard cleared.") |
| 45 | } |