lostwebsite.net/ posts/ debian/ mercurial shell prompt

I thought this could be interesting.

Based on this post by Mike Hommey, I decided to setup my own Mercurial-aware ZSH prompt.

Here the revelant part of my .zshrc.

function precmd {
    psvar=()
    if [ ! -e `pwd`/.hg ]; then
        PROMPT=$DEFAULT_PROMPT
        return
    else
        # TODO: We need some way to know quickly if there are changes.
        hg_tip=`hg tip --template 'hg_rev={rev};\nhg_branch={branches}'`        
        hg_status=`hg status -mardi`
        eval $hg_tip
        psvar[1]=$hg_rev
        if [[ "$hg_branch" == "" ]]; then
            psvar[2]=default
        else
            psvar[2]=$hg_branch
        fi
        if [[ "$hg_status" != "" ]]; then
            psvar[3]="*"
        else
            psvar[3]="-"
        fi
        PROMPT="[r:%1v b:%2v s:%3v]%n@%m:%1d > "
    fi
}

export DEFAULT_PROMPT="%n@%m:%~/ > "
export PROMPT=$DEFAULT_PROMPT

See the TODO for the problem I currently have with that. It is calling Mercurial twice. Mercurial is fast, but its still not a native program and it takes a noticable time to update the prompt. If I got all the informations in one call to hg I would be quite happy, but I feel I would need to hack some Python code and use the Mercurial classes.

The resulting prompt is compact and quite nice:

[r:4 b:debian s:-]fdgonthier@moka:spamassassin >