956ca639bba5780dc6272e76b0507c67e93c098b

Author
Robert Estelle <robertestelle@gmail.com>
Committer
Robert Estelle <robertestelle@gmail.com>
Date

Message

clipboard: Avoid unnecessary re-detection each time

Previously, OS detection would happen on each invocation. This makes it
happen once (unless it fails, in which case it will try again on the
next invocation).

This has the additional benefit of localizing the platform-specific
checks and commands, too, versus spreading them out in separate
functions.

Diff

 1diff --git a/lib/clipboard.zsh b/lib/clipboard.zsh
 2index 15ad6d91683d90d040fab53cb5eb851c79e24b69..512a5a2489c792c379ed12164b5558f626187041 100644
 3--- a/lib/clipboard.zsh
 4+++ b/lib/clipboard.zsh
 5@@ -15,26 +15,9 @@
 6 #
 7 #  clipcopy <file>         - copies a file's contents to clipboard
 8 #
 9-function clipcopy() {
10-  emulate -L zsh
11-  local file="${1:-/dev/stdin}"
12-
13-  if [[ $OSTYPE == darwin* ]]; then
14-    pbcopy < "${file}"
15-  elif [[ $OSTYPE == cygwin* ]]; then
16-    cat "${file}" > /dev/clipboard
17-  else
18-    if (( $+commands[xclip] )); then
19-      xclip -in -selection clipboard < "${file}"
20-    elif (( $+commands[xsel] )); then
21-      xsel --clipboard --input  < "${file}"
22-    else
23-      print "clipcopy: Platform $OSTYPE not supported or xclip/xsel not installed" >&2
24-      return 1
25-    fi
26-  fi
27-}
28-
29+#
30+##
31+#
32 # clippaste - "Paste" data from clipboard to stdout
33 #
34 # Usage:
35@@ -52,20 +35,40 @@ function clipcopy() {
36 #
37 #   # Paste to a file
38 #   clippaste > file.txt
39-function clippaste() {
40+#
41+function detect-clipboard() {
42   emulate -L zsh
43+
44   if [[ $OSTYPE == darwin* ]]; then
45-    pbpaste
46+    function clipcopy() { pbcopy < "${1:-/dev/stdin}"; }
47+    function clippaste() { pbpaste; }
48   elif [[ $OSTYPE == cygwin* ]]; then
49-    cat /dev/clipboard
50+    function clipcopy() { cat "${1:-/dev/stdin}" > /dev/clipboard; }
51+    function clippaste() { cat /dev/clipboard; }
52+  elif (( $+commands[xclip] )); then
53+    function clipcopy() { xclip -in -selection clipboard < "${1:-/dev/stdin}"; }
54+    function clippaste() { xclip -out -selection clipboard; }
55+  elif (( $+commands[xsel] )); then
56+    function clipcopy() { xsel --clipboard --input  < "${1:-/dev/stdin}"; }
57+    function clippaste() { xsel --clipboard --output; }
58   else
59-    if (( $+commands[xclip] )); then
60-      xclip -out -selection clipboard
61-    elif (( $+commands[xsel] )); then
62-      xsel --clipboard --output
63-    else
64-      print "clipcopy: Platform $OSTYPE not supported or xclip/xsel not installed" >&2
65-      return 1
66-    fi
67+    function _retry_clipboard_detection_or_fail() {
68+      local clipcmd="${1}"; shift
69+      if detect-clipboard; then
70+        "${clipcmd}" "$@"
71+      else
72+        print "${clipcmd}: Platform $OSTYPE not supported or xclip/xsel not installed" >&2
73+        return 1
74+      fi
75+    }
76+    function clipcopy() { _retry_clipboard_detection_or_fail clipcopy "$@"; }
77+    function cilppaste() { _retry_clipboard_detection_or_fail clippaste "$@"; }
78+    return 1
79   fi
80 }
81+
82+# Detect at startup. A non-zero exit here indicates that the dummy clipboards were set,
83+# which is not really an error. If the user calls them, they will attempt to redetect
84+# (for example, perhaps the user has now installed xclip) and then either print an error
85+# or proceed successfully.
86+detect-clipboard || true