From: Erich Smith Date: Fri, 27 Apr 2012 19:07:04 +0000 (-0400) Subject: import composure functions X-Git-Url: https://source.supwisdom.com/gerrit/gitweb?a=commitdiff_plain;h=a8fcf9f18ceaca27a4b2e8600c51cce8869d69db;p=common%2Fbash_it.git import composure functions --- diff --git a/bash_it.sh b/bash_it.sh old mode 100644 new mode 100755 index 8564c88..cd97ab3 --- a/bash_it.sh +++ b/bash_it.sh @@ -21,6 +21,9 @@ then unset $BASH_THEME; fi +# Load composure first, so we support function metadata +source "${BASH_IT}/lib/composure.sh" + # Load colors first so they can be use in base theme source "${BASH_IT}/themes/colors.theme.bash" source "${BASH_IT}/themes/base.theme.bash" diff --git a/lib/composure.sh b/lib/composure.sh new file mode 100755 index 0000000..b4b740b --- /dev/null +++ b/lib/composure.sh @@ -0,0 +1,213 @@ +#!/bin/bash +# Composure - don't fear the UNIX chainsaw... +# by erichs, 2012 + +# these are a set of light-hearted shell functions that aim to make +# programming the shell easier and more intuitive + +# latest source available at http://git.io/composure + +source_composure () +{ + if [ -z "$EDITOR" ] + then + export EDITOR=vi + fi + + if $(tty -s) # is this a TTY? + then + bind '"\C-j": edit-and-execute-command' + fi + + cite () + { + about () { :; } + about creates a new meta keyword for use in your functions + local keyword=$1 + for keyword in $*; do + eval "function $keyword { :; }" + done + } + + cite about param example + + draft () + { + about wraps last command into a new function + param 1: name to give function + example $ ls + example $ draft list + example $ list + local name=$1 + eval 'function ' $name ' { ' $(fc -ln -1) '; }' + } + + write () + { + about prints function declaration to stdout + param name of function or functions, separated by spaces + example $ write myfunction + example $ write func1 func2 func3 > ~/funcs.sh + local func + for func in $* + do + # trim trailing semicolons generated by declare -f + declare -f $func | sed "s/^\(.*\);$/\1/" + echo + done + } + + revise () + { + about loads function into editor for revision + param name of function or functions, separated by spaces + example $ revise myfunction + example $ revise func1 func2 func3 + local temp=$(mktemp /tmp/revise.XXXX) + write $* > $temp + $EDITOR $temp + eval "$(cat $temp)" + rm $temp + } + + metafor () + { + about prints function metadata associated with keyword + param 1: function name + param 2: meta keyword + example $ metafor reference example + local func=$1 keyword=$2 + write $func | sed -n "s/^ *$keyword \([^([].*\)$/\1/p" + } + + reference () + { + about displays help summary for all functions, or help for specific function + param 1: optional, function name + example $ reference + example $ reference metafor + + printline () + { + local metadata=$1 lhs=${2:- } + + if [[ -z "$metadata" ]] + then + return + fi + + OLD=$IFS; IFS=$'\n' + local line + for line in $metadata + do + printf "%-20s%s\n" $lhs $line + done + IFS=$OLD + } + + help () + { + local func=$1 + + local about="$(metafor $func about)" + printline "$about" $func + + local params="$(metafor $func param)" + if [[ -n "$params" ]] + then + echo "parameters:" + printline "$params" + fi + + local examples="$(metafor $func example)" + if [[ -n "$examples" ]] + then + echo "examples:" + printline "$examples" + fi + + unset printline + } + + if [[ -n "$1" ]] + then + help $1 + else + for func in $(compgen -A function); do + local about="$(metafor $func about)" + printline "$about" $func + done + fi + + unset help printline + } + +} + +install_composure () +{ + echo 'stay calm. installing composure elements...' + + # find our absolute PATH + SOURCE="${BASH_SOURCE[0]}" + while [ -h "$SOURCE" ] + do + SOURCE="$(readlink "$SOURCE")" + done + DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )" + + # vim: automatically chmod +x scripts with #! lines + done_previously () { [ ! -z "$(grep BufWritePost | grep bin | grep chmod)" ]; } + + if [ -f ~/.vimrc ] && ! $(<~/.vimrc done_previously) + then + echo 'vimrc: adding automatic chmod+x for files with shebang (#!) lines...' + echo 'au BufWritePost * if getline(1) =~ "^#!" | if getline(1) =~ "/bin/" | silent execute "!chmod a+x " | endif | endif' >> ~/.vimrc + fi + + # source this file in your startup: .bashrc, or .bash_profile + local done=0 + done_previously () { [ ! -z "$(grep source | grep $DIR | grep composure)" ]; } + + [ -f ~/.bashrc ] && $(<~/.bashrc done_previously) && done=1 + ! (($done)) && [ -f ~/.bash_profile ] && $(<~/.bash_profile done_previously) && done=1 + + if ! (($done)) + then + echo 'sourcing composure from .bashrc...' + echo "source $DIR/$(basename $0)" >> ~/.bashrc + fi + + echo 'composure installed.' +} + +if [[ "$BASH_SOURCE" == "$0" ]] +then + install_composure +else + source_composure + unset install_composure source_composure +fi + +: <