hg PS1 bash shell helper

hg PS1 bash shell helper

18. prosinca 2015. 14:00 drazen Bash

 

Simple bash function to include hg information in PS1 variable in bash shell. (Gist)

Code:

#!/bin/bash
#
# Ref: http://unix.stackexchange.com/questions/66581
# For getting both exit status and output from command
#
# __hg_ps1 function shows hg branch and minified hg status when working directory is in a mercurial repo.
#
__hg_ps1() {
    local INFO
    INFO=$(hg branch 2> /dev/null)
    if [ $? -eq 0 ]; then
        echo -e "[$INFO $(hg status | cut -b 1 | uniq | sort | tr -d '\n')]"
    fi
}

# PS1 example
export PS1='$(__hg_ps1)'$PS1