Skip to content

Instantly share code, notes, and snippets.

@behnammodi
Last active December 25, 2019 18:17
Show Gist options
  • Save behnammodi/0771fb79d101212784d42c7e090bdbdf to your computer and use it in GitHub Desktop.
Save behnammodi/0771fb79d101212784d42c7e090bdbdf to your computer and use it in GitHub Desktop.
.bash_profile
# Git branch in prompt.
function parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}
function number_of_uncommited(){
git status -s 2> /dev/null | wc -l | awk '{$1=$1};1' | grep -v "^0"
}
function color_blue(){
echo -e "\033[34m$1\033[00m"
}
function color_green(){
echo -e "\033[32m$1\033[00m"
}
function text_bold(){
echo -e "\033[1m$1\033[21m"
}
function text_blink(){
echo -e "\033[5m$1\033[25m"
}
export PS1="\u@\h 📂 \W \$(color_green \$(parse_git_branch)) \$(text_bold \$(text_blink \$(color_blue \$(number_of_uncommited)))) ❯ "
@nainemom
Copy link

nainemom commented Dec 25, 2019

Features

  • show git branch and status
  • show js package name and version
  • show js bundle size
  • show last command exit code
# CREATE PS1
working_dir() {
    local userdir="$(echo ~)"
    local wholedir="$(pwd)"
    echo ${wholedir} | sed "s*${userdir}*~*g"
}
current_user() {
    echo $(whoami)
}

npm_state() {
    if [ -f ./package.json ]
    then
        local pkg=$(cat package.json)
        local pkgname="$(echo $pkg | jq '.name' | sed -e 's*"**g')"
        local pkgver="$(echo $pkg | jq '.version' | sed -e 's*"**g')"
        local deps="$(echo $pkg | jq '.dependencies' | sed -e 's/ *}*{*$//g' | grep ':' | wc -l)"
        local pkgmain=$(echo $pkg | jq '.main' | sed -e 's*"**g')
        local fsize="NaN"
        if [ -f $pkgmain ]
        then
            local fsize="$(du -sh $pkgmain | awk '{ print $1 }')"
        fi
        echo " (pkg:$pkgname@$pkgver,d:$deps,bs:$fsize)"
    else
        echo ""
    fi
}
git_state() {
    if [ -d ./.git ]
    then
        local gitbranch=`git rev-parse --abbrev-ref HEAD`
        local gitstatus=`git status -s | wc -l`
        if [ $gitstatus -gt 0 ]
        then
            gitstatus="*"
        else
            gitstatus=""
        fi
        echo " (git:$gitbranch$gitstatus)"
    else
        echo ""
    fi
}

start_text() {
    if [ $? = "0" ]
    then
        echo "$ "
    else
        echo "$:$? "
    fi
}




PS1=$(echo -e "\$(start_text)\$(current_user):\$(working_dir)\$(git_state)\$(npm_state) \n$\> ")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment