From ef911aacb73ec47b9971d283b15fa69b5053697d Mon Sep 17 00:00:00 2001 From: Sebastian Tramp Date: Mon, 3 Jan 2011 00:12:13 +0100 Subject: [PATCH] new prompt and re-order --HG-- rename : aliases => aliases.zsh rename : completion => completion.zsh rename : functions => functions.zsh rename : options => options.zsh rename : prompt => prompt.zsh --- aliases => aliases.zsh | 6 +- colors.zsh | 36 +++++++ completion => completion.zsh | 0 functions => functions.zsh | 9 ++ options => options.zsh | 0 prompt | 178 ----------------------------------- prompt.zsh | 107 +++++++++++++++++++++ zshrc | 13 +-- 8 files changed, 162 insertions(+), 187 deletions(-) rename aliases => aliases.zsh (92%) create mode 100644 colors.zsh rename completion => completion.zsh (100%) rename functions => functions.zsh (83%) rename options => options.zsh (100%) delete mode 100644 prompt create mode 100644 prompt.zsh diff --git a/aliases b/aliases.zsh similarity index 92% rename from aliases rename to aliases.zsh index ac54ae7..ff5d548 100644 --- a/aliases +++ b/aliases.zsh @@ -4,9 +4,9 @@ # alias definitions which can be edited/modified with 'aedit' # -alias aedit=" $EDITOR ~/.zsh/aliases; source ~/.zsh/aliases" -alias fedit=" $EDITOR ~/.zsh/functions; source ~/.zsh/functions" -alias pedit=" $EDITOR ~/.zsh/private; source ~/.zsh/private" +alias aedit=" $EDITOR ~/.zsh/aliases.zsh; source ~/.zsh/aliases.zsh" +alias fedit=" $EDITOR ~/.zsh/functions.zsh; source ~/.zsh/functions.zsh" +alias pedit=" $EDITOR ~/.zsh/private.zsh; source ~/.zsh/private.zsh" alias vedit=" $EDITOR ~/.vimrc" #alias man="unset PAGER; man" diff --git a/colors.zsh b/colors.zsh new file mode 100644 index 0000000..affec11 --- /dev/null +++ b/colors.zsh @@ -0,0 +1,36 @@ +#! /bin/zsh +# A script to make using 256 colors in zsh less painful. +# P.C. Shyamshankar +# Copied from http://github.com/sykora/etc/blob/master/zsh/functions/spectrum/ + +typeset -Ag FX FG BG + +FX=( + reset "%{%}" + bold "%{%}" no-bold "%{%}" + italic "%{%}" no-italic "%{%}" + underline "%{%}" no-underline "%{%}" + blink "%{%}" no-blink "%{%}" + reverse "%{%}" no-reverse "%{%}" +) + +for color in {000..255}; do + FG[$color]="%{[38;5;${color}m%}" + BG[$color]="%{[48;5;${color}m%}" +done + + +# See if we can use colors. +# Original from Phil Gold at http://aperiodic.net/phil/prompt/ +autoload colors zsh/terminfo +if [[ "$terminfo[colors]" -ge 8 ]]; then + colors +fi +for color in RED GREEN YELLOW BLUE MAGENTA CYAN WHITE; do + eval PR_$color='%{$terminfo[bold]$fg[${(L)color}]%}' + eval PR_LIGHT_$color='%{$fg[${(L)color}]%}' + (( count = $count + 1 )) +done +PR_NO_COLOR="%{$terminfo[sgr0]%}" + + diff --git a/completion b/completion.zsh similarity index 100% rename from completion rename to completion.zsh diff --git a/functions b/functions.zsh similarity index 83% rename from functions rename to functions.zsh index 1d4c083..3aa0e49 100644 --- a/functions +++ b/functions.zsh @@ -48,6 +48,15 @@ buf () { atomtitles () { curl --silent $1 | xmlstarlet sel -N atom="http://www.w3.org/2005/Atom" -t -m /atom:feed/atom:entry -v atom:title -n} +function printHookFunctions () { + print -C 1 ":::pwd_functions:" $chpwd_functions + print -C 1 ":::periodic_functions:" $periodic_functions + print -C 1 ":::precmd_functions:" $precmd_functions + print -C 1 ":::preexec_functions:" $preexec_functions + print -C 1 ":::zshaddhistory_functions:" $zshaddhistory_functions + print -C 1 ":::zshexit_functions:" $zshexit_functions +} + # reloads all functions # http://www.zsh.org/mla/users/2002/msg00232.html r() { diff --git a/options b/options.zsh similarity index 100% rename from options rename to options.zsh diff --git a/prompt b/prompt deleted file mode 100644 index f37a53d..0000000 --- a/prompt +++ /dev/null @@ -1,178 +0,0 @@ -# @author Sebastian Tramp -# @license http://opensource.org/licenses/gpl-license.php -# -# the prompt definition -# - -#on#### http://aperiodic.net/phil/prompt/ -function precmd { - local TERMWIDTH - (( TERMWIDTH = ${COLUMNS} - 1 )) - - ### - # Truncate the path if it's too long. - - PR_FILLBAR="" - PR_PWDLEN="" - - local promptsize=${#${(%):---(%n@%m:%l)---()--}} - local pwdsize=${#${(%):-%~}} - - if [[ "$promptsize + $pwdsize" -gt $TERMWIDTH ]]; then - ((PR_PWDLEN=$TERMWIDTH - $promptsize)) - else - PR_FILLBAR="\${(l.(($TERMWIDTH - ($promptsize + $pwdsize)))..${PR_HBAR}.)}" - fi - - ### - # Get APM info. - -# if which ibam > /dev/null; then -# PR_APM_RESULT=`ibam --percentbattery` -# elif which apm > /dev/null; then -# PR_APM_RESULT=`apm` -# fi - - ### - # Get Mercurial info - PR_HG_BRANCH=`hg branch 2>/dev/null` - if [ "$?" != "255" ]; then - #tmuxhglog - PR_HG_CHANGECOUNTER=`hg st 2>/dev/null | wc -l` - if [ "$PR_HG_CHANGECOUNTER" != "0" ]; then - PR_MESSAGE="$PR_HG_BRANCH ($PR_HG_CHANGECOUNTER)" - else - PR_MESSAGE="$PR_HG_BRANCH" - fi - else - PR_MESSAGE="" - fi -} -setopt extended_glob -preexec () { - if [[ "$TERM" == "screen" ]]; then - local CMD=${1[(wr)^(*=*|sudo|-*)]} - echo -n "\ek$CMD\e\\" - fi -} -setprompt () { - ### - # Need this so the prompt will work. - - setopt prompt_subst - - - ### - # See if we can use colors. - - autoload colors zsh/terminfo - if [[ "$terminfo[colors]" -ge 8 ]]; then - colors - fi - for color in RED GREEN YELLOW BLUE MAGENTA CYAN WHITE; do - eval PR_$color='%{$terminfo[bold]$fg[${(L)color}]%}' - eval PR_LIGHT_$color='%{$fg[${(L)color}]%}' - (( count = $count + 1 )) - done - PR_NO_COLOUR="%{$terminfo[sgr0]%}" - - - ### - # See if we can use extended characters to look nicer. - - typeset -A altchar - set -A altchar ${(s..)terminfo[acsc]} - PR_SET_CHARSET="%{$terminfo[enacs]%}" - PR_SHIFT_IN="%{$terminfo[smacs]%}" - PR_SHIFT_OUT="%{$terminfo[rmacs]%}" - PR_HBAR=${altchar[q]:--} - PR_ULCORNER=${altchar[l]:--} - PR_LLCORNER=${altchar[m]:--} - PR_LRCORNER=${altchar[j]:--} - PR_URCORNER=${altchar[k]:--} - - - ### - # Decide if we need to set titlebar text. - - case $TERM in - xterm*) - PR_TITLEBAR=$'%{\e]0;%(!.-=*[ROOT]*=- | .)%n@%m:%~ | ${COLUMNS}x${LINES} | %y\a%}' - ;; - screen) - PR_TITLEBAR=$'%{\e_screen \005 (\005t) | %(!.-=[ROOT]=- | .)%n@%m:%~ | ${COLUMNS}x${LINES} | %y\e\\%}' - ;; - *) - PR_TITLEBAR='' - ;; - esac - - - ### - # Decide whether to set a screen title - if [[ "$TERM" == "screen" ]]; then - PR_STITLE=$'%{\ekzsh\e\\%}' - else - PR_STITLE='' - fi - - - ### - # APM detection - -# if which ibam > /dev/null; then -# PR_APM='$PR_RED${${PR_APM_RESULT[(f)1]}[(w)-2]}%%(${${PR_APM_RESULT[(f)3]}[(w)-1]})$PR_LIGHT_BLUE:' -# elif which apm > /dev/null; then -# PR_APM='$PR_RED${PR_APM_RESULT[(w)5,(w)6]/\% /%%}$PR_LIGHT_BLUE:' -# else -# PR_APM='' -# fi - - ### - # Finally, the prompt. - - # the original one - PROMPT=' -$PR_SET_CHARSET$PR_STITLE${(e)PR_TITLEBAR}\ -$PR_CYAN$PR_SHIFT_IN$PR_ULCORNER$PR_BLUE$PR_HBAR$PR_SHIFT_OUT(\ -$PR_GREEN%(!.%SROOT%s.%n)$PR_GREEN@%m:%l\ -$PR_BLUE)$PR_SHIFT_IN$PR_HBAR$PR_CYAN$PR_HBAR${(e)PR_FILLBAR}$PR_BLUE$PR_HBAR$PR_SHIFT_OUT(\ -$PR_MAGENTA%$PR_PWDLEN<...<%~%<<\ -$PR_BLUE)$PR_SHIFT_IN$PR_HBAR$PR_CYAN$PR_URCORNER$PR_SHIFT_OUT\ - -$PR_CYAN$PR_SHIFT_IN$PR_LLCORNER$PR_BLUE$PR_HBAR$PR_SHIFT_OUT(\ -%(?..$PR_LIGHT_RED%?$PR_BLUE:)\ -${(e)PR_APM}$PR_YELLOW%D{%H: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PR_HBAR$PR_SHIFT_OUT(\ -$PR_LIGHT_GREEN%_$PR_BLUE)$PR_SHIFT_IN$PR_HBAR$PR_SHIFT_OUT\ -$PR_CYAN$PR_SHIFT_IN$PR_HBAR$PR_SHIFT_OUT$PR_NO_COLOUR ' -} - -setprompt -#off#### http://aperiodic.net/phil/prompt/ - diff --git a/prompt.zsh b/prompt.zsh new file mode 100644 index 0000000..b37487b --- /dev/null +++ b/prompt.zsh @@ -0,0 +1,107 @@ +# enable hook method +autoload add-zsh-hook + +# enable and configure vcs_info +autoload -Uz vcs_info +add-zsh-hook precmd vcs_info +zstyle ':vcs_info:*' enable hg git cvs svn +zstyle ':vcs_info:*' formats '%s|%b|%a|%i|%R|%r|%S|%m' + +# my prompt theme +function promptSetup () { + setopt prompt_subst + local TERMWIDTH + (( TERMWIDTH = ${COLUMNS} - 1 )) + + NOCOLOR="%{$terminfo[sgr0]%}" + PS1=''; RPS1='' + PS2="↷ %_>"; RPS2='' + PS3="↷ ?#"; RPS3='' + PS4="↷ +i>"; RPS3='' + + # prepare vcs info + VCS_LINE='' + VCS=$vcs_info_msg_0_ + VCS_TYPE=$VCS[(ws:|:)1] + VCS_BRANCH=$VCS[(ws:|:)2] + VCS_CHANGES='' + + # setup the prompt sign + if [[ $VCS_TYPE != '' ]]; then + VCS_LINE+=$NOCOLOR + VCS_LINE+='➜ ' + case $VCS_TYPE in + 'hg') + VCS_LINE+='☿ ' + VCS_CHANGES=`hg st 2>/dev/null | wc -l` + ;; + 'git') + VCS_LINE+='± ' + ;; + *) + VCS_LINE+="$VCS_TYPE " + ;; + esac + fi + + VCS_LINE+=$VCS_BRANCH + + if [[ $VCS_CHANGES > 0 ]]; then + VCS_LINE+="%F{yellow}%B" + VCS_LINE+=' ★ ' + VCS_LINE+=$VCS_CHANGES + fi + + # rootshell gets another prompt sign + PR_SIGN=$NOCOLOR + PR_SIGN+="%F{red}%B" + PR_SIGN+=%(#."☠".'☭') + PR_SIGN+="%F{white}%b" + + # http://unix.stackexchange.com/questions/1022/is-it-possible-to-display-stuff-below-the-prompt-at-a-prompt + terminfo_down_sc=$terminfo[cud1]$terminfo[cuu1]$terminfo[sc]$terminfo[cud1] + + # Finally, the prompt. + PS1=$'\n' # newline (specially quotet, see zsh FAQ 3.13) + PS1+="%{$terminfo_down_sc$VCS_LINE$terminfo[rc]%}" # the second line + PS1+=$PR_STITLE # tmux title if present + PS1+=$PR_VCSSIGN # version control part if present + PS1+=%(?..'%F{red}%B%'?) # output last error number if present + PS1+=$PR_SIGN # the user sign + PS1+=" " # an additional space + + # reset the tmux title + promptSetMultiplexerTabTitle "zsh" +} +add-zsh-hook precmd promptSetup + +# set a tmux / screen 'tabulator' title if needed +function promptSetMultiplexerTabTitle () { + if [[ "$TERM" == "screen" ]]; then + if [[ "$1" == "" ]]; then + local CMD=${1[(wr)^(*=*|sudo|-*)]} + echo -n "\ek$CMD\e\\" + else + echo -n "\ek$1\e\\" + fi + fi +} +add-zsh-hook preexec promptSetMultiplexerTabTitle + +# setup tmux environment (context + status) +function tmuxChangeDirectory () { + # set the tmux status line + if [[ "$TMUX" != "" ]]; then + tmux set-option -g status-right $OLDPWD >/dev/null + fi +} +add-zsh-hook chpwd tmuxChangeDirectory + +# remove the line after the prompt on execution +# http://unix.stackexchange.com/questions/1022/is-it-possible-to-display-stuff-below-the-prompt-at-a-prompt +function eraseSecondLine () { + print -rn -- $terminfo[el]; + #echo; # this would keep the second line +} +add-zsh-hook preexec eraseSecondLine + diff --git a/zshrc b/zshrc index b64b9d3..67f9afc 100644 --- a/zshrc +++ b/zshrc @@ -5,12 +5,13 @@ # typeset -ga sources -sources+="$HOME/.zsh/options" -sources+="$HOME/.zsh/prompt" -sources+="$HOME/.zsh/functions" -sources+="$HOME/.zsh/private" -sources+="$HOME/.zsh/aliases" -sources+="$HOME/.zsh/completion" +#sources+="$HOME/.zsh/colors.zsh" # maybe we do not need this anymore +sources+="$HOME/.zsh/options.zsh" +sources+="$HOME/.zsh/prompt.zsh" +sources+="$HOME/.zsh/functions.zsh" +sources+="$HOME/.zsh/private.zsh" +sources+="$HOME/.zsh/aliases.zsh" +sources+="$HOME/.zsh/completion.zsh" sources+="$HOME/.zsh/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh" sources+="/etc/zsh_command_not_found"