TIL

Today I Learned

View the Project on GitHub ragubyun/TIL

prompt 설정

이렇게 깔끔하게 바꾸고싶다.

prompt clean

prompt not clean

zsh

아래 shell script를 .zshrc에 추가

function parse_git_branch() {
    git branch 2> /dev/null | sed -n -e 's/^\* \(.*\)/(\1) /p'
}

COLOR_RED=160
COLOR_ORANGE=172
COLOR_SKY=039
COLOR_GREEN=041
COLOR_GREY=008
COLOR_PUPPLE=099

function branch_color() {
    local git_status="$(git status 2> /dev/null)"

    if [[ $git_status =~ "Untracked files" ]]; then
      echo -e $COLOR_GREY
    elif [[ $git_status =~ "Changes not staged for commit" ]]; then
      echo -e $COLOR_RED
    elif [[ $git_status =~ "Changes to be committed" ]]; then
      echo -e $COLOR_ORANGE
    elif [[ $git_status =~ "Your branch is ahead of" ]]; then
      echo -e $COLOR_PUPPLE
    else
      echo -e $COLOR_GREEN
    fi
}

setopt PROMPT_SUBST
export PROMPT='%F{$COLOR_SKY}%~ %F{$(branch_color)}$(parse_git_branch)%F{$COLOR_SKY}$ %f'

more detail : ZSH Prompt Expansion

bash

아래 shell script를 .bash_profile에 추가

parse_git_branch() {
  git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}

# export PS1="\u@\h \W\[\033[32m\]\$(parse_git_branch)\[\033[00m\] $ "
# export PS1="\e[0;32m\u@\w $ \e[0m"

참고