From 66438f666c733facffe87f415dbf0a32a5d4bcc5 Mon Sep 17 00:00:00 2001 From: Eryn Wells Date: Wed, 28 Nov 2012 21:44:13 -0800 Subject: [PATCH] Prompt configuration to a prompt theme Move my prompt configuration to a prompt theme function in my $fpath. I call it 'loquacious'. The prompt is configured by the prompt_loquacious_setup function. This change also necessitated a new function called prompt_colorize to generate a string with the correct set of color escapes for the prompt. --- zsh/func/prompt_colorize | 38 +++++++++ zsh/func/prompt_loquacious_setup | 136 +++++++++++++++++++++++++++++++ zshrc | 129 +++++++++-------------------- 3 files changed, 213 insertions(+), 90 deletions(-) create mode 100644 zsh/func/prompt_colorize create mode 100644 zsh/func/prompt_loquacious_setup diff --git a/zsh/func/prompt_colorize b/zsh/func/prompt_colorize new file mode 100644 index 0000000..ecb3cc1 --- /dev/null +++ b/zsh/func/prompt_colorize @@ -0,0 +1,38 @@ +#!/usr/bin/zsh +# Return a prompt string with appropriate color escapes +# Eryn Wells + +local bold=0 +local foreground='' +local background='' + +while getopts 'bf:k:' opt; do + case $opt in + b) bold=1;; + f) foreground=$OPTARG;; + k) background=$OPTARG;; + *) echo "Unknown option: $opt" 1>&2; return -1;; + esac +done + +local str=$@[$OPTIND,${#@}] + +autoload -U is-at-least +if is-at-least '4.3.7'; then + [[ -n "$foreground" ]] && str="%F{$foreground}$str%f" + [[ -n "$background" ]] && str="%K{$background}$str%k" + [[ $bold -eq 1 ]] && str="%B$str%b" +else + local fg_hash bg_hash + if [[ $bold -eq 1 ]]; then + fg_hash='fg_bold' + bg_hash='bg_bold' + else + fg_hash='fg_no_bold' + bg_hash='bg_no_bold' + fi + [[ -n "$foreground" ]] && str="%{\$${fg_hash}[$foreground]%}$str%{\$reset_color%}" + [[ -n "$background" ]] && str="%%{\$${bg_hash}[$background]%}$str%{\$reset_color%}" +fi + +print $str diff --git a/zsh/func/prompt_loquacious_setup b/zsh/func/prompt_loquacious_setup new file mode 100644 index 0000000..113f16b --- /dev/null +++ b/zsh/func/prompt_loquacious_setup @@ -0,0 +1,136 @@ +#!/usr/bin/zsh +# A wordy prompt theme. +# Eryn Wells + + +autoload -U prompt_colorize + + +# +# PROMPT MODULE SCAFFOLDING +# + +function prompt_loquacious_help +{ + cat < at in on + % + +By default, is only shown when the SSH_CONNECTION variable is set; in +other words, when this shell session was opened via an SSH connection. The +prompt will also show the current git branch () if the current +directory is in a git repository. +EOF +} + + +function prompt_loquacious_setup +{ + typeset -ga precmd_functions + typeset -ga preexec_functions + + # preexec_newline is here so there are blank lines on either end of the + # command output + precmd_functions=(preexec_newline \ + precmd_xterm_title \ + precmd_prompt \ + precmd_info \ + precmd_git_branch \ + precmd_flags_rprompt \ + precmd_assemble_prompt) + + preexec_functions=(preexec_newline) + + prompt_opts=(cr subst percent) +} + + +function prompt_loquacious_preview +{ + # TODO: Implement prompt preview. +} + + +# +# PRECMD FUNCTIONS +# + + +function precmd_xterm_title +{ + # Set xterm and screen titles + [[ -n "$DISPLAY" ]] && print -Pn "\e]2;%n@%m\a" +} + + +function precmd_assemble_prompt +{ + local p="$PS1_NAME " + [[ -n "$SSH_CONNECTION" ]] && p+="at $PS1_HOST " + p+="in $PS1_CWD" + if [[ -n "$PS1_REPO" ]]; then + p+=" on $PS1_REPO" + fi + PS1="$p +$PS1_LINE" +} + + +function precmd_flags_rprompt +{ + # background jobs + RPS1="%(1j.[`prompt_colorize -b -f 'magenta' '%j'`].)" + # exit status + RPS1+="%(0?..[`prompt_colorize -b -f 'red' '%?'`])" +} + + +function precmd_info +{ + PS1_NAME=`prompt_colorize -b -f 'magenta' '%n'` + PS1_HOST=`prompt_colorize -b -f 'red' '%m'` + PS1_CWD=`prompt_colorize -b -f 'green' '%~'` +} + + +function precmd_prompt +{ + PS1_LINE="%(!.`prompt_colorize -b -f 'red' '%#'`.%#) " +} + + +function precmd_git_branch +{ + local git_branch_output + git_branch_output=`git branch 2>/dev/null` + if [[ $? -eq 0 ]]; then + export gitbranch=`echo $git_branch_output | grep '^\*' | cut -d' ' -f2` + PS1_REPO=`prompt_colorize -b -f 'cyan' $gitbranch` + else + PS1_REPO='' + fi +} + + +# +# PREEXEC FUNCTIONS +# + +# First prompt flag. See precmd_newline. +is_first_prompt=1 + +function preexec_newline +{ + # Don't print newlines the first time the prompt is displayed. + if [[ -n $is_first_prompt ]]; then + unset is_first_prompt + [[ -z $SSH_CONNECTION ]] && return + fi + echo +} + + +# Finally, run setup to get everything going +prompt_loquacious_setup "$@" diff --git a/zshrc b/zshrc index a11ab26..1f81efe 100644 --- a/zshrc +++ b/zshrc @@ -5,103 +5,37 @@ # # Eryn Wells +PROMPT_THEME='loquacious' + + # load bash/zsh/ksh agnostic configurations [ -e $HOME/.rc ] && source $HOME/.rc -print_info_noisy 1 "Initializing interactive Z Shell" -autoload is-at-least -# # Report seconds since shell was invoked in milliseconds typeset -F SECONDS -print_info_sub_noisy 2 'Setting up prompt' +print_info_noisy 1 "Initializing interactive Z Shell" -is_first_prompt=1 - -prompt_newline() -{ - # Don't print newlines the first time the prompt is displayed. - if [[ -n $is_first_prompt ]]; then - unset is_first_prompt - [[ -z $SSH_CONNECTION ]] && return - fi - echo +# Function path +function { + local myfpath="$HOME/.zsh/func" + fpath=($myfpath/makers $myfpath $fpath) } -precmd_xterm_title() -{ - # Set xterm and screen titles - [[ -n "$DISPLAY" ]] && print -Pn "\e]2;%n@%m\a" -} -if (is-at-least '4.3.7'); then - precmd_prompt() { - PROMPT_LINE="%(!.%B%F{red}%#%F{default}%b.%#) " - } - - precmd_flags_rprompt() { - # background jobs - RPROMPT="%(1j.[%B%F{magenta}%j%F{default}%b].)" - # exit status - RPROMPT+="%(0?..[%B%F{red}%?%F{default}%b])" - } -else - precmd_prompt() { - PROMPT_LINE="%(!.%{$fg_bold[red]%}%#%{$reset_color%}.%#) " - } - - precmd_flags_rprompt() { - # background jobs - RPROMPT="%(1j.[%{$fg_bold[magenta]%}%j%{$reset_color%}].)" - # exit status - RPROMPT+="%(0?..[%{$fg_bold[red]%}%?%{$reset_color%}])" - } -fi - -precmd_info() -{ - PROMPT_NAME='%B%F{magenta}%n%f%b' - PROMPT_HOST='%B%F{red}%m%f%b' - PROMPT_CWD='%B%F{green}%~%f%b' -} - -precmd_git_branch() -{ - local git_branch_output - git_branch_output=`git branch 2>/dev/null` - if [[ $? -eq 0 ]]; then - export gitbranch=`echo $git_branch_output | grep '^\*' | cut -d' ' -f2` - PROMPT_REPO="%B%F{cyan}$gitbranch%f%b" - else - PROMPT_REPO='' - fi -} - -precmd_assemble_prompt() -{ - local p="$PROMPT_NAME " - [[ -n "$SSH_CONNECTION" ]] && p+="on $PROMPT_HOST " - p+="at $PROMPT_CWD" - if [[ -n "$PROMPT_REPO" ]]; then - p+=" on $PROMPT_REPO" - fi - PROMPT="$p -$PROMPT_LINE" -} - -precmd_functions=(precmd_xterm_title prompt_newline \ - precmd_prompt precmd_info precmd_git_branch \ - precmd_flags_rprompt) -preexec_functions=(prompt_newline) +print_info_sub_noisy 2 "Configuring prompt: $PROMPT_THEME" +autoload -U promptinit +promptinit +prompt $PROMPT_THEME -print_info_sub_noisy 2 'Setting options' -# Shell options +print_info_sub_noisy 2 'Setting shell options' setopt \ EXTENDED_GLOB \ MULTIOS + print_info_sub_noisy 3 'Creating aliases' alias pd='pushd' alias pod='popd' @@ -185,12 +119,6 @@ zstyle ':completion:*:*:kill:*' menu yes select # FUNCTIONS ### -# Function path -function { - local myfpath="$HOME/.zsh/func" - fpath=($myfpath/makers $myfpath $fpath) -} - # Generate a password print_info_sub_noisy 3 "Loading pw module" autoload pw @@ -226,7 +154,28 @@ if [ -e $HOME/.zshrc.local ]; then source $HOME/.zshrc.local fi -# Put this down here 'cause some of the local stuff might modify the prompt or -# add functions to $precmd_functions. Doing this here ensures it's always the -# last function to be executed before the prompt is displayed. -precmd_functions+=(precmd_assemble_prompt) +function zle_get_mode { + case "$KEYMAP" in + main|viins) + echo "vi-ins" + ;; + vicmd) + echo "vi-cmd" + ;; + esac +} + +function zle_keymap_select { + local mode=`zle_get_mode` + case "$mode" in + vi-ins) + RPS1="INSERT" + ;; + vi-cmd) + RPS1="COMMAND" + ;; + esac + zle reset-prompt +} + +zle -N zle-keymap-select zle_keymap_select