View previous topic :: View next topic |
Author |
Message |
Bapt Veteran
Joined: 14 Apr 2003 Posts: 1152 Location: Paris
|
Posted: Wed Nov 26, 2008 5:52 pm Post subject: [SCRIPT] un nopaste en pur zsh |
|
|
ça faisait longtemps que je n'avais pas parlé de zsh ici alors voici en un nopaste pour rafb.net écrit en pure zsh par mes soin, il n'en est qu'aux début mais est déjà fonctionnel
http://baptux.free.fr/zpaste
Code: | #!/usr/bin/env zsh
PROG=$0
usage() {
print "usage:\n$PROG [-f file] [-l lang] [-x]"
print ""
print -- "-l lang: \n\t available language: C, CPP, Csharp, Java, Pascal, Perl, PHP, Python, Ruby, SQL"
print -- "-f file: \n\t paste the content of the file"
print -- "-x: \n\t paste from X11 clipboard using xclip"
print ""
print "if no option is given $PROG paste content read from stdin"
exit 1
}
zmodload -i zsh/net/tcp
zurlencode() {
typeset -A substs
URLREPLY=($@)
substs=(
' ' '%20'
' ' '%09'
'!' '%21'
'"' '%22'
'#' '%23'
'$' '%24'
'&' '%26'
"'" '%27'
'(' '%28'
')' '%29'
'+' '%2b'
',' '%2c'
'-' '%2d'
'.' '%2e'
'/' '%2f'
':' '%3a'
';' '%3b'
'<' '%3e'
'>' '%3e'
'?' '%3f'
'@' '%40'
'[' '%5b'
'\' '%5c'
']' '%5d'
'^' '%5e'
'_' '%5f'
'`' '%60'
'{' '%7b'
'|' '%7c'
'}' '%7d'
'~' '%7e'
'€' '%80'
)
URLREPLY=(${URLREPLY//\%/\%25})
for keys (${(k)substs}) URLREPLY=(${URLREPLY//$keys/$substs[$keys]})
URLREPLY=${(j:%0D%0A:)URLREPLY}
}
if [ $# -ne 0 ];then
case $1 in
-*);;
*) usage;;
esac
else
COMPLETEINPUT=()
while read INPUT;do
COMPLETEINPUT+=$INPUT
done
zurlencode $COMPLETEINPUT
fi
while getopts "hl:f:x" opt;do
case $opt in
f)
zurlencode "${(f)"$(<$OPTARG)"}"
;;
l)
case $OPTARG in
C)LANGPASTE="C%20%28C99%29";;
CPP)LANGPASTE="C%2b%2b";;
Csharp)LANGPASTE="C%23";;
Java)LANGPASTE="Java";;
Pascal)LANGPASTE="Pascal";;
Perl)LANGPASTE="Perl";;
PHP)LANGPASTE="PHP";;
Python)LANGPASTE="Python";;
Ruby)LANGPASTE="Ruby";;
SQL)LANGPASTE="SQL";;
*)LANGPASTE="Plain%20Text";;
esac
;;
x)
[[ -x =xclip ]] || print "xclip not found" && exit 1
INPUT=$(xclip -o 2>/dev/null)
zurlencode "$INPUT"
;;
h) usage
;;
*)
usage
;;
esac
done
autoload -U tcp_open
TCP_SILENT=1
tcp_open -q rafb.net 80 rafb || return 2
url="lang=${LANGPASTE:=Plain%20Text}&text=$URLREPLY"
tcp_send -s rafb -- "POST /paste/paste.php HTTP/1.1"
tcp_send -s rafb -- "HOST: rafb.net"
tcp_send -s rafb -- "Accept-Charset: utf-8"
tcp_send -s rafb -- "Content-Type: application/x-www-form-urlencoded"
tcp_send -s rafb -- "Content-Length: $#url"
tcp_send -s rafb -- ""
tcp_send -s rafb -- "$url"
tcp_read -b -d -s rafb
tcp_close -q -s rafb
print ${tcp_lines[5]/*Location: /}
|
cas d'utilisation
Code: |
cat bla | zpaste
zpaste -f bla
zpaste -f bla.c -l C
zpaste -x # Depuis le clipboard X11
|
voila |
|
Back to top |
|
|
kwenspc Advocate
Joined: 21 Sep 2003 Posts: 4954
|
Posted: Thu Dec 04, 2008 11:25 am Post subject: |
|
|
Sympa comme idée. Y aurait-il moyen que le script détermine tout seul le type de langage? Bon je sais pas si ça peut être super fiable... quand on voit que la commande file trouve alternativement qu'un fichier .py est du pascal, du java ou du C++ selon son contenu
(enfin y a sans doute moyen de faire mieux que file j'imagine) _________________ membre officieux du SAV Ati GEntoo |
|
Back to top |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|