From a0a52e3cd4d35bffd43887cd62ed845a31bc3f1e Mon Sep 17 00:00:00 2001
From: Yigit Sever
Date: Sun, 15 May 2022 00:01:08 +0300
Subject: fish: bring in async prompt

---
 .config/fish/conf.d/__async_prompt.fish | 130 ++++++++++++++++++++++++++++++++
 1 file changed, 130 insertions(+)
 create mode 100644 .config/fish/conf.d/__async_prompt.fish

diff --git a/.config/fish/conf.d/__async_prompt.fish b/.config/fish/conf.d/__async_prompt.fish
new file mode 100644
index 0000000..8a51156
--- /dev/null
+++ b/.config/fish/conf.d/__async_prompt.fish
@@ -0,0 +1,130 @@
+# https://github.com/acomagu/fish-async-prompt/blob/master/conf.d/__async_prompt.fish
+status is-interactive
+or exit 0
+
+set -g __async_prompt_tmpdir (command mktemp -d)
+
+# Setup after the user defined prompt functions are loaded.
+function __async_prompt_setup_on_startup --on-event fish_prompt
+    functions -e (status current-function)
+
+    for func in (__async_prompt_config_functions)
+        function $func -V func
+            test -e $__async_prompt_tmpdir'/'$fish_pid'_'$func
+            and cat $__async_prompt_tmpdir'/'$fish_pid'_'$func
+        end
+    end
+end
+
+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)
+    set st $status
+
+    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
+        end
+
+        __async_prompt_config_inherit_variables | __async_prompt_spawn $st \
+            $func' | read -z prompt
+            echo -n $prompt >'$tmpfile
+    end
+end
+
+function __async_prompt_spawn
+    set -l envs
+    begin
+        set st $argv[1]
+        while read line
+            switch "$line"
+                case 'fish_bind_mode'
+                  echo fish_bind_mode $fish_bind_mode
+                case FISH_VERSION PWD _ history 'fish_*' hostname version
+                case status
+                    echo status $st
+                case SHLVL
+                    set envs $envs SHLVL=(math $SHLVL - 1)
+                case '*'
+                    echo $line (string escape -- $$line)
+            end
+        end
+    end | read -lz vars
+    echo $vars | env $envs fish -c '
+    function __async_prompt_set_status
+        return $argv
+    end
+    function __async_prompt_signal
+        kill -s "'(__async_prompt_config_internal_signal)'" '$fish_pid'
+    end
+    while read -a line
+        test -z "$line"
+        and continue
+
+        if test "$line[1]" = status
+            set st $line[2]
+        else
+            eval set "$line"
+        end
+    end
+
+    not set -q st
+    and true
+    or __async_prompt_set_status $st
+    '$argv[2]'
+    __async_prompt_signal
+    sleep 0.3
+    __async_prompt_signal
+    sleep 0.3
+    __async_prompt_signal' &
+    disown
+end
+
+function __async_prompt_config_inherit_variables
+    if set -q async_prompt_inherit_variables
+        if test "$async_prompt_inherit_variables" = all
+            set -ng
+        else
+            for item in $async_prompt_inherit_variables
+                echo $item
+            end
+        end
+    else
+        echo status
+        echo SHLVL
+        echo CMD_DURATION
+        echo fish_bind_mode
+    end
+end
+
+function __async_prompt_config_functions
+    set -l funcs (
+        if set -q async_prompt_functions
+            string join \n $async_prompt_functions
+        else
+            echo fish_prompt
+            echo fish_right_prompt
+        end
+    )
+    for func in $funcs
+        functions -q "$func"
+        or continue
+
+        echo $func
+    end
+end
+
+function __async_prompt_config_internal_signal
+    if test -z "$async_prompt_signal_number"
+        echo SIGUSR1
+    else
+        echo "$async_prompt_signal_number"
+    end
+end
+
+function __async_prompt_repaint_prompt --on-signal (__async_prompt_config_internal_signal)
+    commandline -f repaint >/dev/null 2>/dev/null
+end
-- 
cgit v1.2.3-70-g09d2