Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use universal variables instead of temp files #76

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 22 additions & 14 deletions conf.d/__async_prompt.fish
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
status is-interactive
or exit 0

set -g __async_prompt_tmpdir (command mktemp -d)
set -g __async_prompt_var _async_prompt_$fish_pid

# Setup after the user defined prompt functions are loaded.
function __async_prompt_setup_on_startup --on-event fish_prompt
Expand All @@ -11,9 +11,12 @@ function __async_prompt_setup_on_startup --on-event fish_prompt
end

for func in (__async_prompt_config_functions)
set -U $__async_prompt_var'_'$func
function $func -V func
test -e $__async_prompt_tmpdir'/'$fish_pid'_'$func
and cat $__async_prompt_tmpdir'/'$fish_pid'_'$func
if set -q $__async_prompt_var'_'$func
set -l result $__async_prompt_var'_'$func
echo -n $$result
Copy link

@petertriho petertriho Jul 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm using starship and this causes the multiline prompts to merge into a single line.
I changed this to printf "%s\n" $$result and seems to work for both single and multiline prompts

EDIT: only needed printf "%s" $$result nvm need \n

end
end
end
end
Expand All @@ -26,16 +29,12 @@ not set -q async_prompt_on_variable
and set async_prompt_on_variable fish_bind_mode
function __async_prompt_fire --on-event fish_prompt (for var in $async_prompt_on_variable; printf '%s\n' --on-variable $var; end)
for func in (__async_prompt_config_functions)
set -l tmpfile $__async_prompt_tmpdir'/'$fish_pid'_'$func

if functions -q $func'_loading_indicator' && test -e $tmpfile
read -zl last_prompt <$tmpfile
eval (string escape -- $func'_loading_indicator' "$last_prompt") >$tmpfile
if functions -q $func'_loading_indicator' && set -q $__async_prompt_var'_'$func
set $__async_prompt_var'_'$func ($func'_loading_indicator' $__async_prompt_var'_'$func)
end

__async_prompt_config_inherit_variables | __async_prompt_spawn \
$func' | read -z prompt
echo -n $prompt >'$tmpfile
$func
end
end

Expand Down Expand Up @@ -134,10 +133,10 @@ function __async_prompt_spawn -a cmd
else
true
end
'$cmd'
set '$__async_prompt_var\'_\'$cmd' ('$cmd')
__async_prompt_signal' &
if test (__async_prompt_config_disown) = 1
disown
builtin disown
end
end

Expand Down Expand Up @@ -196,6 +195,15 @@ function __async_prompt_repaint_prompt --on-signal (__async_prompt_config_intern
commandline -f repaint >/dev/null 2>/dev/null
end

function __async_prompt_tmpdir_cleanup --on-event fish_exit
rm -rf "$__async_prompt_tmpdir"
function __async_prompt_variable_cleanup --on-event fish_exit
set -l prefix (string replace $fish_pid '' $__async_prompt_var)
set -l prompt_vars (set --show | string match -rg '^\$('"$prefix"'\d+_[a-z_]+):' | uniq)
for var in $prompt_vars
set -l pid (string match -rg '^'"$prefix"'(\d+)_[a-z_]+' $var)
if not ps $pid &> /dev/null
or test $pid -eq $fish_pid
set -Ue $var
end
end
set -ge __async_prompt_var
end