forked from oh-my-fish/theme-agnoster
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fish_prompt.fish
266 lines (231 loc) · 7.61 KB
/
fish_prompt.fish
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
# name: Agnoster
# agnoster's Theme - https://gist.github.com/3712874
# A Powerline-inspired theme for FISH
#
# # README
#
# In order for this theme to render correctly, you will need a
# [Powerline-patched font](https://gist.github.com/1595572).
## Set this options in your config.fish (if you want to :])
# set -g theme_display_user yes
# set -g theme_hide_hostname yes
# set -g theme_hide_hostname no
# set -g default_user your_normal_user
set -g current_bg NONE
set segment_separator \uE0B0
set right_segment_separator \uE0B0
# ===========================
# Color setting
# You can set these variables in config.fish like:
# set -g color_dir_bg red
# If not set, default color from agnoster will be used.
# ===========================
set -q color_virtual_env_bg; or set color_virtual_env_bg white
set -q color_virtual_env_str; or set color_virtual_env_str black
set -q color_user_bg; or set color_user_bg black
set -q color_user_str; or set color_user_str yellow
set -q color_dir_bg; or set color_dir_bg blue
set -q color_dir_str; or set color_dir_str black
set -q color_hg_removed_bg; or set color_hg_removed_bg red
set -q color_hg_removed_str; or set color_hg_removed_str white
set -q color_hg_untracked_bg; or set color_hg_untracked_bg yellow
set -q color_hg_untracked_str; or set color_hg_untracked_str black
set -q color_hg_bg; or set color_hg_bg green
set -q color_hg_str; or set color_hg_str black
set -q color_git_dirty_bg; or set color_git_dirty_bg yellow
set -q color_git_dirty_str; or set color_git_dirty_str black
set -q color_git_bg; or set color_git_bg green
set -q color_git_str; or set color_git_str black
set -q color_svn_bg; or set color_svn_bg green
set -q color_svn_str; or set color_svn_str black
set -q color_status_nonzero_bg; or set color_status_nonzero_bg black
set -q color_status_nonzero_str; or set color_status_nonzero_str red
set -q color_status_superuser_bg; or set color_status_superuser_bg black
set -q color_status_superuser_str; or set color_status_superuser_str yellow
set -q color_status_jobs_bg; or set color_status_jobs_bg black
set -q color_status_jobs_str; or set color_status_jobs_str cyan
# ===========================
# Helper methods
# ===========================
set -g __fish_git_prompt_showdirtystate 'yes'
set -g __fish_git_prompt_char_dirtystate '±'
set -g __fish_git_prompt_char_cleanstate ''
function parse_git_dirty
set -l submodule_syntax
set submodule_syntax "--ignore-submodules=dirty"
set git_dirty (command git status --porcelain $submodule_syntax 2> /dev/null)
if [ -n "$git_dirty" ]
if [ $__fish_git_prompt_showdirtystate = "yes" ]
echo -n "$__fish_git_prompt_char_dirtystate"
end
else
if [ $__fish_git_prompt_showdirtystate = "yes" ]
echo -n "$__fish_git_prompt_char_cleanstate"
end
end
end
# ===========================
# Segments functions
# ===========================
function prompt_segment -d "Function to draw a segment"
set -l bg
set -l fg
if [ -n "$argv[1]" ]
set bg $argv[1]
else
set bg normal
end
if [ -n "$argv[2]" ]
set fg $argv[2]
else
set fg normal
end
if [ "$current_bg" != 'NONE' -a "$argv[1]" != "$current_bg" ]
set_color -b $bg
set_color $current_bg
echo -n "$segment_separator "
set_color -b $bg
set_color $fg
else
set_color -b $bg
set_color $fg
echo -n " "
end
set current_bg $argv[1]
if [ -n "$argv[3]" ]
echo -n -s $argv[3] " "
end
end
function prompt_finish -d "Close open segments"
if [ -n $current_bg ]
set_color -b normal
set_color $current_bg
echo -n "$segment_separator "
end
set -g current_bg NONE
end
# ===========================
# Theme components
# ===========================
function prompt_virtual_env -d "Display Python virtual environment"
if test "$VIRTUAL_ENV"
prompt_segment $color_virtual_env_bg $color_virtual_env_str (basename $VIRTUAL_ENV)
end
end
function prompt_user -d "Display current user if different from $default_user"
if [ "$theme_display_user" = "yes" ]
if [ "$USER" != "$default_user" -o -n "$SSH_CLIENT" ]
set USER (whoami)
get_hostname
if [ $HOSTNAME_PROMPT ]
set USER_PROMPT $USER@$HOSTNAME_PROMPT
else
set USER_PROMPT $USER
end
prompt_segment $color_user_bg $color_user_str $USER_PROMPT
end
else
get_hostname
if [ $HOSTNAME_PROMPT ]
prompt_segment $color_user_bg $color_user_str $HOSTNAME_PROMPT
end
end
end
function get_hostname -d "Set current hostname to prompt variable $HOSTNAME_PROMPT if connected via SSH"
set -g HOSTNAME_PROMPT ""
if [ "$theme_hide_hostname" = "no" -o \( "$theme_hide_hostname" != "yes" -a -n "$SSH_CLIENT" \) ]
set -g HOSTNAME_PROMPT (hostname)
end
end
function prompt_dir -d "Display the current directory"
prompt_segment $color_dir_bg $color_dir_str (prompt_pwd)
end
function prompt_hg -d "Display mercurial state"
set -l branch
set -l state
if command hg id >/dev/null 2>&1
if command hg prompt >/dev/null 2>&1
set branch (command hg prompt "{branch}")
set state (command hg prompt "{status}")
set branch_symbol \uE0A0
if [ "$state" = "!" ]
prompt_segment $color_hg_removed_bg $color_hg_removed_str "$branch_symbol $branch ±"
else if [ "$state" = "?" ]
prompt_segment $color_hg_untracked_bg $color_hg_untracked_str "$branch_symbol $branch ±"
else
prompt_segment $color_hg_bg $color_hg_str "$branch_symbol $branch"
end
end
end
end
function prompt_git -d "Display the current git state"
set -l ref
set -l dirty
if command git rev-parse --is-inside-work-tree >/dev/null 2>&1
set dirty (parse_git_dirty)
set ref (command git symbolic-ref HEAD 2> /dev/null)
if [ $status -gt 0 ]
set -l branch (command git show-ref --head -s --abbrev |head -n1 2> /dev/null)
set ref "➦ $branch "
end
set branch_symbol \uE0A0
set -l branch (echo $ref | sed "s-refs/heads/-$branch_symbol -")
if [ "$dirty" != "" ]
prompt_segment $color_git_dirty_bg $color_git_dirty_str "$branch $dirty"
else
prompt_segment $color_git_bg $color_git_str "$branch $dirty"
end
end
end
function prompt_svn -d "Display the current svn state"
set -l ref
if command svn ls . >/dev/null 2>&1
set branch (svn_get_branch)
set branch_symbol \uE0A0
set revision (svn_get_revision)
prompt_segment $color_svn_bg $color_svn_str "$branch_symbol $branch:$revision"
end
end
function svn_get_branch -d "get the current branch name"
svn info 2> /dev/null | awk -F/ \
'/^URL:/ { \
for (i=0; i<=NF; i++) { \
if ($i == "branches" || $i == "tags" ) { \
print $(i+1); \
break;\
}; \
if ($i == "trunk") { print $i; break; } \
} \
}'
end
function svn_get_revision -d "get the current revision number"
svn info 2> /dev/null | sed -n 's/Revision:\ //p'
end
function prompt_status -d "the symbols for a non zero exit status, root and background jobs"
if [ $RETVAL -ne 0 ]
prompt_segment $color_status_nonzero_bg $color_status_nonzero_str "✘"
end
# if superuser (uid == 0)
set -l uid (id -u $USER)
if [ $uid -eq 0 ]
prompt_segment $color_status_superuser_bg $color_status_superuser_str "⚡"
end
# Jobs display
if [ (jobs -l | wc -l) -gt 0 ]
prompt_segment $color_status_jobs_bg $color_status_jobs_str "⚙"
end
end
# ===========================
# Apply theme
# ===========================
function fish_prompt
set -g RETVAL $status
prompt_status
prompt_virtual_env
prompt_user
prompt_dir
type -q hg; and prompt_hg
type -q git; and prompt_git
type -q svn; and prompt_svn
prompt_finish
end