Skip to content

Instantly share code, notes, and snippets.

@duament
Created March 28, 2022 16:01
Show Gist options
  • Save duament/bac0181935953b97ca71640727c9c029 to your computer and use it in GitHub Desktop.
Save duament/bac0181935953b97ca71640727c9c029 to your computer and use it in GitHub Desktop.
status is-interactive
or exit 0
if test -n "$XDG_RUNTIME_DIR"
set -g __starship_async_tmpdir "$XDG_RUNTIME_DIR"/fish-async-prompt
else
set -g __starship_async_tmpdir /tmp/fish-async-prompt
end
mkdir -p "$__starship_async_tmpdir"
set -g __starship_async_signal SIGUSR1
# Starship
set -g VIRTUAL_ENV_DISABLE_PROMPT 1
builtin functions -e fish_mode_prompt
set -gx STARSHIP_SHELL fish
set -gx STARSHIP_SESSION_KEY (random 10000000000000 9999999999999999)
# Transient prompt
function __starship_async_cancel_repaint --on-event fish_cancel
commandline -f repaint
end
function __starship_async_maybe_execute
commandline --is-valid
if test $status != 2
set -g TRANSIENT 1
commandline -f repaint
end
commandline -f execute
end
function __starship_async_cancel_commandline
if string length -q -- (commandline)
set -g TRANSIENT 1
commandline -f repaint
end
commandline -f cancel-commandline
end
function __starship_async_edit_command_buffer
printf '\e[2F' # 2 lines up
edit_command_buffer
end
bind \r __starship_async_maybe_execute # ENTER
bind \cc __starship_async_cancel_commandline # CTRL+C
bind \ee __starship_async_edit_command_buffer # ALT+E
bind \ev __starship_async_edit_command_buffer # ALT+V
# Prompt
function fish_prompt
printf '\e[0J' # Clear from cursor to end of screen
if test $TRANSIENT -eq 1 &> /dev/null
set -g TRANSIENT 0
__starship_async_simple_prompt
else if test -e "$__starship_async_tmpdir"/"$fish_pid"_fish_prompt
cat "$__starship_async_tmpdir"/"$fish_pid"_fish_prompt
else
__starship_async_simple_prompt
end
end
# Async task
function __starship_async_fire --on-event fish_prompt
switch "$fish_key_bindings"
case fish_hybrid_key_bindings fish_vi_key_bindings
set STARSHIP_KEYMAP "$fish_bind_mode"
case '*'
set STARSHIP_KEYMAP insert
end
set STARSHIP_CMD_PIPESTATUS $pipestatus
set STARSHIP_CMD_STATUS $status
set STARSHIP_DURATION "$CMD_DURATION"
set STARSHIP_JOBS (count (jobs -p))
set -l tmpfile "$__starship_async_tmpdir"/"$fish_pid"_fish_prompt
fish -c '
starship prompt --terminal-width="'$COLUMNS'" --status='$STARSHIP_CMD_STATUS' --pipestatus="'$STARSHIP_CMD_PIPESTATUS'" --keymap='$STARSHIP_KEYMAP' --cmd-duration='$STARSHIP_DURATION' --jobs='$STARSHIP_JOBS' > '$tmpfile'
kill -s "'$__starship_async_signal'" '$fish_pid &
disown
end
function __starship_async_simple_prompt
set_color brgreen
echo -n ''
set_color normal
echo ' '
end
function __starship_async_repaint_prompt --on-signal "$__starship_async_signal"
commandline -f repaint
end
function __starship_async_cleanup --on-event fish_exit
rm -f "$__starship_async_tmpdir"/"$fish_pid"_fish_prompt
end
# https://github.com/acomagu/fish-async-prompt
# https://github.com/fish-shell/fish-shell/issues/8223
@JanPokorny
Copy link

Very nice! I had trouble getting starship to work with fish-async-prompt, but this one works flawlessly.

@soraxas
Copy link

soraxas commented May 15, 2023

Thanks heaps!

@Arch-Storm
Copy link

how would you add a right prompt to this? @duament

@luca-rath
Copy link

Thanks a lot for this snippet, this is amazing!

On macOS, this works flawlessly.
Unfortunately it doesn't work for me on Ubuntu though.
Whenever I execute a command, then the expected prompt is written to the next line, but not transiently.
But whenever I just press Enter, the default "❯" prompt is printed, without ever being replaced by the real prompt.

Does anybody have the same issue or an idea what the reason for this behaviour could be?
Thanks in advance!

@JanPokorny
Copy link

@luca-rath You could try a different terminal emulator (Alacritty, Kitty etc.) and a different fish version (if you installed from apt, maybe you aren't on the most recent 3.6.1 -- try brew instead)

@luca-rath
Copy link

@JanPokorny Thanks for your reply. I've got fish 3.6.1 installed via nix-env, but I've been using the default ubuntu terminal. Using kitty has the same outcome, didn't try alacritty yet. Starship version is 1.16.0, which is the latest as well.

@JanPokorny
Copy link

@luca-rath In that case no clue. I actually also stopped using this since it bugged in some places (like integrated IDE terminals)

@luca-rath
Copy link

I found the cause of the problem now ...
I had the starship_async_transient_prompt.fish file inside my .config/fish/conf.d/ directory, which is correct, but I also still had the starship init fish | source line in my .config/fish/config.fish, which I wasn't supposed to have, my bad.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment