michalbartnicki
5/18/2018 - 6:01 PM

prompt

ELIPSIS_CUT=18
function elipsis {
  INPUT_STRING=$1
  INPUT_STRING_LENGHT=${#INPUT_STRING}
  CUT=$(($ELIPSIS_CUT - 3))

  if [ $CUT -lt 0 ]
  then
    echo $INPUT_STRING
  else
    if [ $INPUT_STRING_LENGHT -lt $CUT ]
    then
      echo $INPUT_STRING
    else
      echo ${INPUT_STRING:0:$CUT}"..."
    fi
  fi
}

RESTORE=$(echo -en '\001\e[0m\002')
RED=$(echo -en '\001\033[00;31m\002')
GREEN=$(echo -en '\001\033[00;32m\002')
YELLOW=$(echo -en '\001\033[00;33m\002')
BLUE=$(echo -en '\001\033[00;34m\002')
MAGENTA=$(echo -en '\001\033[00;35m\002')
PURPLE=$(echo -en '\001\033[00;35m\002')
CYAN=$(echo -en '\001\033[00;36m\002')
LIGHTGRAY=$(echo -en '\001\033[00;37m\002')
LRED=$(echo -en '\001\033[01;31m\002')
LGREEN=$(echo -en '\001\033[01;32m\002')
LYELLOW=$(echo -en '\001\033[01;33m\002')
LBLUE=$(echo -en '\001\033[01;34m\002')
LMAGENTA=$(echo -en '\001\033[01;35m\002')
LPURPLE=$(echo -en '\001\033[01;35m\002')
LCYAN=$(echo -en '\001\033[01;36m\002')
WHITE=$(echo -en '\001\033[01;37m\002')

GIT_IN_PROMPT=1

function parse_git_branch() {
  if [ $GIT_IN_PROMPT -ne 1 ]
  then
    return
  fi

  BRANCH=`git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/'`
  BRANCH=`elipsis $BRANCH`

  if [ ! "${BRANCH}" == "" ]
  then
    STAT=`parse_git_dirty`
    if [ ! "${STAT}" == "" ]
    then
      printf "${YELLOW}| ${BRANCH} (${LRED}${STAT}${RESTORE}${YELLOW})${RESTORE} "
    else
      printf "${YELLOW}| ${BRANCH}${RESTORE} "
    fi
  else
    echo ""
  fi
}

function parse_git_dirty {
	status=`git status 2>&1 | tee`
	dirty=`echo -n "${status}" 2> /dev/null | grep "modified:" &> /dev/null; echo "$?"`
	untracked=`echo -n "${status}" 2> /dev/null | grep "Untracked files" &> /dev/null; echo "$?"`
	ahead=`echo -n "${status}" 2> /dev/null | grep "Your branch is ahead of" &> /dev/null; echo "$?"`
	newfile=`echo -n "${status}" 2> /dev/null | grep "new file:" &> /dev/null; echo "$?"`
	renamed=`echo -n "${status}" 2> /dev/null | grep "renamed:" &> /dev/null; echo "$?"`
	deleted=`echo -n "${status}" 2> /dev/null | grep "deleted:" &> /dev/null; echo "$?"`
	bits=''
	if [ "${renamed}" == "0" ]; then
		bits=">${bits}"
	fi
	if [ "${ahead}" == "0" ]; then
		bits="*${bits}"
	fi
	if [ "${newfile}" == "0" ]; then
		bits="+${bits}"
	fi
	if [ "${untracked}" == "0" ]; then
		bits="?${bits}"
	fi
	if [ "${deleted}" == "0" ]; then
		bits="x${bits}"
	fi
	if [ "${dirty}" == "0" ]; then
		bits="!${bits}"
	fi
	if [ ! "${bits}" == "" ]; then
		echo "${bits}"
	else
		echo ""
	fi
}

function prevCommandReturn {
    if [ $? != 0 ]; then
        echo $RED"☹"$RESTORE
    else
        echo $GREEN"☺"$RESTORE
    fi
}

function promptSign {
  echo $LRED⚡$RESTORE
}

function decoratePath {
  echo $LGREEN${1/$HOME/"~"}$RESTORE
} 

# there's a problem with decorating \w
export PS1="\$(prevCommandReturn) \$(decoratePath \w) \$(parse_git_branch)\$(promptSign) "