diff options
author | Robert Estelle <robertestelle@gmail.com> | 2019-07-12 17:16:01 -0400 |
---|---|---|
committer | Robert Estelle <robertestelle@gmail.com> | 2019-07-12 17:45:57 -0400 |
commit | 956ca639bba5780dc6272e76b0507c67e93c098b (patch) | |
tree | aa9a7b009a604d2331fa173454dd008f34098c13 /lib/clipboard.zsh | |
parent | d855547661ee4173bd01ab89ad18418d4dbf508a (diff) | |
download | zsh-956ca639bba5780dc6272e76b0507c67e93c098b.tar.gz zsh-956ca639bba5780dc6272e76b0507c67e93c098b.tar.bz2 zsh-956ca639bba5780dc6272e76b0507c67e93c098b.zip |
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.
Diffstat (limited to 'lib/clipboard.zsh')
-rw-r--r-- | lib/clipboard.zsh | 65 |
1 files changed, 34 insertions, 31 deletions
diff --git a/lib/clipboard.zsh b/lib/clipboard.zsh index 15ad6d916..512a5a248 100644 --- a/lib/clipboard.zsh +++ b/lib/clipboard.zsh @@ -15,26 +15,9 @@ # # clipcopy <file> - copies a file's contents to clipboard # -function clipcopy() { - emulate -L zsh - local file="${1:-/dev/stdin}" - - if [[ $OSTYPE == darwin* ]]; then - pbcopy < "${file}" - elif [[ $OSTYPE == cygwin* ]]; then - cat "${file}" > /dev/clipboard - else - if (( $+commands[xclip] )); then - xclip -in -selection clipboard < "${file}" - elif (( $+commands[xsel] )); then - xsel --clipboard --input < "${file}" - else - print "clipcopy: Platform $OSTYPE not supported or xclip/xsel not installed" >&2 - return 1 - fi - fi -} - +# +## +# # clippaste - "Paste" data from clipboard to stdout # # Usage: @@ -52,20 +35,40 @@ function clipcopy() { # # # Paste to a file # clippaste > file.txt -function clippaste() { +# +function detect-clipboard() { emulate -L zsh + if [[ $OSTYPE == darwin* ]]; then - pbpaste + function clipcopy() { pbcopy < "${1:-/dev/stdin}"; } + function clippaste() { pbpaste; } elif [[ $OSTYPE == cygwin* ]]; then - cat /dev/clipboard + function clipcopy() { cat "${1:-/dev/stdin}" > /dev/clipboard; } + function clippaste() { cat /dev/clipboard; } + elif (( $+commands[xclip] )); then + function clipcopy() { xclip -in -selection clipboard < "${1:-/dev/stdin}"; } + function clippaste() { xclip -out -selection clipboard; } + elif (( $+commands[xsel] )); then + function clipcopy() { xsel --clipboard --input < "${1:-/dev/stdin}"; } + function clippaste() { xsel --clipboard --output; } else - if (( $+commands[xclip] )); then - xclip -out -selection clipboard - elif (( $+commands[xsel] )); then - xsel --clipboard --output - else - print "clipcopy: Platform $OSTYPE not supported or xclip/xsel not installed" >&2 - return 1 - fi + function _retry_clipboard_detection_or_fail() { + local clipcmd="${1}"; shift + if detect-clipboard; then + "${clipcmd}" "$@" + else + print "${clipcmd}: Platform $OSTYPE not supported or xclip/xsel not installed" >&2 + return 1 + fi + } + function clipcopy() { _retry_clipboard_detection_or_fail clipcopy "$@"; } + function cilppaste() { _retry_clipboard_detection_or_fail clippaste "$@"; } + return 1 fi } + +# Detect at startup. A non-zero exit here indicates that the dummy clipboards were set, +# which is not really an error. If the user calls them, they will attempt to redetect +# (for example, perhaps the user has now installed xclip) and then either print an error +# or proceed successfully. +detect-clipboard || true |