User Tools

Site Tools


ps1

This is an old revision of the document!


PS1

if ! $(declare -F __git_ps1 >/dev/null); then
    # Try to source a file with __git_ps1
    if [[ -e /usr/lib/git-core/git-sh-prompt ]]; then
        . /usr/lib/git-core/git-sh-prompt
    elif [[ -e /usr/share/git-core/contrib/completion/git-prompt.sh ]]; then
        . /usr/share/git-core/contrib/completion/git-prompt.sh
    elif [[ $(uname -s) == Darwin* ]] && [[ -f $(brew --prefix git)/etc/bash_completion.d/git-prompt.sh ]]; then
        . $(brew --prefix git)/etc/bash_completion.d/git-prompt.sh
    elif [[ -f $HOME/.git-prompt.sh ]]; then
        # wget -O ~/.git-prompt.sh https://raw.githubusercontent.com/git/git/master/contrib/completion/git-prompt.sh
        . $HOME/.git-prompt.sh
    fi

    # Still no __git_ps1? Fake it.
    if ! $(declare -F __git_ps1 >/dev/null); then
        # By icetan at https://stackoverflow.com/a/35513635/9181
        # Ex. to test if in a git repo: "rtrav .git $PWD"
        rtrav() { [ -e "$2"/"$1" ] || { [ "$2" != / ] && rtrav "$1" $(dirname "$2"); } }

        __git_ps1() {
            if rtrav .git "$PWD"; then
                local fmt="${1:- (%s)}"
                local branch=$(git branch 2>/dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/')
                if [ -n "$branch" ]; then
                    printf -- "$fmt" "$branch"
                fi
            fi
        }
    fi
fi

GIT_PS1_SHOWUPSTREAM="auto"
GIT_PS1_SHOWDIRTYSTATE="true"
GIT_PS1_STATESEPARATOR=""

# Trim everything before the dash, then trim everything after the colon.
SHORT_DOCKER_VER=${DOCKER_VER#*-}
SHORT_DOCKER_VER="🐳 \e[0;36m${SHORT_DOCKER_VER%:*}"

# Experimenting with git branch in PS1. Turn off by setting to false.
if true ; then
    export PROMPT_DIRTRIM=1
    # I like 023 or 030 for the git branch color. See https://i.stack.imgur.com/UQVe5.png
    if [[ -n $SSH_CLIENT ]]; then
        export PS1='$(if [ $? -eq 0 ]; then echo -e "\[\e[32m\]\xe2\x9c\x93";
                      else echo -e "\[\e[31m\]\xe2\x9c\x97"; fi) \[\e[38;5;242m\]\h:\w$(__git_ps1 " \[\e[38;5;030m\]%s\[\e[38;5;242m\]")$\[\e[0m\] '
    else
        export PS1='$(if [ $? -eq 0 ]; then echo -e "\[\e[32m\]\xe2\x9c\x93";
                      else echo -e "\[\e[31m\]\xe2\x9c\x97";
                      fi)$(if [ -n $DOCKER_VER ]; then echo -e " $SHORT_DOCKER_VER";
                      fi) \[\e[38;5;242m\]\w$(__git_ps1 " \[\e[38;5;030m\]%s\[\e[38;5;242m\]")$\[\e[0m\] '
    fi
else
    export PROMPT_DIRTRIM=4
    if [[ -n $SSH_CLIENT ]]; then
        export PS1='$(if [ $? -eq 0 ]; then echo -e "\[\e[32m\]\xe2\x9c\x93";
                      else echo -e "\[\e[31m\]\xe2\x9c\x97"; fi) \[\e[38;5;242m\]\h:\w$\[\e[0m\] '
    else
        export PS1='$(if [ $? -eq 0 ]; then echo -e "\[\e[32m\]\xe2\x9c\x93";
                      else echo -e "\[\e[31m\]\xe2\x9c\x97"; fi) \[\e[38;5;242m\]\w$\[\e[0m\] '
    fi
fi
ps1.1642652388.txt.gz · Last modified: 2023/04/12 20:44 (external edit)