From 9ba040a977488c39df6ece88dec6dc8b4438244b Mon Sep 17 00:00:00 2001 From: Changwoo Park Date: Thu, 13 Oct 2011 14:19:03 +0900 Subject: [PATCH 1/6] Make $HOSTNAME not necessary for hostname. --- themes/zork/zork.theme.bash | 2 ++ 1 file changed, 2 insertions(+) diff --git a/themes/zork/zork.theme.bash b/themes/zork/zork.theme.bash index f541c0d9..c71f1344 100644 --- a/themes/zork/zork.theme.bash +++ b/themes/zork/zork.theme.bash @@ -53,6 +53,8 @@ prompt() { ;; "pandora") my_ps_host="${red}\h${normal}"; ;; + * ) my_ps_host="${green}\h${normal}"; + ;; esac my_ps_user="\[\033[01;32m\]\u\[\033[00m\]"; From 5659a734456612e15c411693c6d572ce4aa65d7b Mon Sep 17 00:00:00 2001 From: Daniel Leavitt Date: Tue, 24 Apr 2012 23:08:40 -0700 Subject: [PATCH 2/6] Show rbenv active ruby rather than system ruby --- themes/base.theme.bash | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/themes/base.theme.bash b/themes/base.theme.bash index fe112ecf..15bd4aa9 100644 --- a/themes/base.theme.bash +++ b/themes/base.theme.bash @@ -121,7 +121,7 @@ function rvm_version_prompt { function rbenv_version_prompt { if which rbenv &> /dev/null; then - rbenv=$(rbenv global) || return + rbenv=$(rbenv version-name) || return echo -e "$RBENV_THEME_PROMPT_PREFIX$rbenv$RBENV_THEME_PROMPT_SUFFIX" fi } From a8fcf9f18ceaca27a4b2e8600c51cce8869d69db Mon Sep 17 00:00:00 2001 From: Erich Smith Date: Fri, 27 Apr 2012 15:07:04 -0400 Subject: [PATCH 3/6] import composure functions --- bash_it.sh | 3 + lib/composure.sh | 213 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 216 insertions(+) mode change 100644 => 100755 bash_it.sh create mode 100755 lib/composure.sh diff --git a/bash_it.sh b/bash_it.sh old mode 100644 new mode 100755 index 8564c883..cd97ab3e --- 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 00000000..b4b740b5 --- /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 + +: < Date: Sat, 28 Apr 2012 00:43:38 -0400 Subject: [PATCH 4/6] help metadata for base plugin --- plugins/available/base.plugin.bash | 168 ++++++++++++++++++----------- 1 file changed, 103 insertions(+), 65 deletions(-) mode change 100644 => 100755 plugins/available/base.plugin.bash diff --git a/plugins/available/base.plugin.bash b/plugins/available/base.plugin.bash old mode 100644 new mode 100755 index 3edd8ad2..46cde308 --- a/plugins/available/base.plugin.bash +++ b/plugins/available/base.plugin.bash @@ -2,84 +2,114 @@ # For generic functions. -function ips { - ifconfig | grep "inet " | awk '{ print $2 }' +ips () +{ + about display all ip addresses for this host + ifconfig | grep "inet " | awk '{ print $2 }' } -function down4me() { - curl -s "http://www.downforeveryoneorjustme.com/$1" | sed '/just you/!d;s/<[^>]*>//g' +down4me () +{ + about checks whether a website is down for you, or everybody + param 1: website url + example $ down4me http://www.google.com + curl -s "http://www.downforeveryoneorjustme.com/$1" | sed '/just you/!d;s/<[^>]*>//g' } -function myip { - res=$(curl -s checkip.dyndns.org | grep -Eo '[0-9\.]+') - echo -e "Your public IP is: ${echo_bold_green} $res ${echo_normal}" +myip () +{ + about displays your ip address, as seen by the Internet + res=$(curl -s checkip.dyndns.org | grep -Eo '[0-9\.]+') + echo -e "Your public IP is: ${echo_bold_green} $res ${echo_normal}" } -pass() { - which gshuf &> /dev/null - if [ $? -eq 1 ] - then - echo "Error: shuf isn't installed!" - return 1 - fi +pass () +{ + about generates password from random dictionary words + which gshuf &> /dev/null + if [ $? -eq 1 ] + then + echo "Error: shuf isn't installed!" + return 1 + fi - pass=$(shuf -n4 /usr/share/dict/words | tr '\n' ' ') - echo "With spaces (easier to memorize): $pass" - echo "Without (use this as the pass): $(echo $pass | tr -d ' ')" + pass=$(shuf -n4 /usr/share/dict/words | tr '\n' ' ') + echo "With spaces (easier to memorize): $pass" + echo "Without (use this as the pass): $(echo $pass | tr -d ' ')" } -# Function for previewing markdown files in the browser - -function pmdown() { - if command -v markdown &>/dev/null - then - markdown $1 | browser - else - echo "You don't have a markdown command installed!" - fi +pmdown () +{ + about preview markdown file in a browser + param 1: markdown file + example $ pmdown README.md + if command -v markdown &>/dev/null + then + markdown $1 | browser + else + echo "You don't have a markdown command installed!" + fi } -# Make a directory and immediately 'cd' into it - -function mkcd() { - mkdir -p "$*" - cd "$*" +mkcd () +{ + about make a directory and cd into it + param path to create + example $ mkcd foo + example $ mkcd /tmp/img/photos/large + mkdir -p "$*" + cd "$*" } -# Search through directory contents with grep - -function lsgrep(){ - ls | grep "$*" -} - -# View man documentation in Preview -pman () { - man -t "${1}" | open -f -a $PREVIEW +lsgrep () +{ + about search through directory contents with grep + ls | grep "$*" } -pcurl() { - curl "${1}" | open -f -a $PREVIEW +pman () +{ + about view man documentation in Preview + param 1: man page to view + example $ pman bash + man -t "${1}" | open -f -a $PREVIEW } -pri() { - ri -T "${1}" | open -f -a $PREVIEW + +pcurl () +{ + about download file and Preview it + param 1: download URL + example $ pcurl http://www.irs.gov/pub/irs-pdf/fw4.pdf + curl "${1}" | open -f -a $PREVIEW } -quiet() { +pri () +{ + about display information about Ruby classes, modules, or methods, in Preview + param 1: Ruby method, module, or class + example $ pri Array + ri -T "${1}" | open -f -a $PREVIEW +} + +quiet () +{ $* &> /dev/null & } -banish-cookies() { +banish-cookies () +{ + about redirect .adobe and .macromedia files to /dev/null rm -r ~/.macromedia ~/.adobe ln -s /dev/null ~/.adobe ln -s /dev/null ~/.macromedia } -# disk usage per directory -# in Mac OS X and Linux usage () { + about disk usage per directory, in Mac OS X and Linux + param 1: directory name if [ $(uname) = "Darwin" ]; then if [ -n $1 ]; then du -hd $1 @@ -96,25 +126,31 @@ usage () fi } -# One thing todo -function t() { - if [[ "$*" == "" ]] ; then - cat ~/.t - else - echo "$*" > ~/.t - fi +t () +{ + about one thing todo + param if not set, display todo item + param 1: todo text + if [[ "$*" == "" ]] ; then + cat ~/.t + else + echo "$*" > ~/.t + fi } -# Checks for existence of a command -command_exists () { +command_exists () +{ + about checks for existence of a command + param 1: command to check + example $ command_exists ls && echo 'exists' type "$1" &> /dev/null ; } -# List all plugins and functions defined by bash-it -function plugins-help() { - +plugins-help () +{ + about list all plugins and functions defined by bash-it echo "bash-it Plugins Help-Message" - echo + echo set | grep "()" \ | sed -e "/^_/d" | grep -v "BASH_ARGC=()" \ @@ -128,10 +164,12 @@ function plugins-help() { | grep -v "COMPREPLY=()" | sed -e "s/()//" } -# back up file with timestamp # useful for administrators and configs -buf () { - filename=$1 - filetime=$(date +%Y%m%d_%H%M%S) +buf () +{ + about back up file with timestamp + param filename + local filename=$1 + local filetime=$(date +%Y%m%d_%H%M%S) cp ${filename} ${filename}_${filetime} } From e3011c5ead82c62c0eddb9119f0cd4cf5e87ec58 Mon Sep 17 00:00:00 2001 From: Erich Smith Date: Sat, 28 Apr 2012 10:35:01 -0400 Subject: [PATCH 5/6] more portable pass function --- plugins/available/base.plugin.bash | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/plugins/available/base.plugin.bash b/plugins/available/base.plugin.bash index 46cde308..441b3ff2 100755 --- a/plugins/available/base.plugin.bash +++ b/plugins/available/base.plugin.bash @@ -23,17 +23,28 @@ myip () echo -e "Your public IP is: ${echo_bold_green} $res ${echo_normal}" } + +pickfrom () +{ + about picks random line from file + param 1: filename + example $ pickfrom /usr/share/dict/words + local file=$1 + [ -z "$file" ] && reference $FUNCNAME && return + length=$(cat $file | wc -l) + n=$(expr $RANDOM \* $length \/ 32768 + 1) + head -n $n $f | tail -1 +} + pass () { - about generates password from random dictionary words - which gshuf &> /dev/null - if [ $? -eq 1 ] - then - echo "Error: shuf isn't installed!" - return 1 - fi - - pass=$(shuf -n4 /usr/share/dict/words | tr '\n' ' ') + about generates random password from dictionary words + param optional integer length + param if unset, defaults to 4 + example $ pass + example $ pass 6 + local i pass length=${1:-4} + pass=$(echo $(for i in $(eval echo "{1..$length}"); do pickfrom /usr/share/dict/words; done)) echo "With spaces (easier to memorize): $pass" echo "Without (use this as the pass): $(echo $pass | tr -d ' ')" } From 2086a053dfc0e4a94a2139b7cf02bd3b4ecab5de Mon Sep 17 00:00:00 2001 From: Erich Smith Date: Sat, 28 Apr 2012 10:40:16 -0400 Subject: [PATCH 6/6] fix typo --- plugins/available/base.plugin.bash | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/available/base.plugin.bash b/plugins/available/base.plugin.bash index 441b3ff2..0447f337 100755 --- a/plugins/available/base.plugin.bash +++ b/plugins/available/base.plugin.bash @@ -33,7 +33,7 @@ pickfrom () [ -z "$file" ] && reference $FUNCNAME && return length=$(cat $file | wc -l) n=$(expr $RANDOM \* $length \/ 32768 + 1) - head -n $n $f | tail -1 + head -n $n $file | tail -1 } pass ()