Last active
April 13, 2022 20:09
-
-
Save flavio-fernandes/9992ba68182c655ada95dcd3b1f1e196 to your computer and use it in GitHub Desktop.
remoce extra end of the line when used with --clip
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Helper for determining if this is MacOS | |
_is_osx() { | |
uname | grep --quiet -i Darwin && echo 1 || echo 0 | |
} | |
function rhpaste() { | |
# -c will take link generated by rhpaste and put it in clipboard | |
# -d will dump contents of response as a browser would receive | |
# usage rhpaste <filename> or via pipe e.g.: git show|rhpaste | |
# rhpaste file1 file2 | |
# rhpaste file1 -c | |
language=text | |
author=Anonymous | |
expiry=m | |
clip=0 | |
dump=0 | |
local clipCmd='xclip -sel clip' | |
[ $(_is_osx) -eq 1 ] && clipCmd='pbcopy' | |
#set -x | |
local clipCmdShort=$(echo "$clipCmd" | cut -d " " -f1) | |
local redir='>&2 2>/dev/null' | |
local i=1 plain arg opt | |
while [ $i -le $# ]; do | |
eval "arg=\${$i}" | |
local eqopt="$(echo "$arg" | cut -d= -f2-)" | |
if [ -n "$eqopt" ]; then | |
opt="$eqopt" | |
arg="$(echo "$arg" | cut -d= -f1)" | |
doshift="i=$i" | |
else | |
eval "opt=\${$((i+1))}" | |
doshift="i=$((i+1))" | |
fi | |
unset eqopt | |
case "$arg" in | |
-l|--language) language="$opt"; eval "$doshift";; | |
-a|--author) author="$opt"; eval "$doshift";; | |
-e|--expiry) expiry="$opt"; eval "$doshift";; | |
-c|--clip) clip=1;; | |
-d|--dump) dump=1;; | |
--) i=$((i+1)); break;; | |
-) plain="$plain '$arg'";; | |
-*) >&2 echo "rhpaste: Unsupported option '$arg'."; return 3;; | |
*) plain="$plain '$arg'";; | |
esac | |
i=$((i+1)) | |
done | |
while [ $i -le $# ]; do | |
eval "arg=\${$i}" | |
plain="$plain '$arg'" | |
i=$((i+1)) | |
done | |
unset i | |
eval "set ${plain:-''}" | |
if [ "$clip" -eq 1 ]; then | |
if [ -z "$DISPLAY" ] && [ $(_is_osx) -eq 0 ]; then | |
>&2 echo "rhpaste: Can't copy url to clipboard -- DISPLAY is not defined. If you're using ssh, have you enabled X11 forwarding?" | |
return 1 | |
fi | |
if ! command -v ${clipCmdShort} >/dev/null; then | |
>&2 echo "rhpaste: Can't copy url to clipboard -- ${clipCmdShort} is not installed." | |
return 1 | |
fi | |
if [ "$dump" -eq 1 ]; then | |
redir="| ${clipCmd}" | |
else | |
redir=$(echo '2>/dev/null | tr -d "\n" | tee /dev/stderr |' ${clipCmd}) | |
fi | |
elif [ "$dump" -eq 1 ]; then | |
redir='>/dev/null' | |
fi | |
local f | |
for f; do | |
local input="${f:--}" | |
eval 'curl -L -w "%{url_effective}\n" -s -X POST -d "format=${language}&poster=${author}&paste=Send&expiry=${expiry}" http://pastebin.test.redhat.com/pastebin.php --data-urlencode code2@${input:--} -o /dev/stderr' $redir | |
done | |
} | |
# Use clipboard as input instead of pipe/file | |
function rhpastec() { | |
local clipCmd='xclip -o' | |
[ $(_is_osx) -eq 1 ] && clipCmd='pbpaste -Prefer txt' | |
local clipCmdShort=$(echo "$clipCmd" | cut -d " " -f1) | |
if ! command -v ${clipCmdShort} >/dev/null; then | |
>&2 echo "rhpaste: Can't paste from clipboard -- ${clipCmdShort} is not installed." | |
return 1 | |
fi | |
${clipCmd} | rhpaste $@ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment