-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
powerless.zsh
96 lines (79 loc) · 2.17 KB
/
powerless.zsh
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
################################################
### This is the ZSH powerless main script.
###
### How to call: source powerless.zsh <ENABLE_CUSTOM_COLORS>
################################################
# Set options and settings.
setopt PROMPT_SUBST
setopt PROMPT_SP
# Specify colors.
if [[ $1 == true ]]; then
color_text="0"
color_user_host="79"
color_code_wrong="196"
color_pwd="75"
color_git_ok="79"
color_git_dirty="203"
color_venv="33"
else
color_text="black"
color_user_host="green"
color_code_wrong="red"
color_pwd="blue"
color_git_ok="green"
color_git_dirty="red"
color_venv="yellow"
fi
# Specify common variables.
prompt_char='$'
rc='%{%f%k%}'
get-user-host() {
[[ -n "$SSH_CLIENT" ]] && echo -n "%{%F{$1}%K{$2}%} %n@%M $rc"
}
get-pwd() {
echo -n "%{%F{$1}%K{$2}%} %~ $rc"
}
get-git-info() {
local git_branch=$(git symbolic-ref --short HEAD 2> /dev/null)
if [[ -n "$git_branch" ]]; then
git diff --quiet --ignore-submodules --exit-code HEAD > /dev/null 2>&1
if [[ "$?" != "0" ]]; then
git_symbols="❗ "
back_color=$3
else
back_color=$2
fi
echo -n "%{%F{$1}%K{$back_color}%} $git_branch $git_symbols$rc"
fi
}
get-last-code() {
[[ (-n "$last_code") && ($last_code -ne 0) ]] && echo -n "%{%F{$1}%K{$2}%} ✘ $last_code $rc"
}
get-venv-info() {
if [ -n "$VIRTUAL_ENV" ]; then
echo -n "%{%F{$1}%K{$2}%} $(basename $VIRTUAL_ENV) $rc"
fi
}
get-prompt() {
echo -n "\n" && ([[ "$(print -P "%#")" == "#" ]] && echo -n "%{%F{$color_code_wrong}%} $prompt_char$rc%{\e[0m%} " || echo -n " $prompt_char%{\e[0m%} " )
}
powerless-prompt() {
get-user-host $color_text $color_user_host
get-last-code $color_text $color_code_wrong
get-pwd $color_text $color_pwd
get-git-info $color_text $color_git_ok $color_git_dirty
get-venv-info $color_text $color_venv
get-prompt
}
precmd-powerless() {
last_code=$?
if [[ $is_first_prompt -eq 999 ]]; then
echo -n "\n"
else
is_first_prompt=999
fi
}
# Attach the hook functions.
[[ ${precmd_functions[(r)precmd-powerless]} != "precmd-powerless" ]] && precmd_functions+=(precmd-powerless)
# Set the prompts.
PROMPT='$(powerless-prompt)'