-
Notifications
You must be signed in to change notification settings - Fork 0
/
eggshausted.plugin.zsh
199 lines (169 loc) · 5.17 KB
/
eggshausted.plugin.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# This is a zsh plugin modified from https://github.com/yous/lime
# Outputs -1, 0, or 1 if the installed git version is less than, equal to, or
# greater than the input version, respectively
git_compare_version() {
local input_version
local installed_version
input_version=(${(s/./)1})
installed_version=($(command git --version 2>/dev/null))
installed_version=(${(s/./)installed_version[3]})
for i in {1..3}; do
if [[ $installed_version[$i] -gt $input_version[$i] ]]; then
echo 1
return 0
fi
if [[ $installed_version[$i] -lt $input_version[$i] ]]; then
echo -1
return 0
fi
done
echo 0
}
prompt_egg_precmd() {
# Set title
prompt_egg_set_title
# Get VCS information
vcs_info
}
prompt_egg_preexec() {
# Show the current job
prompt_egg_set_title "$1"
}
prompt_egg_set_title() {
local window_title="$(prompt_egg_window_title)"
local tab_title="$(prompt_egg_tab_title "$@")"
# Inside screen or tmux
case "$TERM" in
screen*)
# Set window title
print -n '\e]0;'
echo -n "${window_title}"
print -n '\a'
# Set window name
print -n '\ek'
echo -n "${tab_title}"
print -n '\e\\'
;;
cygwin|putty*|rxvt*|xterm*)
# Set window title
print -n '\e]2;'
echo -n "${window_title}"
print -n '\a'
# Set tab name
print -n '\e]1;'
echo -n "${tab_title}"
print -n '\a'
;;
*)
# Set window title if it's available
zmodload zsh/terminfo
if [[ -n "$terminfo[tsl]" ]] && [[ -n "$terminfo[fsl]" ]]; then
echoti tsl
echo -n "${window_title}"
echoti fsl
fi
;;
esac
}
prompt_egg_window_title() {
# Username, hostname and current directory
print -Pn '%n@%m:'
}
prompt_egg_tab_title() {
if [ "$#" -eq 1 ]; then
echo -n "$(prompt_egg_first_command "$1")"
else
# `%40<..<` truncates following string longer than 40 characters with `..`.
# `%~` is current working directory with `~` instead of full `$HOME` path.
# `%<<` sets the end of string to truncate.
print -Pn '%40<..<%~%<<'
fi
}
prompt_egg_first_command() {
setopt local_options extended_glob
# Return the first command excluding env, options, sudo, ssh
print -rn ${1[(wr)^(*=*|-*|sudo|ssh)]:gs/%/%%}
}
prompt_egg_render() {
echo -n "${prompt_egg_rendered_user}"
echo -n ' '
prompt_egg_dir
echo -n ' '
prompt_egg_git
echo -n "${prompt_egg_rendered_symbol}"
echo -n ' '
prompt_egg_time
}
prompt_egg_user() {
echo -n "%F{${prompt_egg_default_user_color}}%n@%m%f"
}
prompt_egg_dir() {
echo -n "%(?.%F{${prompt_egg_default_dir_color}}.%F{${prompt_egg_default_error_color}})%~%f"
}
prompt_egg_git() {
# Store working_tree without the 'x' prefix
local working_tree="${vcs_info_msg_1_#x}"
[[ -n $working_tree ]] || return
echo -n "%F{${prompt_egg_default_git_color}}${vcs_info_msg_0_}$(prompt_egg_git_dirty)%f "
}
prompt_egg_git_dirty() {
local git_status_options
git_status_options=(--porcelain -unormal)
if [[ "$prompt_egg_git_post_1_7_2" -ge 0 ]]; then
git_status_options+=(--ignore-submodules=dirty)
fi
[ -n "$(command git status $git_status_options)" ] && echo -n '*'
}
prompt_egg_symbol() {
echo -n '%(?.\U1F423 .\U1F373 )'
}
prompt_egg_time() {
echo -n "%F{${prompt_egg_default_time_color}}%*"
}
prompt_egg_setup() {
precmd_functions+=(prompt_egg_precmd)
preexec_functions+=(prompt_egg_preexec)
autoload -Uz vcs_info
zstyle ':vcs_info:*' enable git
# Export only two msg variables from vcs_info
zstyle ':vcs_info:*' max-exports 2
# %s: The current version control system, like 'git' or 'svn'
# %r: The name of the root directory of the repository
# #S: The current path relative to the repository root directory
# %b: Branch information, like 'master'
# %m: In case of Git, show information about stashes
# %u: Show unstaged changes in the repository (works with 'check-for-changes')
# %c: Show staged changes in the repository (works with 'check-for-changes')
#
# vcs_info_msg_0_ = '%b'
# vcs_info_msg_1_ = 'x%r' x-prefix prevents creation of a named path
# (AUTO_NAME_DIRS)
zstyle ':vcs_info:git*' formats '%b' 'x%r'
# '%a' is for action like 'rebase', 'rebase-i', 'merge'
zstyle ':vcs_info:git*' actionformats '%b(%a)' 'x%r'
# The '--ignore-submodules' option is introduced on git 1.7.2
prompt_egg_git_post_1_7_2=$(git_compare_version '1.7.2')
# Support 8 colors
if [[ "$TERM" = *"256color" ]]; then
prompt_egg_default_user_color=109
prompt_egg_default_dir_color=143
prompt_egg_default_git_color=109
prompt_egg_default_error_color=124
prompt_egg_default_time_color=109
else
prompt_egg_default_user_color=cyan
prompt_egg_default_dir_color=green
prompt_egg_default_git_color=cyan
prompt_egg_default_error_color=red
prompt_egg_default_time_color=cyan
fi
prompt_egg_rendered_user="$(prompt_egg_user)"
prompt_egg_rendered_symbol="$(prompt_egg_symbol)"
# If set, parameter expansion, command substitution and arithmetic expansion
# is performed in prompts
setopt prompt_subst
PROMPT='$(prompt_egg_render) '
}
prompt_egg_setup
# Clean up the namespace
unfunction git_compare_version