Skip to content

Commit

Permalink
Add custom bash PS1
Browse files Browse the repository at this point in the history
Referenced <https://stackoverflow.com/a/11975827> to get git tag names.
  • Loading branch information
spenserlee committed Jun 23, 2023
1 parent 08cc18d commit 9e4d80d
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
40 changes: 40 additions & 0 deletions .bashrc
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,46 @@ xterm*|rxvt*)
;;
esac

function we_are_in_git_work_tree {
git rev-parse --is-inside-work-tree &> /dev/null
}

function parse_git_branch {
if we_are_in_git_work_tree
then
local BR=$(git rev-parse --symbolic-full-name --abbrev-ref HEAD 2> /dev/null)
if [ "$BR" == HEAD ]
then
# revision isn't a named branch, try to get tag name
local NM=$(git name-rev --name-only HEAD 2> /dev/null)
# revision isn't a tag, use shortened hash
if [ "$NM" != undefined ]
then echo -n "@$NM"
else git rev-parse --short HEAD 2> /dev/null
fi
else
echo -n $BR
fi
fi
}

function set_prompt {
# local time="\[\e[34m\]\T\[\e[m\]-"
local user="\[\e[36m\]\u"
local at="\[\e[m\]@"
local host="\[\e[32m\]`hostname | cut -d "-" -f 3`\[\e[m\]:"
local dir="\[\e[33m\]\w"
local git_color="\[\033[31m\]"
local git_branch='(`parse_git_branch`)'
local git_diff='`git rev-parse 2>/dev/null && (git diff --no-ext-diff --quiet --exit-code 2> /dev/null || echo -e \*)`'
local prompt="\[\e[m\]$"

# export PS1="$time$user$at$host$dir$git_color$git_branch$git_diff$prompt "
export PS1="$user$at$host$dir$git_color$git_branch$git_diff$prompt "
}

set_prompt

# enable color support of ls and also add handy aliases
if [ -x /usr/bin/dircolors ]; then
test -r ~/.dircolors && eval "$(dircolors -b ~/.dircolors)" || eval "$(dircolors -b)"
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ New machine:
* <https://old.reddit.com/r/tmux/comments/yd62te/i_really_need_help_with_italic_and_truecolor>

* fix git gutter / fugitive not working for bare repo
* potential leads:
* workaround is to invoke nvim after setting some env vars:
`GIT_DIR=$HOME/.dotfiles GIT_WORK_TREE=$HOME nvim`
* similar problem discussed here:
* <https://github.com/tpope/vim-fugitive/issues/1981#issuecomment-1107388377>
* <https://stackoverflow.com/a/66624354/5323947>

Expand Down

0 comments on commit 9e4d80d

Please sign in to comment.