mirror of
https://github.com/ohmyzsh/ohmyzsh.git
synced 2026-01-02 02:19:06 +01:00
Better version of the sprunge command. Its better organized, has better error messages, and the while loop to read from stdin is completely unnecessary, redirecting stdin is more efficient.
This commit is contained in:
parent
65c9751b92
commit
a9fbe5505c
1 changed files with 28 additions and 48 deletions
|
|
@ -1,40 +1,10 @@
|
||||||
# Smart sprunge alias/script.
|
# Smart sprunge alias/script.
|
||||||
#
|
#
|
||||||
# To add the sprunge script to your path, add this to your .zshrc file:
|
|
||||||
#
|
|
||||||
# zstyle :omz:plugins:sprunge add-path on
|
|
||||||
#
|
|
||||||
# Otherwise, a simple alias for sprunge, but only if there isn't a smarter,
|
|
||||||
# better one out there in $PATH, will be added.
|
|
||||||
|
|
||||||
# zstyle -b :omz:plugins:sprunge add-path _plugin__path
|
|
||||||
# if [[ ${_plugin__path} == "on" ]]; then
|
|
||||||
# Plugin setting: Add this plugin directory to the path
|
|
||||||
# export PATH=$PATH:$ZSH/plugins/sprunge
|
|
||||||
# elif [[ -z "${commands[sprunge]}" ]]; then
|
|
||||||
# Nope. No `sprunge` command, period. So, dumb/simple alias, here we go!
|
|
||||||
# alias sprunge="curl -F 'sprunge=<-' http://sprunge.us/"
|
|
||||||
# fi
|
|
||||||
#
|
|
||||||
# Contributed and SLIGHTLY modded by Matt Parnell/ilikenwf <parwok -at- gmail>
|
# Contributed and SLIGHTLY modded by Matt Parnell/ilikenwf <parwok -at- gmail>
|
||||||
# Created by the blogger at the URL below...I don't know where to find his/her name
|
# Created by the blogger at the URL below...I don't know where to find his/her name
|
||||||
# Original found at http://www.shellperson.net/sprunge-pastebin-script/
|
# Original found at http://www.shellperson.net/sprunge-pastebin-script/
|
||||||
|
|
||||||
sprunge () {
|
sprunge_usage() {
|
||||||
local url syntax
|
|
||||||
|
|
||||||
if [[ -t 0 ]]; then
|
|
||||||
if [[ -n "$*" ]]; then
|
|
||||||
if [[ -f "$*" ]]; then
|
|
||||||
# Use python to attempt to detect the syntax
|
|
||||||
syntax=$(echo "try:
|
|
||||||
from pygments.lexers import get_lexer_for_filename
|
|
||||||
print(get_lexer_for_filename('$*').aliases[0])
|
|
||||||
except:
|
|
||||||
print('text')" | python)
|
|
||||||
url=$(curl -s -F 'sprunge=<-' http://sprunge.us < "$*")
|
|
||||||
fi
|
|
||||||
else
|
|
||||||
cat << HERE
|
cat << HERE
|
||||||
|
|
||||||
DESCRIPTION
|
DESCRIPTION
|
||||||
|
|
@ -79,27 +49,37 @@ description, it will NOT generate an error, but will instead treat it as
|
||||||
a text string and upload it.
|
a text string and upload it.
|
||||||
|
|
||||||
HERE
|
HERE
|
||||||
|
}
|
||||||
|
|
||||||
|
sprunge () {
|
||||||
|
local url syntax
|
||||||
|
|
||||||
|
if [[ ! -t 0 ]]; then
|
||||||
|
# We're dumb in this mode. So, dumb syntax highlighting!
|
||||||
|
syntax="text"
|
||||||
|
url=$(curl -s -F 'sprunge=<-' http://sprunge.us <& 0)
|
||||||
|
elif [[ $#argv -eq 0 ]]; then
|
||||||
|
sprunge_usage
|
||||||
|
return 1
|
||||||
|
elif [[ -f $1 ]]; then
|
||||||
|
# Use python to attempt to detect the syntax
|
||||||
|
syntax=$(echo "try:
|
||||||
|
from pygments.lexers import get_lexer_for_filename
|
||||||
|
print(get_lexer_for_filename('$*').aliases[0])
|
||||||
|
except:
|
||||||
|
print('text')" | python)
|
||||||
|
url=$(curl -s -F 'sprunge=<-' http://sprunge.us < $1)
|
||||||
|
else
|
||||||
|
echo "$1 isn't a file"
|
||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
else
|
|
||||||
syntax="text" # We're dumb in this mode. So, dumb syntax highlighting!
|
|
||||||
url=$(while read -r line ; do
|
|
||||||
echo $line
|
|
||||||
done | curl -s -F 'sprunge=<-' http://sprunge.us)
|
|
||||||
fi
|
|
||||||
|
|
||||||
local flags
|
|
||||||
|
|
||||||
# trim whitespaces and add syntax info
|
# trim whitespaces and add syntax info
|
||||||
url=${url//[[:space:]]}
|
url=${url//[[:space:]]}
|
||||||
[[ $syntax != text ]] && url=${url}?${syntax}
|
[[ $syntax != text ]] && url=${url}?${syntax}
|
||||||
|
|
||||||
# if stdout is not a tty, suppress trailing newline
|
|
||||||
# XXX: i don't think this is the right thing to do
|
|
||||||
# [[ ! -t 1 ]] && flags='-n'
|
|
||||||
|
|
||||||
# output
|
# output
|
||||||
echo $flags $url
|
echo $url
|
||||||
|
|
||||||
# don't copy to clipboad if piped
|
# don't copy to clipboad if piped
|
||||||
[[ ! -t 1 ]] && return 0
|
[[ ! -t 1 ]] && return 0
|
||||||
|
|
@ -109,7 +89,7 @@ HERE
|
||||||
echo -n $url | xclip -sel primary
|
echo -n $url | xclip -sel primary
|
||||||
echo -n $url | xclip -sel clipboard
|
echo -n $url | xclip -sel clipboard
|
||||||
elif (( $+commands[xsel] )); then
|
elif (( $+commands[xsel] )); then
|
||||||
echo -n $url | xsel -ip #primary
|
echo -n $url | xsel -ip # primary
|
||||||
echo -n $url | xsel -ib #clipboard
|
echo -n $url | xsel -ib # clipboard
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue