-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpowerline-shell.bash
96 lines (74 loc) · 3.67 KB
/
powerline-shell.bash
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/usr/bin/env bash
__ps_main() {
readonly __ps_branch_indicator='⎇'
readonly __ps_clean_indicator='✓'
readonly __ps_dirty_indicator='✗'
readonly __ps_staged_indicator='+'
readonly __ps_unstaged_indicator='-'
readonly __ps_untracked_indicator='?'
readonly __ps_stash_indicator='¢'
readonly __ps_pull_indicator='⇣'
readonly __ps_push_indicator='⇡'
readonly __ps_color_fg_base="\\[$(tput setaf 0)\\]"
readonly __ps_color_bg_base="\\[$(tput setab 7)\\]"
readonly __ps_color_bg_exit_nonzero="\\[$(tput setab 1)\\]"
readonly __ps_color_bg_exit_zero="\\[$(tput setab 2)\\]"
readonly __ps_color_bg_git="\\[$(tput setab 7)\\]"
readonly __ps_color_reset="\\[$(tput sgr0)\\]"
__ps_repository_status() {
hash git 2>/dev/null || return
local -r branch=$(git symbolic-ref --short HEAD 2>/dev/null || git describe --tags --always 2>/dev/null)
[[ -n "${branch}" ]] || return
local -r git_status=$(git status --porcelain)
if [[ -z "${git_status}" ]]; then
local status_indicators="${__ps_clean_indicator}"
else
local status_indicators="${__ps_dirty_indicator}"
fi
local -r staged_files=$(echo "${git_status}" | grep -cE '^[A-Z]' | grep -Eo '\d+')
[[ "${staged_files}" -gt 0 ]] 2>/dev/null && status_indicators+=" ${__ps_staged_indicator}${staged_files}"
local -r unstaged_files=$(echo "${git_status}" | grep -cE '^.[A-Z]' | grep -Eo '\d+')
[[ "${unstaged_files}" -gt 0 ]] 2>/dev/null && status_indicators+=" ${__ps_unstaged_indicator}${unstaged_files}"
local -r untracked_files=$(echo "${git_status}" | grep -cE '^\?\?' | grep -Eo '\d+')
[[ "${untracked_files}" -gt 0 ]] 2>/dev/null && status_indicators+=" ${__ps_untracked_indicator}${untracked_files}"
local -r stash_items=$(git stash list | wc -l | grep -Eo '\d+')
[[ "${stash_items}" -gt 0 ]] 2>/dev/null && status_indicators+=" ${__ps_stash_indicator}${stash_items}"
local -r git_status_branch=$(git status --porcelain --branch)
local -r commits_behind=$(echo "${git_status_branch}" | grep -E '^##' | grep -Eo 'behind \d+' | grep -Eo '\d+')
[[ -n "${commits_behind}" ]] && status_indicators+=" ${__ps_pull_indicator}${commits_behind}"
local -r commits_ahead=$(echo "${git_status_branch}" | grep -E '^##' | grep -Eo 'ahead \d+' | grep -Eo '\d+')
[[ -n "${commits_ahead}" ]] && status_indicators+=" ${__ps_push_indicator}${commits_ahead}"
printf '%s %s %s ' "${__ps_branch_indicator}" "${branch}" "${status_indicators}"
}
__ps_ps1() {
# shellcheck disable=SC2181
if [[ $? -eq 0 ]]; then
PS1="${__ps_color_bg_exit_zero} "
else
PS1="${__ps_color_bg_exit_nonzero} "
fi
PS1+="${__ps_color_reset}${__ps_color_bg_base}${__ps_color_fg_base} "
local -r prefix=$(git rev-parse --show-prefix 2>/dev/null)
local -r toplevel=$(git rev-parse --show-toplevel 2>/dev/null)
local -r git_dir=$(git rev-parse --git-dir 2>/dev/null)
if [[ -n "$prefix" ]]; then
PS1+="$(basename "$toplevel")/${prefix%/}"
elif [[ -d "$git_dir" && "$toplevel" = "$(pwd)" ]]; then
PS1+="\\W"
else
PS1+="\\w"
fi
PS1+=" ${__ps_color_reset}${__ps_color_bg_git}${__ps_color_fg_base}"
# Description: 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
escaped_repository_status="$(__ps_repository_status)"
PS1+="\${escaped_repository_status}"
else
PS1+="$(__ps_repository_status)"
fi
PS1+="| \\A ${__ps_color_reset}\\nλ "
}
PROMPT_COMMAND="__ps_ps1; ${PROMPT_COMMAND}"
}
__ps_main && unset -f __ps_main