X-Git-Url: http://gitweb.pimeys.fr/?a=blobdiff_plain;f=.zsh%2Frc%2Fbase%2F15_functions;fp=.zsh%2Frc%2Fbase%2F15_functions;h=98c97970365b80244105c382263eea4b54a23129;hb=3d71eab53c5b63556363ece880859b0af7f162da;hp=0000000000000000000000000000000000000000;hpb=1f8b08895681d46802368eec0764ca70d08898ca;p=config-20-100.git diff --git a/.zsh/rc/base/15_functions b/.zsh/rc/base/15_functions new file mode 100644 index 0000000..98c9797 --- /dev/null +++ b/.zsh/rc/base/15_functions @@ -0,0 +1,167 @@ +#!/bin/zsh + +# utility functions {{{ +# this function checks if a command exists and returns either true +# or false. This avoids using 'which' and 'whence', which will +# avoid problems with aliases for which on certain weird systems. :-) +# Usage: check_com [-c|-g] word +# -c only checks for external commands +# -g does the usual tests and also checks for global aliases +check_com() { + emulate -L zsh + local -i comonly gatoo + + if [[ $1 == '-c' ]] ; then + (( comonly = 1 )) + shift + elif [[ $1 == '-g' ]] ; then + (( gatoo = 1 )) + else + (( comonly = 0 )) + (( gatoo = 0 )) + fi + + if (( ${#argv} != 1 )) ; then + printf 'usage: check_com [-cg] \n' >&2 + return 1 + fi + + if (( comonly > 0 )) ; then + [[ -n ${commands[$1]} ]] && return 0 + return 1 + fi + + if [[ -n ${commands[$1]} ]] \ + || [[ -n ${functions[$1]} ]] \ + || [[ -n ${aliases[$1]} ]] \ + || [[ -n ${reswords[(r)$1]} ]] ; then + + return 0 + fi + + if (( gatoo > 0 )) && [[ -n ${galiases[$1]} ]] ; then + return 0 + fi + + return 1 +} + +# creates an alias and precedes the command with +# sudo if $EUID is not zero. +salias() { + emulate -L zsh + local only=0 ; local multi=0 + while [[ $1 == -* ]] ; do + case $1 in + (-o) only=1 ;; + (-a) multi=1 ;; + (--) shift ; break ;; + (-h) + printf 'usage: salias [-h|-o|-a] \n' + printf ' -h shows this help text.\n' + printf ' -a replace '\'' ; '\'' sequences with '\'' ; sudo '\''.\n' + printf ' be careful using this option.\n' + printf ' -o only sets an alias if a preceding sudo would be needed.\n' + return 0 + ;; + (*) printf "unkown option: '%s'\n" "$1" ; return 1 ;; + esac + shift + done + + if (( ${#argv} > 1 )) ; then + printf 'Too many arguments %s\n' "${#argv}" + return 1 + fi + + key="${1%%\=*}" ; val="${1#*\=}" + if (( EUID == 0 )) && (( only == 0 )); then + alias -- "${key}=${val}" + elif (( EUID > 0 )) ; then + (( multi > 0 )) && val="${val// ; / ; sudo }" + alias -- "${key}=sudo ${val}" + fi + + return 0 +} + +# Check if we can read given files and source those we can. +xsource() { + emulate -L zsh + if (( ${#argv} < 1 )) ; then + printf 'usage: xsource FILE(s)...\n' >&2 + return 1 + fi + + while (( ${#argv} > 0 )) ; do + [[ -r $1 ]] && source $1 + shift + done + return 0 +} + +# Check if we can read a given file and 'cat(1)' it. +xcat() { + emulate -L zsh + if (( ${#argv} != 1 )) ; then + printf 'usage: xcat FILE\n' >&2 + return 1 + fi + + [[ -r $1 ]] && cat $1 + return 0 +} + +# Remove these functions again, they are of use only in these +# setup files. This should be called at the end of .zshrc. +xunfunction() { + emulate -L zsh + local -a funcs + funcs=(salias xcat xsource xunfunction zrcautoload) + + for func in $funcs ; do + [[ -n ${functions[$func]} ]] \ + && unfunction $func + done + return 0 +} + +#}}} + +# Handles command not fount error +function command_not_found_handler() { + emulate -L zsh + if [[ -x /usr/share/command-not-found/command-not-found ]] ; then + /usr/share/command-not-found/command-not-found $1 + fi + return 1 +} + +# Gets battery percentage. +battery() { + PERCENT="${${"$(acpi 2>/dev/null)"}/(#b)[[:space:]]#Battery <->: [^0-9]##, (<->)%*/${match[1]}}" +} + +# Sets color in BATTCOLOR, designed to be used with RPROMPT. +batcolor() { + battery + if [ ! -z $PERCENT ]; then + if (acpi | head -n 1 | grep "Charging") 2> /dev/null 1>&2; then + BATTCOLOR="%F{cyan}" + else + if [ $PERCENT -gt 65 ]; then + BATTCOLOR="%F{green}" + else + if [ $PERCENT -gt 32 ]; then + BATTCOLOR="%F{yellow}" + else + BATTCOLOR="%F{red}" + fi + fi + fi + BATTERY="${BATTCOLOR} ${PERCENT} %%" + else + BATTERY="" + fi +} +