2017-11-20 2 views
0

이것은 대부분 작동합니다. git 상태에 따라 bash 프롬프트에 색상을 추가 할 수있었습니다. 모든 것이 커밋되면 (원점/마스터) 녹색이되고, 그렇지 않다면 내가 커밋하지 않은 것을 알게됩니다. 그러나 나는 '변경되지 않은 변경 사항'을 위해 노란색과 같은 추가 색상을 추가하려고하는데, 아무 것도하지 않으면 빨간색이됩니다. 여기 거의 다! bash 프롬프트에 또 다른 git 상태 색상을 어떻게 추가 할 수 있습니까?

내가 내 .bashrc에 여러 글에서 지금까지 함께 자갈길을 사용해보세요

배쉬 버전 : -release 4.3.48 (1)

OS : 리눅스 민트 18.2

## trim to two dir depth 
PROMPT_DIRTRIM=2 

## green [email protected], then blue dirs, then colours for git branch 

COLOURGREEN="\033[01;32m" 
COLOURBLUE="\033[01;34m" 
COLOURPLAIN="\033[m" 
COLOURRED="\033[1;31m" 
COLOURYELLOW="\033[1;33m" 

## This works fine on it's own, I see the (origin/master) in prompt 
parse_git_branch() { 
    git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/' 
} 

## green works when all files committed but not other colours 
git_colour() { 

    local gitstatus="$(git status 2> /dev/null)" 

    if [[ ! $gitstatus =~ "working directory clean" ]]; then 
     echo -e $COLOURRED 
    elif [[ $gitstatus =~ "Untracked files:" ]]; then 
     echo -e $COLOURYELLOW 
    elif [[ $gitstatus =~ "nothing to commit" ]]; then 
     echo -e $COLOURGREEN 
    else 
     echo -e $COLOURPLAIN 
fi 
} 

## working export without colour on git 
# Example: [email protected] ~/.../dir3/dir4 (origin/master) 
# export PS1="\[\033[01;32m\]\[email protected]\h\[\033[01;34m\] \w\[\033[m\]\$(parse_git_branch) $ " 

## works only when green 
export PS1="\[\033[01;32m\]\[email protected]\h\[\033[01;34m\] \w\[\$(git_colour)\]\$(parse_git_branch)\[\033[m\] $ " 

git_colour()에 약간의 도움이되었거나 bash 컬러 코드를 망쳤다면? Thanks

+0

상태가 "작업 디렉토리 클린"이 아닌 경우 빨간색이 표시됩니다. –

+0

아니요 깨끗하지 않으면 녹색이되고 그렇지 않으면 녹색으로 유지됩니다. 다른 색상은 발생하지 않습니다. 보고 싶은 git 상태의 여러 상태를 시도하고 있지만 완전히 커밋 된 상태 만이 dir을 녹색으로 만듭니다. – sf2k

+0

if 문을 줄이고 다시 작성하여 진행 상황을 확인하십시오. – sf2k

답변

0

Benjamin W의 git status --porcelain 제안을 사용하고 COLOURGREEN을 마지막 else로 설정하기 위해 if 문을 다시 실행하면 git 상태에 따라 색상이 바뀌는 프롬프트가 나타납니다! 후자. 위의 git_colour()를 다음으로 변경하십시오.

git_colour() { 


    local gitstatus="$(git status --porcelain 2> /dev/null)" 

    if [[ $gitstatus =~ "??" ]]; then 
     echo -e $COLOURRED 
    elif [[ $gitstatus =~ "A" ]]; then 
     echo -e $COLOURYELLOW 
    else 
     echo -e $COLOURGREEN 
    fi 
} 
관련 문제