common.zsh (1849B)
1 setopt no_unset warn_create_global # be strict 2 setopt extended_glob # allow glob specifiers 3 4 # nice PS4 with ellapsed seconds (to two decimal places) {{{1 5 zmodload zsh/system # to get actual pid 6 setopt PROMPT_SUBST 7 if [[ ${TERM:-dumb} == (xterm|rxvt|screen|linux|console|Eterm|putty)* ]]; then 8 # first color is for main shell process, second is subshell 9 PS4_PID_COLORS=(cyan magenta) 10 PS4='+%B${SECONDS} %F{${PS4_PID_COLORS[1+($$ != ${sysparams[pid]})]}}%N%f:%F{yellow}%i%f>%b ' 11 else 12 PS4='+%B${SECONDS} ${sysparams[pid]} %N:%i>%b ' 13 fi 14 15 # two digits after the decimal point 16 typeset -g -F 2 SECONDS 17 18 ### Color and text definition {{{1 19 20 typeset -g hl_fatal hl_reset 21 22 # die messages 23 if (( $terminfo[colors] >= 8 )); then 24 hl_fatal='%F{red}%B'; hl_fatal=${(%)hl_fatal} 25 hl_warn='%F{yellow}%B'; hl_warn=${(%)hl_warn} 26 hl_reset='%b%f'; hl_reset=${(%)hl_reset} 27 fi 28 29 ### Utility functions {{{1 30 err_msg() { 31 local first=$1 32 shift 33 printf >&2 '%s%s%s%s\n' "$hl_fatal" "$first" "$hl_reset" "$*" 34 } 35 36 warn_msg() { 37 local first=$1 38 shift 39 printf >&2 '%s%s%s%s\n' "$hl_warn" "$first" "$hl_reset" "$*" 40 } 41 42 # helper that prints out stack, error message and exits 43 die_ret() { 44 set +x 45 local ret n 46 ret=$1 47 shift 48 print -r - >&2 "${hl_fatal}Fatal$hl_reset error occurend in:" 49 for n in {${#funcfiletrace}..1}; do 50 printf >&2 '%d> %s (%s)\n' $n "$funcfiletrace[$n]" "$functrace[$n]" 51 done 52 printf >&2 '%s\n' "${hl_fatal}*$hl_reset $^@" 53 exit $ret 54 } 55 56 die() { 57 set +x 58 die_ret 1 "$@" 59 } 60 die100() { # 100: wrong usage 61 set +x 62 die_ret 100 "$@" 63 } 64 die111() { # 111: system call failed 65 set +x 66 die_ret 111 "$@" 67 } 68 69 -() { # Run command and die on nonzero exitcode 70 "$@" || die_ret $? "command failed with exitcode $?: ${(j: :)${(q)@}}" 71 } 72 73