1

My terminal has output like the one below.

pc@pop-os:~/my-project (main)$

This project was downloaded via GitHub. (main) is colored green. I set it as a branch name that appears on my terminal when I go to the project's directory. When I make a change, I want the terminal to recognize the change and change the (main) color to red. Does anyone who can help with that, please?

8
  • Are you using zsh? Commented Oct 9, 2022 at 15:59
  • No, I am using bash @EdgarMagallon
    – Serdar
    Commented Oct 9, 2022 at 16:01
  • Do you use ohmybash or any other plugin/framework to customize your shell? Btw , what is the output of echo $PS1 Commented Oct 9, 2022 at 16:16
  • I am not using anything. Output of PS1 is \[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[32m\]$(parseGitBranch) \[\033[00m\]\$\n>
    – Serdar
    Commented Oct 9, 2022 at 16:29
  • @SerdarAKYOL I think PopOs has installed some framework to customize your shell like that. Can you show us the output of type -a parseGitBranch I'm not sure if this will show some output. Commented Oct 9, 2022 at 17:13

1 Answer 1

1

I used the parseGitBranch function to define the colors. Your function should look like this:

parseGitBranch () {

   if ! git rev-parse --git-dir &> /dev/null; then
      return 
   fi
   branch="($(git branch --show-current))"

   local STATUS
   STATUS=$(git status --porcelain)
   if [ -n "$STATUS" ]; then
      echo -e "\033[38;5;1m $branch" #red
   else
      echo -e "\033[38;5;34m $branch" #green
   fi
}

And you should set PS1 to:

PS1="\[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\$(parseGitBranch) \[\033[00m\]\$\n>"

The code above could be inside ~/.bashrc.

Explanation

The following line will check if current directory is a git project:

   if ! git rev-parse --git-dir &> /dev/null; then
      return 
   fi

I've changed your code git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/' to git branch --show-current. That's an easier way to get the current branch name.
Now, if the condition above is true then the function will check the status for the current git directory.
The most effective way to know if there are changes in your project is using git status --porcelain. So if you don't have any changes the output of git status --porcelain will be empty. With -n "$STATUS" checks if the length of STATUS is nonzero.

If -n $STATUS is true then the branch color will be red (because you have changes): echo -e "\033[38;5;1m $branch" #red.
If it's false then the branch color will be green: echo -e "\033[38;5;34m $branch" #green.

You can check this link for more information about bash colors and formatting. Maybe your terminal doesn't support some formatting or the colors will not be printed correctly.

2
  • That's quite good. Thank you so much for the explanation. I wanted to give +1 but like I have less than 15 reputations, I couldn't.
    – Serdar
    Commented Oct 10, 2022 at 19:09
  • @SerdarAKYOL no problem! Glad this works for you. Commented Oct 10, 2022 at 19:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .