home-config/fish/functions/fish_prompt.fish

adf9468e7a4dcd88f38233e9fc99f99610f080bc · 6.6 KB · 171 lines raw

1 function fish_prompt
2 end # In case this file gets loaded non-interactively, e.g by conda
3 status is-interactive || exit
4
5 _tide_remove_unusable_items
6 _tide_cache_variables
7 _tide_parent_dirs
8 source (functions --details _tide_pwd)
9
10 set -l prompt_var _tide_prompt_$fish_pid
11 set -U $prompt_var # Set var here so if we erase $prompt_var, bg job won't set a uvar
12
13 set_color normal | read -l color_normal
14 status fish-path | read -l fish_path
15
16 # _tide_repaint prevents us from creating a second background job
17 function _tide_refresh_prompt --on-variable $prompt_var --on-variable COLUMNS
18 set -g _tide_repaint
19 commandline -f repaint
20 end
21
22 if contains newline $_tide_left_items # two line prompt initialization
23 test "$tide_prompt_add_newline_before" = true && set -l add_newline '\n'
24
25 set_color $tide_prompt_color_frame_and_connection -b normal | read -l prompt_and_frame_color
26
27 set -l column_offset 5
28 test "$tide_left_prompt_frame_enabled" = true &&
29 set -l top_left_frame "$prompt_and_frame_color╭─" &&
30 set -l bot_left_frame "$prompt_and_frame_color╰─" &&
31 set column_offset (math $column_offset-2)
32 test "$tide_right_prompt_frame_enabled" = true &&
33 set -l top_right_frame "$prompt_and_frame_color─╮" &&
34 set -l bot_right_frame "$prompt_and_frame_color─╯" &&
35 set column_offset (math $column_offset-2)
36
37 if test "$tide_prompt_transient_enabled" = true
38 eval "
39 function fish_prompt
40 _tide_status=\$status _tide_pipestatus=\$pipestatus if not set -e _tide_repaint
41 jobs -q && jobs -p | count | read -lx _tide_jobs
42 $fish_path -c \"set _tide_pipestatus \$_tide_pipestatus
43 set _tide_parent_dirs \$_tide_parent_dirs
44 PATH=\$(string escape \"\$PATH\") CMD_DURATION=\$CMD_DURATION fish_bind_mode=\$fish_bind_mode set $prompt_var (_tide_2_line_prompt)\" &
45 builtin disown
46
47 command kill \$_tide_last_pid 2>/dev/null
48 set -g _tide_last_pid \$last_pid
49 end
50
51 if not set -q _tide_transient
52 math \$COLUMNS-(string length -V \"\$$prompt_var[1][1]\$$prompt_var[1][3]\")+$column_offset | read -lx dist_btwn_sides
53
54 echo -n $add_newline'$top_left_frame'(string replace @PWD@ (_tide_pwd) \"\$$prompt_var[1][1]\")'$prompt_and_frame_color'
55 string repeat -Nm(math max 0, \$dist_btwn_sides-\$_tide_pwd_len) '$tide_prompt_icon_connection'
56
57 echo \"\$$prompt_var[1][3]$top_right_frame\"
58 end
59 echo -n \e\[0J\"$bot_left_frame\$$prompt_var[1][2]$color_normal \"
60 end
61
62 function fish_right_prompt
63 set -e _tide_transient || string unescape \"\$$prompt_var[1][4]$bot_right_frame$color_normal\"
64 end"
65 else
66 eval "
67 function fish_prompt
68 _tide_status=\$status _tide_pipestatus=\$pipestatus if not set -e _tide_repaint
69 jobs -q && jobs -p | count | read -lx _tide_jobs
70 $fish_path -c \"set _tide_pipestatus \$_tide_pipestatus
71 set _tide_parent_dirs \$_tide_parent_dirs
72 PATH=\$(string escape \"\$PATH\") CMD_DURATION=\$CMD_DURATION fish_bind_mode=\$fish_bind_mode set $prompt_var (_tide_2_line_prompt)\" &
73 builtin disown
74
75 command kill \$_tide_last_pid 2>/dev/null
76 set -g _tide_last_pid \$last_pid
77 end
78
79 math \$COLUMNS-(string length -V \"\$$prompt_var[1][1]\$$prompt_var[1][3]\")+$column_offset | read -lx dist_btwn_sides
80
81 echo -ns $add_newline'$top_left_frame'(string replace @PWD@ (_tide_pwd) \"\$$prompt_var[1][1]\")'$prompt_and_frame_color'
82 string repeat -Nm(math max 0, \$dist_btwn_sides-\$_tide_pwd_len) '$tide_prompt_icon_connection'
83 echo -ns \"\$$prompt_var[1][3]$top_right_frame\"\n\"$bot_left_frame\$$prompt_var[1][2]$color_normal \"
84 end
85
86 function fish_right_prompt
87 string unescape \"\$$prompt_var[1][4]$bot_right_frame$color_normal\"
88 end"
89 end
90 else # one line prompt initialization
91 test "$tide_prompt_add_newline_before" = true && set -l add_newline '\0'
92
93 math 5 -$tide_prompt_min_cols | read -l column_offset
94 test $column_offset -ge 0 && set column_offset "+$column_offset"
95
96 if test "$tide_prompt_transient_enabled" = true
97 eval "
98 function fish_prompt
99 set -lx _tide_status \$status
100 _tide_pipestatus=\$pipestatus if not set -e _tide_repaint
101 jobs -q && jobs -p | count | read -lx _tide_jobs
102 $fish_path -c \"set _tide_pipestatus \$_tide_pipestatus
103 set _tide_parent_dirs \$_tide_parent_dirs
104 PATH=\$(string escape \"\$PATH\") CMD_DURATION=\$CMD_DURATION fish_bind_mode=\$fish_bind_mode set $prompt_var (_tide_1_line_prompt)\" &
105 builtin disown
106
107 command kill \$_tide_last_pid 2>/dev/null
108 set -g _tide_last_pid \$last_pid
109 end
110
111 if set -q _tide_transient
112 echo -n \e\[0J
113 add_prefix= _tide_item_character
114 echo -n '$color_normal '
115 else
116 math \$COLUMNS-(string length -V \"\$$prompt_var[1][1]\$$prompt_var[1][2]\")$column_offset | read -lx dist_btwn_sides
117 string replace @PWD@ (_tide_pwd) $add_newline \$$prompt_var[1][1]'$color_normal '
118 end
119 end
120
121 function fish_right_prompt
122 set -e _tide_transient || string unescape \"\$$prompt_var[1][2]$color_normal\"
123 end"
124 else
125 eval "
126 function fish_prompt
127 _tide_status=\$status _tide_pipestatus=\$pipestatus if not set -e _tide_repaint
128 jobs -q && jobs -p | count | read -lx _tide_jobs
129 $fish_path -c \"set _tide_pipestatus \$_tide_pipestatus
130 set _tide_parent_dirs \$_tide_parent_dirs
131 PATH=\$(string escape \"\$PATH\") CMD_DURATION=\$CMD_DURATION fish_bind_mode=\$fish_bind_mode set $prompt_var (_tide_1_line_prompt)\" &
132 builtin disown
133
134 command kill \$_tide_last_pid 2>/dev/null
135 set -g _tide_last_pid \$last_pid
136 end
137
138 math \$COLUMNS-(string length -V \"\$$prompt_var[1][1]\$$prompt_var[1][2]\")$column_offset | read -lx dist_btwn_sides
139 string replace @PWD@ (_tide_pwd) $add_newline \$$prompt_var[1][1]'$color_normal '
140 end
141
142 function fish_right_prompt
143 string unescape \"\$$prompt_var[1][2]$color_normal\"
144 end"
145 end
146 end
147
148 eval "function _tide_on_fish_exit --on-event fish_exit
149 set -e $prompt_var
150 end"
151
152 if test "$tide_prompt_transient_enabled" = true
153 function _tide_enter_transient
154 # If the commandline will be executed, or is empty
155 if commandline --is-valid || test -z "$(commandline)"
156 # Pager open usually means selecting, not running
157 # Can be untrue, but it's better than the alternative
158 if not commandline --paging-mode
159 set -g _tide_transient
160 set -g _tide_repaint
161 commandline -f repaint
162 end
163 end
164 commandline -f execute
165 end
166
167 bind \r _tide_enter_transient
168 bind \n _tide_enter_transient
169 bind -M insert \r _tide_enter_transient
170 bind -M insert \n _tide_enter_transient
171 end