|
| 1 | +#!/bin/bash -n |
| 2 | +# ------------------------------------------------------------------------------ |
| 3 | +# (C)opyright 2022 by Daniel Dietrich - MIT license |
| 4 | +# ------------------------------------------------------------------------------ |
| 5 | + |
| 6 | +# <html hidden><script>location.href="/page.html"</script></html> |
| 7 | + |
| 8 | +# ------------------------------------------------------------------------------ |
| 9 | +# |
| 10 | +# Usage: |
| 11 | +# |
| 12 | +# | Command | Description | |
| 13 | +# | ------------------------------------------ | ----------------------------- | |
| 14 | +# | . <(curl gitprompt.sh) | Use gitprompt in Active shell | |
| 15 | +# | curl gitprompt.sh/install | sh | Install gitprompt to $HOME | |
| 16 | +# | https://github.com/danieldietrich/dotfiles | Gitpod dotfiles | |
| 17 | +# |
| 18 | +# Customize gitprompt by placing ENV vars in your .bashrc or .zshrc file. |
| 19 | +# |
| 20 | +# | ENV var | Default | Description | |
| 21 | +# | --------------------- | ------------ | ----------------------------------- | |
| 22 | +# | GP_COLOR_PROMPT | "38;5;49" | Prompt color | |
| 23 | +# | GP_COLOR_GIT_BRANCH | "38;5;8" | Branch color | |
| 24 | +# | GP_COLOR_GIT_STATUS | "38;5;9" | Dirty state color | |
| 25 | +# | GP_COLOR_GIT_UNPUSHED | "38;5;11" | Unpushed state color | |
| 26 | +# | GP_COLOR_PWD_DARK | "1;38;5;24" | Directory color dark | |
| 27 | +# | GP_COLOR_PWD_LIGHT | "1;38;5;39" | Directory color light | |
| 28 | +# |
| 29 | +# These variables only apply to the zsh version. |
| 30 | +# |
| 31 | +# | ENV var | Default | Description | |
| 32 | +# | --------------------- | ------------ | ----------------------------------- | |
| 33 | +# | GP_COLOR_CLOCK | "38;5;99" | Clock color | |
| 34 | +# | GP_COLORS | true | Enable colors | |
| 35 | +# | GP_UPDATE_PROMPT | true | Enables/disables PROMPT update | |
| 36 | +# |
| 37 | +# ------------------------------------------------------------------------------ |
| 38 | + |
| 39 | +# shellcheck disable=SC2154 disable=SC2155 disable=SC2296 disable=SC2301 |
| 40 | + |
| 41 | +__gp_color() { |
| 42 | + if [ -n "$GP_COLORS" ]; then |
| 43 | + [ -n "$ZSH_VERSION" ] && echo -ne "%{\e[$1m%}" || echo -ne "\001\033[$1m\002" |
| 44 | + fi |
| 45 | +} |
| 46 | + |
| 47 | +# param 1: dir |
| 48 | +# param 2 (test only): output of git status -sb |
| 49 | +# param 3 (test only): non-empty for dirty state |
| 50 | +__gp_status() { |
| 51 | + |
| 52 | + # colors |
| 53 | + local COLOR_BRANCH=$(__gp_color "${GP_COLOR_GIT_BRANCH:-38;5;8}") |
| 54 | + local COLOR_STATUS=$(__gp_color "${GP_COLOR_GIT_STATUS:-38;5;9}") |
| 55 | + local COLOR_UNPUSHED=$(__gp_color "${GP_COLOR_GIT_UNPUSHED:-38;5;11}") |
| 56 | + local COLOR_RESET=$(__gp_color 0) |
| 57 | + |
| 58 | + local dir=${1:-.} git_status branch_info branch changes state remote ahead behind unpushed |
| 59 | + |
| 60 | + __parse() ( |
| 61 | + echo -n "$(set -o pipefail; sed -rn "$1" <<< "$branch_info")" |
| 62 | + ) |
| 63 | + |
| 64 | + # first line: branch, remote, ahead, behind |
| 65 | + # subsequent lines: local changes |
| 66 | + if ! git_status=${2:-"$(set -o pipefail; git status -sb 2>/dev/null)"}; then |
| 67 | + return 0 |
| 68 | + fi |
| 69 | + branch_info=$(head -n 1 <<< "$git_status") |
| 70 | + changes=$(echo "$git_status" | tail -n +2 | wc -l | xargs) # xargs trims spaces |
| 71 | + |
| 72 | + # parse state |
| 73 | + [ "$changes" -gt 0 ] && state="✗$changes" |
| 74 | + |
| 75 | + # parse branch |
| 76 | + branch=$(__parse "s/^## (HEAD \(no branch\))$/\1/p") |
| 77 | + branch=${branch:-$(__parse "s/^## No commits yet on (.*)$/\1/p")} |
| 78 | + branch=${branch:-$(__parse "s/^## ([^ \.]*).*$/\1/p")} |
| 79 | + |
| 80 | + # parse push state |
| 81 | + remote=$(__parse "s/^## .*[\.]{3}([^ ]*).*$/\1/p") |
| 82 | + ahead=$(__parse "s/^## .* \[.*ahead ([0-9]*).*$/\1/p") |
| 83 | + behind=$(__parse "s/^## .* \[.*behind ([0-9]*).*$/\1/p") |
| 84 | + unpushed=$( |
| 85 | + [ -n "$ahead" ] && echo -n "↑$ahead"; |
| 86 | + [ -n "$behind" ] && echo -n "↓$behind"; |
| 87 | + ) |
| 88 | + |
| 89 | + printf "%s⎇ %s%s%s%s%s%s" "${COLOR_BRANCH}" "${branch}$([ -n "$remote" ] && echo -n " → $remote")" "${COLOR_STATUS}" "${state}" "${COLOR_UNPUSHED}" "${unpushed}" "${COLOR_RESET}" |
| 90 | +} |
| 91 | + |
| 92 | +__gp_pwd() { |
| 93 | + local COLOR_DARK=$(__gp_color "${GP_COLOR_PWD_DARK:-1;38;5;24}") |
| 94 | + local COLOR_LIGHT=$(__gp_color "${GP_COLOR_PWD_LIGHT:-1;38;5;39}") |
| 95 | + local COLOR_RESET=$(__gp_color 0) |
| 96 | + local pwd="${PWD/#$HOME/~}" dir prefix suffix |
| 97 | + dir=$(dirname "$pwd" 2>/dev/null) |
| 98 | + prefix=$(if [[ "$dir" == "." ]] || [[ -z "$dir" ]]; then echo -n ""; elif [[ "$dir" == "/" ]]; then echo -n "/"; else echo -n "$dir/"; fi) |
| 99 | + dir=$(basename "$pwd" 2>/dev/null) |
| 100 | + suffix=$(if [[ "$dir" == "/" ]]; then echo -n ""; else echo -n "$dir"; fi) |
| 101 | + printf "%s%s%s%s%s" "${COLOR_DARK}" "${prefix/#\~/${COLOR_LIGHT}~${COLOR_DARK}}" "${COLOR_LIGHT}" "${suffix}" "${COLOR_RESET}" |
| 102 | +} |
| 103 | + |
| 104 | +__update_prompt() { |
| 105 | + emulate -L zsh |
| 106 | + zmodload -i zsh/sched |
| 107 | + |
| 108 | + integer i=${"${(@)zsh_scheduled_events#*:*:}"[(I)schedprompt]} |
| 109 | + (( i )) && sched -"$i" |
| 110 | + |
| 111 | + if [ -n "$GP_UPDATE_PROMPT" ]; then |
| 112 | + zle && zle reset-prompt |
| 113 | + fi |
| 114 | + |
| 115 | + sched +1 __update_prompt |
| 116 | +} |
| 117 | + |
| 118 | +export TERM=xterm-256color |
| 119 | + |
| 120 | +GP_COLORS=true |
| 121 | + |
| 122 | +if [ -n "$ZSH_VERSION" ]; then |
| 123 | + setopt PROMPT_SUBST |
| 124 | + export PROMPT="%\$((\$COLUMNS / 2))<…<\$(__gp_pwd)\$(__gp_status)\$(__gp_color \${GP_COLOR_PROMPT:-38;5;49}) ❯$(__gp_color 0) " |
| 125 | + export RPROMPT="\$(__gp_color \${GP_COLOR_CLOCK:-38;5;99})%D{%K:%M:%S}$(__gp_color 0)" |
| 126 | + GP_UPDATE_PROMPT=true ; __update_prompt |
| 127 | +else |
| 128 | + export PS1="\$(__gp_pwd)\$(__gp_status)\$(__gp_color \${GP_COLOR_PROMPT:-38;5;49}) ❯$(__gp_color 0) " |
| 129 | +fi |
0 commit comments