From 2cef84bd8e754595d1e2c5b362216978d9d362e9 Mon Sep 17 00:00:00 2001 From: Shivaprasad Date: Sat, 14 Jul 2018 18:17:00 +0530 Subject: [PATCH 1/5] added watcher functionality with a demo --- README.md | 6 +----- clipboard.go | 30 ++++++++++++++++++++++++++++++ cmd/monitor/main.go | 31 +++++++++++++++++++++++++++++++ 3 files changed, 62 insertions(+), 5 deletions(-) create mode 100644 cmd/monitor/main.go diff --git a/README.md b/README.md index 41fdd57..5d0b61b 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ # Clipboard for Go -Provide copying and pasting to the Clipboard for Go. +Provide copying, pasting and monitoring clipboard functionality for Go. Build: @@ -26,10 +26,6 @@ Notes: * Text string only * UTF-8 text encoding only (no conversion) -TODO: - -* Clipboard watcher(?) - ## Commands: paste shell command: diff --git a/clipboard.go b/clipboard.go index d7907d3..88c932f 100644 --- a/clipboard.go +++ b/clipboard.go @@ -5,6 +5,11 @@ // Package clipboard read/write on clipboard package clipboard +import ( + "context" + "time" +) + // ReadAll read string from clipboard func ReadAll() (string, error) { return readAll() @@ -15,6 +20,31 @@ func WriteAll(text string) error { return writeAll(text) } +// Monitor starts monitoring the clipboard for changes. When +// a change is detected, it is sent over the channel. +func Monitor(ctx context.Context, interval time.Duration, changes chan<- string) error { + defer close(changes) + + currentValue, err := ReadAll() + if err != nil { + return err + } + + for { + select { + case <-ctx.Done(): + return nil + default: + newValue, _ := ReadAll() + if newValue != currentValue { + currentValue = newValue + changes <- currentValue + } + } + time.Sleep(interval) + } +} + // Unsupported might be set true during clipboard init, to help callers decide // whether or not to offer clipboard options. var Unsupported bool diff --git a/cmd/monitor/main.go b/cmd/monitor/main.go new file mode 100644 index 0000000..0a98a91 --- /dev/null +++ b/cmd/monitor/main.go @@ -0,0 +1,31 @@ +package main + +import ( + "context" + "log" + "time" + + "github.com/shivylp/clipboard" +) + +func main() { + changes := make(chan string, 10) + ctx := context.Background() + + go clipboard.Monitor(ctx, time.Second, changes) + + // Watch for changes + for { + select { + case <-ctx.Done(): + break + default: + change, ok := <-changes + if ok { + log.Printf("change received: '%s'", change) + } else { + log.Printf("channel has been closed. exiting..") + } + } + } +} From be3b6bdc0e1508e1df593521ad4c67baa9714ab7 Mon Sep 17 00:00:00 2001 From: Shivaprasad Date: Sat, 14 Jul 2018 18:28:48 +0530 Subject: [PATCH 2/5] removed context package dependency to support older versions of Go --- clipboard.go | 5 ++--- cmd/monitor/main.go | 7 +++---- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/clipboard.go b/clipboard.go index 88c932f..6dec15b 100644 --- a/clipboard.go +++ b/clipboard.go @@ -6,7 +6,6 @@ package clipboard import ( - "context" "time" ) @@ -22,7 +21,7 @@ func WriteAll(text string) error { // Monitor starts monitoring the clipboard for changes. When // a change is detected, it is sent over the channel. -func Monitor(ctx context.Context, interval time.Duration, changes chan<- string) error { +func Monitor(interval time.Duration, stopCh <-chan struct{}, changes chan<- string) error { defer close(changes) currentValue, err := ReadAll() @@ -32,7 +31,7 @@ func Monitor(ctx context.Context, interval time.Duration, changes chan<- string) for { select { - case <-ctx.Done(): + case <-stopCh: return nil default: newValue, _ := ReadAll() diff --git a/cmd/monitor/main.go b/cmd/monitor/main.go index 0a98a91..6638c34 100644 --- a/cmd/monitor/main.go +++ b/cmd/monitor/main.go @@ -1,7 +1,6 @@ package main import ( - "context" "log" "time" @@ -10,14 +9,14 @@ import ( func main() { changes := make(chan string, 10) - ctx := context.Background() + stopCh := make(chan struct{}) - go clipboard.Monitor(ctx, time.Second, changes) + go clipboard.Monitor(time.Second, stopCh, changes) // Watch for changes for { select { - case <-ctx.Done(): + case <-stopCh: break default: change, ok := <-changes From 991082a387141eca9cc860b41d1a3beadfcf982e Mon Sep 17 00:00:00 2001 From: Shivaprasad Bhat Date: Sun, 12 Aug 2018 22:29:18 +0530 Subject: [PATCH 3/5] Removed error from `Monitor` --- clipboard.go | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/clipboard.go b/clipboard.go index 6dec15b..f7d9e7c 100644 --- a/clipboard.go +++ b/clipboard.go @@ -21,18 +21,15 @@ func WriteAll(text string) error { // Monitor starts monitoring the clipboard for changes. When // a change is detected, it is sent over the channel. -func Monitor(interval time.Duration, stopCh <-chan struct{}, changes chan<- string) error { +func Monitor(interval time.Duration, stopCh <-chan struct{}, changes chan<- string) { defer close(changes) - currentValue, err := ReadAll() - if err != nil { - return err - } + currentValue, _ := ReadAll() for { select { case <-stopCh: - return nil + return default: newValue, _ := ReadAll() if newValue != currentValue { From bbf605fceedd7aa701cad551cbf9471600a171f2 Mon Sep 17 00:00:00 2001 From: Shivaprasad Date: Sun, 12 Aug 2018 22:42:21 +0530 Subject: [PATCH 4/5] reverted back removal of error --- clipboard.go | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/clipboard.go b/clipboard.go index f7d9e7c..6dec15b 100644 --- a/clipboard.go +++ b/clipboard.go @@ -21,15 +21,18 @@ func WriteAll(text string) error { // Monitor starts monitoring the clipboard for changes. When // a change is detected, it is sent over the channel. -func Monitor(interval time.Duration, stopCh <-chan struct{}, changes chan<- string) { +func Monitor(interval time.Duration, stopCh <-chan struct{}, changes chan<- string) error { defer close(changes) - currentValue, _ := ReadAll() + currentValue, err := ReadAll() + if err != nil { + return err + } for { select { case <-stopCh: - return + return nil default: newValue, _ := ReadAll() if newValue != currentValue { From 7efa4d7fc65468c657492bbf4c567e3dbbced9fb Mon Sep 17 00:00:00 2001 From: Shivaprasad Date: Tue, 23 Oct 2018 15:24:04 +0530 Subject: [PATCH 5/5] Changed username in appropriate places --- cmd/monitor/main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/monitor/main.go b/cmd/monitor/main.go index 6638c34..c157732 100644 --- a/cmd/monitor/main.go +++ b/cmd/monitor/main.go @@ -4,7 +4,7 @@ import ( "log" "time" - "github.com/shivylp/clipboard" + "github.com/spy16/clipboard" ) func main() {