Skip to content

Commit 035c3f6

Browse files
lemtoccodex
andcommitted
fix(shell): stabilize fish prompt status
- Show non-zero command status in the fish prompt with a red indicator and prompt symbol - Keep git repaint handling asynchronous while avoiding stale status during new commands - Ignore startup status on the first render so exec fish opens cleanly Co-authored-by: Codex <codex@openai.com>
1 parent 8ac4f52 commit 035c3f6

1 file changed

Lines changed: 49 additions & 3 deletions

File tree

home/shell/fish.nix

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
test -n "$XDG_CACHE_HOME"; and set prompt_cache_home "$XDG_CACHE_HOME"
2626
set -g __prompt_session (random)(random)
2727
set -g __prompt_tick 0
28+
set -g __prompt_first_render 1
2829
set -g __prompt_git_cache_file "$prompt_cache_home/fish/prompt-git-$fish_pid-$__prompt_session"
2930
mkdir -p (dirname $__prompt_git_cache_file)
3031
@@ -64,7 +65,7 @@
6465
__prompt_bump_tick = {
6566
onEvent = "fish_preexec";
6667
body = ''
67-
set -g __prompt_tick (math $__prompt_tick + 1)
68+
set -e __prompt_git_repaint
6869
6970
if string match --quiet --regex '^\s*(command\s+)?clear(\s|$)' -- "$argv[1]"
7071
set -g __prompt_suppress_next_git_repaint 1
@@ -79,10 +80,16 @@
7980
'';
8081
};
8182

83+
__prompt_clear_git_repaint = {
84+
onEvent = "fish_postexec";
85+
body = ''
86+
set -e __prompt_git_repaint
87+
'';
88+
};
89+
8290
__prompt_reset_git = {
8391
onVariable = "PWD";
8492
body = ''
85-
set -g __prompt_tick (math $__prompt_tick + 1)
8693
set -e __prompt_git_root
8794
set -e __prompt_git_text
8895
set -e __prompt_git_loaded_root
@@ -107,20 +114,29 @@
107114
set -l text (string join " " $lines[3..-1])
108115
109116
if test "$PWD" != "$root"; and not string match --quiet -- "$root/*" "$PWD"
117+
set -e __prompt_git_pid
118+
return
119+
end
120+
121+
if test "$tick" != "$__prompt_tick"
122+
set -e __prompt_git_pid
123+
set -g __prompt_git_repaint 1
124+
commandline -f repaint 2>/dev/null
110125
return
111126
end
112127
113128
set -g __prompt_git_root $root
114129
set -g __prompt_git_text $text
115130
set -g __prompt_git_loaded_root $root
116-
set -g __prompt_git_loaded_tick $__prompt_tick
131+
set -g __prompt_git_loaded_tick $tick
117132
set -e __prompt_git_pid
118133
119134
if set -q __prompt_suppress_next_git_repaint
120135
set -e __prompt_suppress_next_git_repaint
121136
return
122137
end
123138
139+
set -g __prompt_git_repaint 1
124140
commandline -f repaint 2>/dev/null
125141
'';
126142
};
@@ -281,6 +297,23 @@
281297
'';
282298

283299
fish_prompt = ''
300+
set -l prompt_status $status $pipestatus
301+
set -l last_status $prompt_status[1]
302+
set -l last_pipestatus $prompt_status[2..-1]
303+
test -n "$last_status"; or set last_status 0
304+
test (count $last_pipestatus) -gt 0; or set last_pipestatus $last_status
305+
306+
if set -q __prompt_git_repaint
307+
set -e __prompt_git_repaint
308+
else
309+
if test "$__prompt_first_render" = 1
310+
set -g __prompt_first_render 0
311+
set last_status 0
312+
set last_pipestatus 0
313+
end
314+
set -g __prompt_tick (math $__prompt_tick + 1)
315+
end
316+
284317
set -l reset (set_color normal)
285318
set -l time_color (set_color brblack)
286319
set -l user_color (set_color brcyan)
@@ -289,6 +322,7 @@
289322
set -l aws_color (set_color yellow)
290323
set -l duration_color (set_color brblack)
291324
set -l prompt_color (set_color normal)
325+
set -l error_color (set_color brred)
292326
293327
set -l parts "$time_color"(command date '+%H:%M')"$reset"
294328
@@ -313,6 +347,18 @@
313347
set --append parts "$duration_color$duration$reset"
314348
end
315349
350+
set -l status_text
351+
if test "$last_status" -ne 0
352+
if test (count $last_pipestatus) -gt 1
353+
set status_text "["(string join "|" $last_pipestatus)"]"
354+
else
355+
set status_text "[$last_status]"
356+
end
357+
358+
set --append parts "$error_color$status_text$reset"
359+
set prompt_color "$error_color"
360+
end
361+
316362
printf '%s\n%s ' (string join ' ' $parts) "$prompt_color❱$reset"
317363
'';
318364

0 commit comments

Comments
 (0)