Commit 956ca639 authored by Robert Estelle's avatar Robert Estelle

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.
parent d8555476
...@@ -15,26 +15,9 @@ ...@@ -15,26 +15,9 @@
# #
# clipcopy <file> - copies a file's contents to clipboard # 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 # clippaste - "Paste" data from clipboard to stdout
# #
# Usage: # Usage:
...@@ -52,20 +35,40 @@ function clipcopy() { ...@@ -52,20 +35,40 @@ function clipcopy() {
# #
# # Paste to a file # # Paste to a file
# clippaste > file.txt # clippaste > file.txt
function clippaste() { #
function detect-clipboard() {
emulate -L zsh emulate -L zsh
if [[ $OSTYPE == darwin* ]]; then if [[ $OSTYPE == darwin* ]]; then
pbpaste function clipcopy() { pbcopy < "${1:-/dev/stdin}"; }
function clippaste() { pbpaste; }
elif [[ $OSTYPE == cygwin* ]]; then elif [[ $OSTYPE == cygwin* ]]; then
cat /dev/clipboard function clipcopy() { cat "${1:-/dev/stdin}" > /dev/clipboard; }
else function clippaste() { cat /dev/clipboard; }
if (( $+commands[xclip] )); then elif (( $+commands[xclip] )); then
xclip -out -selection clipboard function clipcopy() { xclip -in -selection clipboard < "${1:-/dev/stdin}"; }
function clippaste() { xclip -out -selection clipboard; }
elif (( $+commands[xsel] )); then elif (( $+commands[xsel] )); then
xsel --clipboard --output function clipcopy() { xsel --clipboard --input < "${1:-/dev/stdin}"; }
function clippaste() { xsel --clipboard --output; }
else
function _retry_clipboard_detection_or_fail() {
local clipcmd="${1}"; shift
if detect-clipboard; then
"${clipcmd}" "$@"
else else
print "clipcopy: Platform $OSTYPE not supported or xclip/xsel not installed" >&2 print "${clipcmd}: Platform $OSTYPE not supported or xclip/xsel not installed" >&2
return 1 return 1
fi fi
}
function clipcopy() { _retry_clipboard_detection_or_fail clipcopy "$@"; }
function cilppaste() { _retry_clipboard_detection_or_fail clippaste "$@"; }
return 1
fi 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
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment