liverepl

Evaluer ses scripts en temps réel (dangereux ⚠️) - retour accueil

git clone git://bebou.netlib.re/liverepl
Log | Files | Refs | README |

liverepl (1327B)


      1 #! /bin/zsh
      2 
      3 while getopts "fi:o:c:s:" opt;do
      4 	case $opt in
      5 		(i|o|c|s)
      6 			eval "$opt='$OPTARG'";;
      7 		(f)
      8 			filter="yes"
      9 	esac
     10 done
     11 
     12 saveanddisp() { tee $tmpres | pv -s 2k -S -q | head -n$(( $(tput lines) - 2 )) 1>&2 }
     13 
     14 dispprompt() {
     15 	echo -n "\033[$(( $(tput lines) - 1 ))B" 1>&2
     16 	echo -n "\033[92m> \033[0m$entry" 1>&2
     17 	echo -n "\033[H" 1>&2
     18 }
     19 
     20 managekey() {
     21 	case "$key" in
     22 		()       [ "${#entry}" -gt "0" ] && entry="${entry%?}";;
     23 		(|$'\n') break;;
     24 		()       entry="";;
     25 		(*)        entry="$entry$key";;
     26 	esac
     27 }
     28 
     29 entry="";key="";timeout="1"
     30 
     31 [ -n "$i" ] && tmpf="$i" || tmpf=$(mktemp)
     32 [ -n "$o" ] && tmpres="$o" || tmpres=$(mktemp)
     33 
     34 if [ -n "$filter" ];then
     35 	[ -z "$i" -o "$i" = "-" ] && { tmpf=$(mktemp); cat > $tmpf; }
     36 else; timeout="10000"; fi
     37 
     38 # On cache le curseur
     39 echo -n "\033[?25l" 1>&2
     40 
     41 while :;do
     42 	clear
     43 	echo -n "\033[2J" 1>&2
     44 	if [ "$filter" = "yes" ];then
     45 		if [ -n "$c" ];then
     46 			eval < $tmpf $(echo "$c" | sed -E "s\{\}$entry") |
     47 				saveanddisp
     48 		else
     49 			eval < $tmpf "${entry:-cat}" |
     50 				saveanddisp
     51 		fi
     52 	elif [ -n "$c" ];then
     53 		eval $(echo "$c" | sed -E "s\{\}$entry") |
     54 				saveanddisp
     55 	else
     56 		eval "$entry" | saveanddisp
     57 	fi
     58 	key=""
     59 	[ -n "$s" ] && echo "$entry" > $s
     60 	dispprompt; read -t$timeout -k1 key; managekey
     61 done
     62 
     63 clear
     64 # On remet le curseur
     65 echo -n "\033[?25h" 1>&2
     66 cat $tmpres
     67