zshrc.d/20-prompt.zrc
author Mikael Berthe <mikael@lilotux.net>
Sat, 14 Jul 2012 19:20:57 +0200
changeset 0 7215ca490221
child 7 c032d39eafcf
permissions -rw-r--r--
Re-import repository after cleanup from personal stuff (see description) I've remove too dirty or specific stuff, so that i could put this configuration on a public repo. Here's a copy of the initial Mercurial changelog, FWIW... * (2012-07-14) Mikael Berthe Reorganize all scripts * (2012-07-14) Mikael Berthe Add a "tools" directory * (2012-07-14) Mikael Berthe Don't use ^X^C as a keybinding * (2012-07-14) Mikael Berthe Disable ... completion for now * (2012-07-14) Mikael Berthe Add rehash key binding * (2012-07-14) Mikael Berthe Add n fix a few key bindings * (2012-07-14) Mikael Berthe More cleanup Remove zshrc_compinstall Move personnal stuff to zshrc_local * (2012-07-14) Mikael Berthe New commands: ...* Previous commands .. and ... have been renamed to ,, and ,,, * (2012-07-14) Mikael Berthe Add WATCHFMT, move REPORTTIME to zshenv * (2012-07-14) Mikael Berthe Add REPORTTIME * (2012-07-13) Mikael Berthe Add a few useful functions Ideas borrowed from https://github.com/whiteinge/dotfiles/blob/master/.zshrc * (2012-07-13) Mikael Berthe Move aliases to a new file * (2012-07-13) Mikael Berthe Check for zshrc_local existence * (2012-07-13) Mikael Berthe Cleanup vcsinfo prompt * (2012-07-12) Mikael Berthe Colorize time in default color prompt as well * (2012-07-12) Mikael Berthe Prompt: colorize time * (2012-07-12) Mikael Berthe Add a copy of git-prompt.sh * (2012-07-12) Mikael Berthe Update prompt themes Since we do not display the mode on the first line, let's use this place for the date/time. * (2012-07-12) Mikael Berthe Update prompts / new showmode * (2012-07-12) Mikael Berthe More showmode cleanup, part2 * (2012-07-12) Mikael Berthe Switch showmode to using RPROMPT, part1 * (2012-07-12) Mikael Berthe Prompt: many improvements and fixes * (2012-07-11) Mikael Berthe More prompt code cleanup * (2012-07-11) Mikael Berthe More readable prompt generation * (2012-07-11) Mikael Berthe Dynamic path color * (2012-07-11) Mikael Berthe New prompt based on Zsh' vcs_info module * (2012-07-10) Mikael Berthe Add git info, add a light dev2 prompt * (2012-07-10) Mikael Berthe Dev prompt: Improve Mercurial repository detection * (2012-07-08) Mikael Berthe hg prompt: Change color of unapplied patches * (2012-07-08) Mikael Berthe Change Mercurial prompt format * (2012-07-08) Mikael Berthe Try to improve showmode support. Enable it for 256 color terms. * (2012-07-08) Mikael Berthe Fix interaction between showmode and the new "dev" prompt When showmode is disabled (e.g. in screen), do not display the mode in PS1. * (2012-07-08) Mikael Berthe Add development prompt (HG and minimal git support) Can be set up from the shell command line with "prompt_set_dev". * (2012-07-08) Mikael Berthe Ignore Vim swap files * (2009-12-22) Mikael Berthe Slight update zlog{in,out} * (2009-12-22) Mikael Berthe Add zlogin * (2009-12-22) Mikael Berthe Update hgignore list * (2009-12-22) Mikael Berthe Initial import into Mercurial


# You can use fast_hg_root from https://bitbucket.org/yaniv_aknin/fast_hg_root
# or maybe use the command false if you don't use Mercurial at all.
# (Override in $ZDOTLOCALDIR/zshrc)
HG_ROOT_CMD=("hg" "root")
export HG_ROOT_CMD


### Extended prompts

function __prompt_path_color {
  local path_color
  if [[ -O "$PWD" ]]; then
    path_color="${fg_no_bold[magenta]}"
  elif [[ -w "$PWD" ]]; then
    path_color="${fg_no_bold[blue]}"
  else
    path_color="${fg_bold[red]}"
  fi
  print "%{$path_color%}"
}

# From http://stevelosh.com/blog/2010/02/my-extravagant-zsh-prompt/
function __prompt_char {
    __gitdir >/dev/null 2>/dev/null && echo '±' && return
    $HG_ROOT_CMD >/dev/null 2>/dev/null && echo '' && return
    echo ''
}

function __hg_prompt_info {
    $HG_ROOT_CMD >/dev/null 2>/dev/null && 
    hg prompt --angle-brackets "
<HG patches: <patches|join( → )|pre_applied(%{$fg[yellow]%})|post_applied(%{$reset_color%})|pre_unapplied(%{$fg[cyan]%})|post_unapplied(%{$reset_color%})>
>%BHG%b<(%{$fg[green]%}<status|modified|unknown>%{$reset_color%})><%{$fg[green]%}<update>%{$reset_color%}>\
< on %{$fg[magenta]%}<branch|quiet>%{$reset_color%}>\
< at %{$fg[yellow]%}<tags|%{$reset_color%}, %{$fg[yellow]%}>%{$reset_color%}>" 2>/dev/null
}

source $ZDOTDIR/tools/git-prompt.sh > /dev/null 2>&1
function __git_prompt_info {
  r=$(__git_ps1)
  if [ ! -z $r ]; then echo -e "\n%Bgit%b$r"; fi
}

function __prompt_set_dev {
    # Prompt complet sur 2 lignes, couleurs, avec un indicateur de dépôt git/hg
    local -a lines pline

    local red="%{$fg[red]%}"
    local cyan="%{$fg[cyan]%}"
    local reset="%{$reset_color%}"

    # 1re ligne
    pline+=( "%D{%m%d} " )              # Date
    pline+=( "$cyan%D{%H:%M}$reset " )  # Time
    pline+=( $(__prompt_path_color) )   # Couleur du chemin dynamique
    pline+=( "%~$reset" )               # Chemin (cwd)
    lines+=( ${(j::)pline} )
    pline=()

    # 2e ligne
    pline+=( "[%B%n%b] " )              # Nom d'utilisateur
    pline+=( "$red%m$reset" )           # Nom de machine
    pline+=( $(__prompt_char) )         # Indicateur de dépôt (hg/git)
    pline+=( "%# " )                    # Invite de commande
    lines+=( ${(j::)pline} )

    export PS1=${(F)lines}
}

function __prompt_set_dev-mikael {
    # Prompt complet sur 2 ou 3 lignes, avec informations sur les dépôts git/hg
    local -a lines pline

    local red="%{$fg[red]%}"
    local cyan="%{$fg[cyan]%}"
    local reset="%{$reset_color%}"

    # 1re ligne
    pline+=( "%D{%m%d} " )              # Date
    pline+=( "$cyan%D{%H:%M}$reset " )  # Time
    pline+=( $(__prompt_path_color) )   # Couleur du chemin dynamique
    pline+=( "%~$reset" )               # Chemin (cwd)
    lines+=( ${(j::)pline} )
    pline=()

    # 2e ligne
    pline+=( "[%B%n%b] " )              # Nom d'utilisateur
    pline+=( "$red%m$reset" )           # Nom de machine
    pline+=( "$(__hg_prompt_info)" )    # Informations de dépôt Mercurial
    pline+=( "$(__git_prompt_info)" )   # Informations de dépôt git
    pline+=( "%# " )                    # Invite de commande
    lines+=( ${(j::)pline} )

    export PS1=${(F)lines}
}

function precmd {
    case $_PROMPT_DEFINED in
        dev)
            __prompt_set_dev
            ;;
        dev-mikael)
            __prompt_set_dev-mikael
            ;;
        dev-vcsinfo)
            __precmd_vcsinfo
            ;;
        esac
}

function prompt_set_dev-vcsinfo {
    _PROMPT_DEFINED="dev-vcsinfo"
    source $ZDOTDIR/functions/zshrc_vcsinfo
}

function __prompt_unset_dev-vcsinfo {
    if [[ $_PROMPT_DEFINED == "dev-vcsinfo" ]]; then
        zstyle ':vcs_info:*' enable NONE
        unset RPS1
    fi
}

function prompt_set_blackwhite {
    # Prompt complet sur 2 lignes, N&B
    __prompt_unset_dev-vcsinfo
    unsetopt prompt_subst

    export PS1="%D{%m%d %H:%M} %B%~%b
[%B%n%b:%l] %S%m%s%# "

    export _PROMPT_DEFINED="blackwhite"
}

function prompt_set_color {
    # Prompt complet sur 2 lignes, couleurs
    __prompt_unset_dev-vcsinfo
    unsetopt prompt_subst

    PS1="%D{%m%d} %{$fg[cyan]%}%D{%H:%M}%{$reset_color%} "
    PS1=$PS1"%{$fg[magenta]%}%~%{$reset_color%}
[%B%n%b:%l] %{$fg[red]%}%m%{$reset_color%}%# "
    export PS1

    export _PROMPT_DEFINED="blackwhite"
}

function prompt_set_dev {
    __prompt_unset_dev-vcsinfo
    export _PROMPT_DEFINED="dev"
}
function prompt_set_dev-mikael {
    __prompt_unset_dev-vcsinfo
    export _PROMPT_DEFINED="dev-mikael"
}


# Invite de commande par défaut
prompt_set_color