diff options
| author | Marc Vertes <mvertes@free.fr> | 2024-10-12 16:58:06 +0200 |
|---|---|---|
| committer | Marc Vertes <mvertes@free.fr> | 2024-10-12 16:58:06 +0200 |
| commit | d8582fc7444451f503432735c35cc0a002311ebb (patch) | |
| tree | 892a6592e8ba7680d090afa9f491b73e26080bdc /.bash-powerline.sh | |
| parent | 37a8db745c42e174a33ef516d1af0071cd02f67c (diff) | |
update
Diffstat (limited to '.bash-powerline.sh')
| -rw-r--r-- | .bash-powerline.sh | 97 |
1 files changed, 97 insertions, 0 deletions
diff --git a/.bash-powerline.sh b/.bash-powerline.sh new file mode 100644 index 0000000..6d2ee2d --- /dev/null +++ b/.bash-powerline.sh @@ -0,0 +1,97 @@ +#!/usr/bin/env bash + +## Uncomment to disable git info +#POWERLINE_GIT=0 + +__powerline() { + # Colorscheme + readonly RESET='\[\033[m\]' + readonly COLOR_CWD='\[\033[0;34m\]' # blue + readonly COLOR_GIT='\[\033[0;36m\]' # cyan + #readonly COLOR_SUCCESS='\[\033[0;32m\]' # green + readonly COLOR_SUCCESS='\[\033[0;1;34m\]' # blue, bold + readonly COLOR_FAILURE='\[\033[0;1;31m\]' # red, bold + + #readonly SYMBOL_GIT_BRANCH='⑂' + readonly SYMBOL_GIT_BRANCH='→' + readonly SYMBOL_GIT_MODIFIED='¤' + readonly SYMBOL_GIT_PUSH='↑' + readonly SYMBOL_GIT_PULL='↓' + + if test -f '../usr/etc/hostname'; then + host=$(<../usr/etc/hostname) + elif test -f '/etc/hostname'; then + host=$(</etc/hostname) + else + host=$(hostname -s) + fi + + [ "$PS_SYMBOL" ] || PS_SYMBOL=';' + + __git_info() { + [[ $POWERLINE_GIT = 0 ]] && return # disabled + hash git 2>/dev/null || return # git not found + local git_eng="env LANG=C git" # force git output in English to make our work easier + + # get current branch name + local ref=$($git_eng symbolic-ref --short HEAD 2>/dev/null) + + if [[ -n "$ref" ]]; then + # prepend branch symbol + ref=$SYMBOL_GIT_BRANCH$ref + else + # get tag name or short unique hash + ref=$($git_eng describe --tags --always 2>/dev/null) + fi + + [[ -n "$ref" ]] || return # not a git repo + + local marks + + # scan first two lines of output from `git status` + while IFS= read -r line; do + if [[ $line =~ ^## ]]; then # header line + [[ $line =~ ahead\ ([0-9]+) ]] && marks+="$SYMBOL_GIT_PUSH${BASH_REMATCH[1]}" + [[ $line =~ behind\ ([0-9]+) ]] && marks+="$SYMBOL_GIT_PULL${BASH_REMATCH[1]}" + else # branch is modified if output contains more lines after the header line + marks="$SYMBOL_GIT_MODIFIED$marks" + break + fi + done < <($git_eng status --porcelain --branch 2>/dev/null) # note the space between the two < + + # print the git branch segment without a trailing newline + printf "$ref$marks" + } + + ps1() { + # Check the exit code of the previous command and display different + # colors in the prompt accordingly. + if [ $? -eq 0 ]; then + local symbol="$COLOR_SUCCESS$PS_SYMBOL $RESET" + else + local symbol="$COLOR_FAILURE$PS_SYMBOL $RESET" + fi + + local cwd="$COLOR_CWD\w$RESET" + # Bash by default expands the content of PS1 unless promptvars is disabled. + # We must use another layer of reference to prevent expanding any user + # provided strings, which would cause security issues. + # POC: https://github.com/njhartwell/pw3nage + # Related fix in git-bash: https://github.com/git/git/blob/9d77b0405ce6b471cb5ce3a904368fc25e55643d/contrib/completion/git-prompt.sh#L324 + if shopt -q promptvars; then + __powerline_git_info="$(__git_info)" + local git="$COLOR_GIT\${__powerline_git_info}$RESET" + else + # promptvars is disabled. Avoid creating unnecessary env var. + local git="$COLOR_GIT$(__git_info)$RESET" + fi + + [ "$SSH_TTY" ] && PS1=": $host:$cwd$git$symbol" || PS1=": $cwd$git$symbol" + } + + PROMPT_DIRTRIM=1 + PROMPT_COMMAND="ps1${PROMPT_COMMAND:+; $PROMPT_COMMAND}" +} + +__powerline +unset __powerline |
