lostwebsite.net blog

Annex to the Lost Website

Mercurial shell prompt

leave a comment »

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 >

Written by fdgonthier

November 28, 2007 at 2:46 am

Posted in Linux, Tips and Tricks

Tagged with ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.