From 8ebe1788bfc121607cf930f3e795083b92477c07 Mon Sep 17 00:00:00 2001 From: Eryn Wells Date: Thu, 29 Nov 2012 15:18:57 -0800 Subject: [PATCH] New message/log functions --- env | 7 ++++--- profile | 2 +- rc | 12 ++++++------ shell-functions | 44 +++++++++++++++++++++++++++++++++----------- zprofile | 2 +- zshrc | 31 +++++++++++++++---------------- 6 files changed, 60 insertions(+), 38 deletions(-) diff --git a/env b/env index f0b78f9..c575a21 100644 --- a/env +++ b/env @@ -14,7 +14,8 @@ export SYS=`uname -s | tr A-Z a-z` # Set this to a non-zero integer to see startup messages export NOISY=0 -print_info_noisy 1 'Initializing environment' +print_heading -l 1 'Initializing environment' + PAGER="less" MANPAGER=$PAGER EDITOR="vim" @@ -38,7 +39,7 @@ PATH=/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin export PATH -print_info_sub_noisy 2 "Setting up $SYS environment" +print_info -l 2 "Setting up $SYS environment" case $SYS in darwin) export PATH=$HOME/Library/Python/2.7/bin:$PATH @@ -58,6 +59,6 @@ esac # Local environment settings if [ -e $HOME/.env.local ]; then - print_info_noisy 2 "Sourcing local environment setup" + print_info -l 2 "Sourcing local environment setup" source $HOME/.env.local fi diff --git a/profile b/profile index cbcc10e..f3660a4 100644 --- a/profile +++ b/profile @@ -4,7 +4,7 @@ # Start SSH agent for password-less logins if [ -z "$SSH_AUTH_SOCK" -a -x "$SSHAGENT" ]; then - print_info_noisy 1 'Starting ssh-agent' + print_info -l 1 'Starting ssh-agent' eval `ssh-agent -s` trap "kill $SSH_AGENT_PID" 0 fi diff --git a/rc b/rc index 6f2d738..89849e4 100644 --- a/rc +++ b/rc @@ -3,9 +3,9 @@ # Generic interactive shell setup # Eryn Wells -print_info_noisy 1 'Initializing interactive shell' +print_heading -l 1 'Initializing interactive shell' -print_info_sub_noisy 2 'Creating aliases' +print_info -l 2 'Creating aliases' alias j='jobs' alias h='history' alias df='df -h' @@ -28,7 +28,7 @@ alias pprint="python -c 'import sys,pprint; pprint.pprint(eval(sys.stdin.read()) alias pprint-json="python -c 'import sys,json;print json.dumps(json.load(sys.stdin), indent=2)'" -print_info_sub_noisy 2 "Sourcing ${SYS}-specific settings" +print_info -l 2 "Sourcing ${SYS}-specific settings" case $SYS in darwin) alias acls='command ls -le' @@ -69,7 +69,7 @@ function { ;; esac - print_info_sub_noisy 3 "Setting up ls: `which $ls`" + print_info_sub -l 3 "Setting up ls: `which $ls`" alias ls="$ls $ls_options" alias la="$ls -A $ls_options" alias ll="$ls -l $ls_options" @@ -81,7 +81,7 @@ function { else dircolors=$HOME/.dircolors/default.cfg fi - print_info_sub_noisy 3 "Setting up dircolors: `basename $dircolors`" + print_info_sub -l 3 "Setting up dircolors: `basename $dircolors`" eval `dircolors $dircolors` fi } @@ -95,6 +95,6 @@ binary_exists nethack && export NETHACKOPTIONS="color" LEDGER_FILE=$HOME/Documents/Financial/personal.ledger if [ -e $HOME/.rc.local ]; then - print_info_noisy 2 "Sourcing local settings for interactive shells" + print_info -l 2 'Sourcing local interactive shell setup' source $HOME/.rc.local fi diff --git a/shell-functions b/shell-functions index 532616f..6476a58 100644 --- a/shell-functions +++ b/shell-functions @@ -5,20 +5,42 @@ # Print prettier, more uniform messages -function print_msg { print -P "%F{$1}==>%f $2" } -function print_info { print_msg 'blue' "$@" } -function print_error { print_msg 'red' "$@" } +function print_msg { + # Level 0: always show + local -i level=0 bold=0 + local prefix message + local foreground background -function print_msg_sub { print -P " %F{$1}*%f $2" } -function print_info_sub { print_msg_sub 'blue' "$@" } -function print_error_sub { print_msg_sub 'error' "$@" } + while getopts 'bl:f:k:p:' opt; do + case $opt in + b) bold=1;; + l) level="$OPTARG";; + f) foreground="$OPTARG";; + k) background="$OPTARG";; + p) prefix="$OPTARG";; + esac + done -# Print if $NOISY is set -function print_info_noisy { [ ${NOISY:-0} -ge $1 ] && print_info $@[2,-1] } -function print_error_noisy { [ ${NOISY:-0} -ge $1 ] && print_error $@[2,-1] } + message=$@[$OPTIND,${#@}] -function print_info_sub_noisy { [ ${NOISY:-0} -ge $1 ] && print_info_sub $@[2,-1] } -function print_error_sub_noisy { [ ${NOISY:-0} -ge $1 ] && print_error_sub $@[2,-1] } + (( bold )) && message="%B${message}%b" + [[ -n "$foreground" ]] && message="%F{$foreground}$message%f" + [[ -n "$background" ]] && message="%K{$background}$message%k" + [[ -n "$prefix" ]] && message="$prefix $message" + + [[ ${NOISY:-0} -ge $level ]] && print -P "${message}" +} + +function print_info { print_msg -p '%F{blue}==>%f' "$@" } +function print_error { print_msg -p '%F{red}==>%f' "$@" } + +function print_heading { print_msg -b "$@" } +function print_info_heading { print_info -b "$@" } +function print_error_heading { print_error -b "$@" } + +function print_msg_sub { print_msg -p " *" "$@" } +function print_info_sub { print_msg_sub -p ' %F{blue}*%f' "$@" } +function print_error_sub { print_msg_sub -p ' %F{error}*%f' "$@" } function binary_exists { return $(hash $1 1>/dev/null 2>&1) } function binary_not_exists { binary_exists $1; return $(( ! $? )) } diff --git a/zprofile b/zprofile index bd45fbc..f19a0d7 100644 --- a/zprofile +++ b/zprofile @@ -5,7 +5,7 @@ # # Eryn Wells -print_info_noisy 1 'Initializing login shell' +print_heading -l 1 'Initializing login shell' [ -e $HOME/.profile ] && source $HOME/.profile diff --git a/zshrc b/zshrc index 6a8d8a7..caf5d83 100644 --- a/zshrc +++ b/zshrc @@ -12,17 +12,17 @@ ZLE_MODE='vim' # load bash/zsh/ksh agnostic configurations [ -e $HOME/.rc ] && source $HOME/.rc -print_info_noisy 1 "Initializing interactive Z Shell" +print_heading -l 1 "Initializing interactive Z Shell" function configure_general #{{{ { - print_info_sub_noisy 2 'Configuring general ZSH settings' + print_info -l 2 'Configuring general ZSH settings' # Report seconds since shell was invoked in milliseconds typeset -F SECONDS - print_info_sub_noisy 2 'Setting shell options' + print_info_sub -l 3 'Setting shell options' # See zshoptions(1) setopt EXTENDED_GLOB \ MULTIOS \ @@ -33,9 +33,9 @@ function configure_general #{{{ function configure_zle #{{{ { - print_info_sub_noisy 2 'Configuring ZLE' + print_info -l 2 'Configuring ZLE' - print_info_sub_noisy 5 "Using $ZLE_MODE command line editing mode" + print_info_sub -l 5 "Using $ZLE_MODE command line editing mode" [[ -z "$ZLE_MODE" ]] && ZLE_MODE='vim' if [[ $ZLE_MODE == 'vim' ]]; then bindkey -v @@ -55,26 +55,26 @@ function configure_zle #{{{ function configure_modules_and_functions #{{{ { - print_info_noisy 2 'Loading modules' + print_info -l 2 'Loading modules' local myfpath="$HOME/.zsh/func" - print_info_sub_noisy 2 "Adding $myfpath to \$fpath" + print_info_sub -l 2 "Adding $myfpath to \$fpath" fpath=($myfpath/makers $myfpath $fpath) - print_info_sub_noisy 3 'Loading vcs_info' + print_info -l 3 'Loading vcs_info' autoload -Uz vcs_info zstyle ':vcs_info:*' enable git load_module 'makers' - print_info_sub_noisy 3 "Loading pw module" + print_info -l 3 "Loading pw module" autoload pw } #}}} function configure_prompt #{{{ { - print_info_sub_noisy 2 "Configuring prompt: $PROMPT_THEME" + print_info -l 2 "Configuring prompt: $PROMPT_THEME" autoload -U promptinit promptinit prompt $PROMPT_THEME @@ -83,7 +83,7 @@ function configure_prompt #{{{ function configure_zsh_aliases #{{{ { - print_info_sub_noisy 3 'Creating ZSH-specific aliases' + print_info -l 3 'Creating ZSH-specific aliases' alias pd='pushd' alias pod='popd' @@ -101,7 +101,7 @@ function configure_zsh_aliases #{{{ function configure_history #{{{ { - print_info_sub_noisy 4 'Setting up history' + print_info -l 2 'Setting up history' setopt \ APPEND_HISTORY \ EXTENDED_HISTORY \ @@ -120,7 +120,7 @@ function configure_history #{{{ function configure_completion #{{{ { - print_info_sub_noisy 2 'Initializing completion system' + print_info -l 2 'Initializing completion system' autoload -U compinit compinit @@ -151,7 +151,6 @@ function configure_completion #{{{ configure_general configure_zle -configure_fpath configure_modules_and_functions configure_zsh_aliases configure_history @@ -160,11 +159,11 @@ configure_prompt if [ -e $HOME/.zshrc.$SYS ]; then - print_info_noisy 3 "Sourcing ${SYS}-specific Z Shell settings" + print_info -l 3 "Sourcing ${SYS}-specific Z Shell settings" source $HOME/.zshrc.$SYS fi if [ -e $HOME/.zshrc.local ]; then - print_info_noisy 3 "Sourcing local Z Shell settings" + print_info -l 3 "Sourcing local Z Shell settings" source $HOME/.zshrc.local fi