#!/bin/bash -n # ------------------------------------------------------------------------------ # (C)opyright 2022 by Daniel Dietrich - MIT license # ------------------------------------------------------------------------------ # # ------------------------------------------------------------------------------ # # Usage: # # | Command | Description | # | ------------------------------------------ | ----------------------------- | # | . <(curl gitprompt.sh) | Use gitprompt in Active shell | # | curl gitprompt.sh/install | sh | Install gitprompt to $HOME | # | https://github.com/danieldietrich/dotfiles | Gitpod dotfiles | # # Customize gitprompt by placing ENV vars in your .bashrc or .zshrc file. # # | ENV var | Default | Description | # | --------------------- | ------------ | ----------------------------------- | # | GP_COLOR_PROMPT | "38;5;49" | Prompt color | # | GP_COLOR_GIT_BRANCH | "38;5;8" | Branch color | # | GP_COLOR_GIT_STATUS | "38;5;9" | Dirty state color | # | GP_COLOR_GIT_UNPUSHED | "38;5;11" | Unpushed state color | # | GP_COLOR_PWD_DARK | "1;38;5;24" | Directory color dark | # | GP_COLOR_PWD_LIGHT | "1;38;5;39" | Directory color light | # # ------------------------------------------------------------------------------ # shellcheck disable=SC2154 disable=SC2155 disable=SC2296 disable=SC2301 __gp_color() { if [ -n "$ZSH_VERSION" ]; then echo -ne "%{\e[$1m%}" else echo -ne "\001\033[$1m\002" fi } # param 1: dir # param 2 (test only): output of git status -sb # param 3 (test only): non-empty for dirty state __gp_status() { # colors local COLOR_BRANCH=$(__gp_color "${GP_COLOR_GIT_BRANCH:-38;5;8}") local COLOR_STATUS=$(__gp_color "${GP_COLOR_GIT_STATUS:-38;5;9}") local COLOR_UNPUSHED=$(__gp_color "${GP_COLOR_GIT_UNPUSHED:-38;5;11}") local COLOR_RESET=$(__gp_color 0) local dir=${1:-.} git_status branch_info branch changes state remote ahead behind unpushed __parse() ( echo -n "$(sed -rn "$1" <<< "$branch_info")" ) # first line: branch, remote, ahead, behind # subsequent lines: local changes if ! git_status=${2:-"$(git status -sb 2>/dev/null)"}; then return 0 fi branch_info=$(head -n 1 <<< "$git_status") changes=$(echo "$git_status" | tail -n +2 | wc -l | xargs) # xargs trims spaces # parse state [ "$changes" -gt 0 ] && state="✗$changes" # parse branch branch=$(__parse "s/^## (HEAD \(no branch\))$/\1/p") branch=${branch:-$(__parse "s/^## No commits yet on (.*)$/\1/p")} branch=${branch:-$(__parse "s/^## (.*)[\.]{3}[^ ]*.*$/\1/p")} # branch...remote branch=${branch:-$(__parse "s/^## (.*)[^ ]*.*$/\1/p")} # branch # parse push state remote=$(__parse "s/^## .*[\.]{3}([^ ]*).*$/\1/p") ahead=$(__parse "s/^## .* \[.*ahead ([0-9]*).*$/\1/p") behind=$(__parse "s/^## .* \[.*behind ([0-9]*).*$/\1/p") unpushed=$( [ -n "$ahead" ] && echo -n "↑$ahead"; [ -n "$behind" ] && echo -n "↓$behind"; ) printf "%s⎇ %s%s%s%s%s%s" "${COLOR_BRANCH}" "${branch}$([ -n "$remote" ] && echo -n " → $remote")" "${COLOR_STATUS}" "${state}" "${COLOR_UNPUSHED}" "${unpushed}" "${COLOR_RESET}" } __gp_pwd() { local COLOR_DARK=$(__gp_color "${GP_COLOR_PWD_DARK:-1;38;5;24}") local COLOR_LIGHT=$(__gp_color "${GP_COLOR_PWD_LIGHT:-1;38;5;39}") local COLOR_RESET=$(__gp_color 0) local pwd="${PWD/#$HOME/~}" dir prefix suffix dir=$(dirname "$pwd" 2>/dev/null) prefix=$(if [[ "$dir" == "." ]] || [[ -z "$dir" ]]; then echo -n ""; elif [[ "$dir" == "/" ]]; then echo -n "/"; else echo -n "$dir/"; fi) dir=$(basename "$pwd" 2>/dev/null) suffix=$(if [[ "$dir" == "/" ]]; then echo -n ""; else echo -n "$dir"; fi) printf "%s%s%s%s%s" "${COLOR_DARK}" "${prefix/#\~/${COLOR_LIGHT}~${COLOR_DARK}}" "${COLOR_LIGHT}" "${suffix}" "${COLOR_RESET}" } export TERM=xterm-256color if [ -n "$ZSH_VERSION" ]; then setopt PROMPT_SUBST export PROMPT="%\$((\$COLUMNS / 2))<…<\$(__gp_pwd)\$(__gp_status)\$(__gp_color \${GP_COLOR_PROMPT:-38;5;49}) ❯$(__gp_color 0) " else export PS1="\$(__gp_pwd)\$(__gp_status)\$(__gp_color \${GP_COLOR_PROMPT:-38;5;49}) ❯$(__gp_color 0) " fi