jimeh / git-aware-prompt

Display current Git branch name in your terminal prompt when in a Git working directory.
Creative Commons Zero v1.0 Universal
2.16k stars 340 forks source link

Spacing control #51

Closed paulej closed 6 years ago

paulej commented 7 years ago

I just found this repo. Very nice! Much appreciated!

Normally, I have this for a prompt:

PS1='[\u@\h \w]\$ '

Using what was provided here, I created this prompt:

PS1='[\u@\h \w]\['\$txtcyn'\]'\$git_branch'\['\$txtred'\]'\$git_dirty'\['\$txtrst'\]\$ '

However, I'd prefer to have a space before the name of the git branch. If I insert one into PS1, I always have a space after the normal closing bracket and the $.

So what I did was this:

PS1='[\u@\h \w]\['\$txtcyn'\]'\$git_space\$git_branch'\['\$txtred'\]'\$git_dirty'\['\$txtrst'\]\$ '

The git_space is an empty string when git_branch is empty and contains one space when git_branch is non-empty. I did this by first setting PROMPT_COMMAND to my own script that will inspect $git_branch and assign $git_space appropriately. It works, but I'd prefer to see if I could have something added to the repo so I don't have to do that as an extension.

Would you entertain this change, just adding two git_space assignments?

find_git_branch() {
  # Based on: http://stackoverflow.com/a/13003854/170413
  local branch
  if branch=$(git rev-parse --abbrev-ref HEAD 2> /dev/null); then
    if [[ "$branch" == "HEAD" ]]; then
      branch='detached*'
    fi
    git_branch="($branch)"
    git_space=" "
  else
    git_branch=""
    git_space=""
  fi
}

One could then insert a space before and/or after the repo name as I have done in my PS1 example above. Some might want the same kind of thing between the branch name and $git_dirty. So perhaps we might have $git_branch_space and $git_dirty_space.

I'd be happy to do a PR is this would be of interest.

joeytwiddle commented 7 years ago

I also like to have a space in my prompt. I hard-coded it.

This is a neat solution to what I can only imagine is a common problem.

jonathandandries commented 6 years ago

+1 Nice suggestion. I was going down the path of creating git_space as a separate function using the output of this: git rev-parse --git-dir > /dev/null 2>&1 but I think your solution is just as effective (assuming everyone uses git_branch). I also like your idea of having separate git_branch_space and git_dirty_space for robustness. PR would be awesome.

lethosor commented 6 years ago

This is the git-related part of my prompt, which adds a space before the branch name. Does this work for you?

${git_branch:+ \[$txtgrn\]\$git_branch\[$txtred\]\$git_dirty\[$txtrst\]}
paulej commented 6 years ago

What I did rather than modifying the code in the repository, I just used PROMPT_COMMAND to do that for me. I set this in .bashrc:

PROMPT_COMMAND='if [ -z "$git_branch" ]; then git_space=""; else git_space=" "; fi'

This effectively does what I want. I still use largely the same PS1 setting, with just a color change:

PS1='[\u@\h \w\['\$txtgrn'\]'\$git_space\$git_branch'\['\$txtred'\]'\$git_dirty'\['\$txtrst'\]]\$ '
jonathandandries commented 6 years ago

I changed the PR to just a README recommendation explaining lethosor's response to use shell variable expansion in the PS1 prompt instead of having explicit space variables in the code. Thanks!

https://github.com/jimeh/git-aware-prompt/pull/61

paulej commented 6 years ago

I think @jonathandandries PR documenting @lethosor proposal works great. I'll close this.