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)))) ❯ "
@behnammodi
Copy link
Author

behnammodi commented Dec 22, 2019

Bug fix

I changed line 7:

- ... | grep -v "0"
+ ... | grep -v "^0"

@nainemom
Copy link

I just created a more readable version with the last command status check feature for my personal use. this is not the same as the previous one, but maybe you can combine some part of them:

red() {
    echo -e "\e[31m${1}\e[0m"
}

green() {
    echo -e "\e[32m${1}\e[0m"
}

magenta() {
    echo -e "\e[35m${1}\e[0m"
}

cyan() {
    echo -e "\e[36m${1}\e[0m"
}

gray() {
    echo -e "\e[90m${1}\e[0m"
}

working_dir() {
    gray $(pwd)
}
current_user() {
    whoami
}

git_state() {
    if [ -d ./.git ]
    then
        local gitbranch=`cyan $(git rev-parse --abbrev-ref HEAD)`
        local gitstatus=`magenta $(git status -s | wc -l)`
        echo " ($gitbranch:$gitstatus)"
    else
        echo ""
    fi
}

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

PS1="\$(start_text) \$(working_dir)\$(git_state) "

@behnammodi
Copy link
Author

@nainemom Can you take a snapshot from your terminal? I think, It's fantastic

@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