-
Notifications
You must be signed in to change notification settings - Fork 139
/
passion.zsh-theme
214 lines (180 loc) · 6.38 KB
/
passion.zsh-theme
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# gdate for macOS
# REF: https://apple.stackexchange.com/questions/135742/time-in-milliseconds-since-epoch-in-the-terminal
if [[ "$OSTYPE" == "darwin"* ]]; then
{
gdate
} || {
echo "\n$fg_bold[yellow]passion.zsh-theme depends on cmd [gdate] to get current time in milliseconds$reset_color"
echo "$fg_bold[yellow][gdate] is not installed by default in macOS$reset_color"
echo "$fg_bold[yellow]to get [gdate] by running:$reset_color"
echo "$fg_bold[green]brew install coreutils;$reset_color";
echo "$fg_bold[yellow]\nREF: https://github.com/ChesterYue/ohmyzsh-theme-passion#macos\n$reset_color"
}
fi
# time
function real_time() {
local color="%{$fg_no_bold[cyan]%}"; # color in PROMPT need format in %{XXX%} which is not same with echo
local time="[$(date +%H:%M:%S)]";
local color_reset="%{$reset_color%}";
echo "${color}${time}${color_reset}";
}
# login_info
function login_info() {
local color="%{$fg_no_bold[cyan]%}"; # color in PROMPT need format in %{XXX%} which is not same with echo
local ip
if [[ "$OSTYPE" == "linux-gnu" ]]; then
# Linux
ip="$(ifconfig | grep ^eth1 -A 1 | grep -o '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}' | head -1)";
elif [[ "$OSTYPE" == "darwin"* ]]; then
# macOS
ip="$(ifconfig | grep ^en1 -A 4 | grep -o '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}' | head -1)";
elif [[ "$OSTYPE" == "cygwin" ]]; then
# POSIX compatibility layer and Linux environment emulation for Windows
elif [[ "$OSTYPE" == "msys" ]]; then
# Lightweight shell and GNU utilities compiled for Windows (part of MinGW)
elif [[ "$OSTYPE" == "win32" ]]; then
# I'm not sure this can happen.
elif [[ "$OSTYPE" == "freebsd"* ]]; then
# ...
else
# Unknown.
fi
local color_reset="%{$reset_color%}";
echo "${color}[%n@${ip}]${color_reset}";
}
# directory
function directory() {
local color="%{$fg_no_bold[cyan]%}";
# REF: https://stackoverflow.com/questions/25944006/bash-current-working-directory-with-replacing-path-to-home-folder
local directory="${PWD/#$HOME/~}";
local color_reset="%{$reset_color%}";
echo "${color}[${directory}]${color_reset}";
}
# git
ZSH_THEME_GIT_PROMPT_PREFIX="%{$fg_no_bold[cyan]%}[";
ZSH_THEME_GIT_PROMPT_SUFFIX="%{$reset_color%} ";
ZSH_THEME_GIT_PROMPT_DIRTY=" %{$fg_no_bold[red]%}✖%{$fg_no_bold[cyan]%}]";
ZSH_THEME_GIT_PROMPT_CLEAN="%{$fg_no_bold[cyan]%}]";
function update_git_status() {
GIT_STATUS=$(_omz_git_prompt_info);
}
function git_status() {
echo "${GIT_STATUS}"
}
# command
function update_command_status() {
local arrow="";
local color_reset="%{$reset_color%}";
local reset_font="%{$fg_no_bold[white]%}";
COMMAND_RESULT=$1;
export COMMAND_RESULT=$COMMAND_RESULT
if $COMMAND_RESULT;
then
arrow="%{$fg_bold[red]%}❱%{$fg_bold[yellow]%}❱%{$fg_bold[green]%}❱";
else
arrow="%{$fg_bold[red]%}❱❱❱";
fi
COMMAND_STATUS="${arrow}${reset_font}${color_reset}";
}
update_command_status true;
function command_status() {
echo "${COMMAND_STATUS}"
}
# output command execute after
output_command_execute_after() {
if [ "$COMMAND_TIME_BEGIN" = "-20200325" ] || [ "$COMMAND_TIME_BEGIN" = "" ];
then
return 1;
fi
# cmd
local cmd="$(fc -ln -1)";
local color_cmd="";
if $1;
then
color_cmd="$fg_no_bold[green]";
else
color_cmd="$fg_bold[red]";
fi
local color_reset="$reset_color";
cmd="${color_cmd}${cmd}${color_reset}"
# time
local time="[$(date +%H:%M:%S)]"
local color_time="$fg_no_bold[cyan]";
time="${color_time}${time}${color_reset}";
# cost
local time_end="$(current_time_millis)";
local cost=$(bc -l <<<"${time_end}-${COMMAND_TIME_BEGIN}");
COMMAND_TIME_BEGIN="-20200325"
local length_cost=${#cost};
if [ "$length_cost" = "4" ];
then
cost="0${cost}"
fi
cost="[cost ${cost}s]"
local color_cost="$fg_no_bold[cyan]";
cost="${color_cost}${cost}${color_reset}";
echo -e "${time} ${cost} ${cmd}";
echo -e "";
}
# command execute before
# REF: http://zsh.sourceforge.net/Doc/Release/Functions.html
preexec() { # cspell:disable-line
COMMAND_TIME_BEGIN="$(current_time_millis)";
}
current_time_millis() {
local time_millis;
if [[ "$OSTYPE" == "linux-gnu" ]]; then
# Linux
time_millis="$(date +%s.%3N)";
elif [[ "$OSTYPE" == "darwin"* ]]; then
# macOS
time_millis="$(gdate +%s.%3N)";
elif [[ "$OSTYPE" == "cygwin" ]]; then
# POSIX compatibility layer and Linux environment emulation for Windows
elif [[ "$OSTYPE" == "msys" ]]; then
# Lightweight shell and GNU utilities compiled for Windows (part of MinGW)
elif [[ "$OSTYPE" == "win32" ]]; then
# I'm not sure this can happen.
elif [[ "$OSTYPE" == "freebsd"* ]]; then
# ...
else
# Unknown.
fi
echo $time_millis;
}
# command execute after
# REF: http://zsh.sourceforge.net/Doc/Release/Functions.html
precmd() { # cspell:disable-line
# last_cmd
local last_cmd_return_code=$?;
local last_cmd_result=true;
if [ "$last_cmd_return_code" = "0" ];
then
last_cmd_result=true;
else
last_cmd_result=false;
fi
# update_git_status
update_git_status;
# update_command_status
update_command_status $last_cmd_result;
# output command execute after
output_command_execute_after $last_cmd_result;
}
# set option
setopt PROMPT_SUBST; # cspell:disable-line
# timer
#REF: https://stackoverflow.com/questions/26526175/zsh-menu-completion-causes-problems-after-zle-reset-prompt
TMOUT=1;
TRAPALRM() { # cspell:disable-line
# $(git_prompt_info) cost too much time which will raise stutters when inputting. so we need to disable it in this occurrence.
# if [ "$WIDGET" != "expand-or-complete" ] && [ "$WIDGET" != "self-insert" ] && [ "$WIDGET" != "backward-delete-char" ]; then
# black list will not enum it completely. even some pipe broken will appear.
# so we just put a white list here.
if [ "$WIDGET" = "" ] || [ "$WIDGET" = "accept-line" ] ; then
zle reset-prompt;
fi
}
# prompt
# PROMPT='$(real_time) $(login_info) $(directory) $(git_status)$(command_status) ';
PROMPT='$(real_time) $(directory) $(git_status)$(command_status) ';