zenu

Des menus dans votre terminal - retour accueil

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

zenu.zsh (1411B)


      1 zenu_store=$( mktemp -d /dev/shm/zenu.$$.XXX )
      2 trap "rm -rf $zenu_store" EXIT
      3 
      4 zenu.up() {
      5 	repeat ${1:-1} printf "\x1b2K\e[A"
      6 	printf "\e[2K"
      7 }
      8 
      9 zenu.emph() {
     10 	sed -r '
     11 		/^'"$zenu_key"'(.*)$/ {
     12 			s//\x1b[7m\1\x1b[m/
     13 			n
     14 		}
     15 		s/^.//
     16 	' $zenu_base/$zenu_stack[-1].emph > $zenu_store/$#zenu_stack
     17 }
     18 
     19 zenu+() {
     20 	zenu.emph 
     21 	zenu_stack+=$1
     22 }
     23 
     24 zenu--() {
     25 	(( $#zenu_stack )) && zenu_stack[-1]=()
     26 }
     27 
     28 zenu.update() {
     29 	set $zenu_base/$zenu_stack
     30 	cat $1.face
     31 	. $1.script
     32 }
     33 
     34 # TODO: doc
     35 # ZENU_HOOK_END: a code to execute while leaving the loop
     36 # ZENU_AGAIN (bool) : run the loop again
     37 
     38 zenu.ends_with() {
     39 	ZENU_HOOK_END="$*"
     40 	ZENU_AGAIN=false
     41 }
     42 
     43 zenu.loop() {
     44 	local zenu_stack=( main )
     45 	local ZENU_AGAIN=true
     46 	local zenu_base= zenu_key=
     47 	while (( $# )) {
     48 		case "$1"
     49 		in (from) zenu_stack=($2)              ; shift 2
     50 		;; (in)
     51 			[[ -d $2 ]] || {
     52 				>&2 echo "$2 isn't a directory"
     53 				return -1
     54 			}
     55 			zenu_base=$2
     56 			shift 2
     57 		;; (*)    >&2 echo "not a valid key $1"; return 1
     58 		esac
     59 	}
     60 	while {eval $ZENU_AGAIN} {
     61 		clear
     62 		case $#zenu_stack
     63 		in (0) return
     64 		;; (1) zenu.update
     65 		;; (*)
     66 			local c=${zenu_base?path of this zenu}/$zenu_stack[-1]
     67 			set $zenu_store/{1..$[$#zenu_stack-1]} $c.face
     68 			paste "$@" | sed 's/\t/│/g'
     69 			echo
     70 			test -s $zenu_store/err && {
     71 				echo -n $'\e[31;47m'
     72 				cat -- $zenu_store/err
     73 				echo -n $'\e[0m'
     74 			} >&2
     75 			. $c.script
     76 		esac
     77 	}
     78 	eval "${ZENU_HOOK_END:-:}"
     79 }
     80