# kaeru (https://github.com/gloomydumber/kaeru) keeps track of this tab, so
# that it can come back after the terminal closes. Your config.fish loads this
# with: kaeru init fish | source
# Everything runs in a function, so that its variables stay local and a
# problem never stops the rest of config.fish.
function __kaeru_init
status is-interactive; or return
# Loaded once, also when config.fish runs again in the same shell.
set -q __kaeru_cwd_file; and return
set -g __kaeru_cwd_file
set -g __kaeru_ask
set -g __kaeru_last_cwd
# kaeru resume prints the code that resumes the session, and this runs it
# here, as if you typed it, so that no kaeru process waits while the
# session runs. A kaeru that waits keeps its file in use, which an update
# can't replace on Windows. Other commands run kaeru as they are.
function kaeru
if test (count $argv) -ne 1; or test "$argv[1]" != resume
command kaeru $argv
return
end
set -l code (command kaeru resume --shell fish); or return
if test -n "$code"
eval (string join \n -- $code)
end
end
set -l output (command kaeru shell start --pid $fish_pid)
# Exit code 3: tabs are waiting, and this tab is the one that asks.
test $status -eq 3; and set -g __kaeru_ask 1
# A tab that kaeru reopened gets the code that resumes its session from
# KAERU_RUN, which kaeru shell start has seen, and runs it at the first
# prompt, once config.fish has set everything up. Programs started in
# the tab don't inherit it.
set -g __kaeru_run "$KAERU_RUN"
set -e KAERU_RUN
set -g __kaeru_cwd_file "$output"
test -n "$__kaeru_cwd_file"; or return
# Programs in this tab, such as an agent and its hook, learn the tab's
# shell from this, for when the OS can't tell.
set -gx KAERU_SHELL_PID $fish_pid
# Writes the folder to the tab's .cwd file when it changes, and touches
# the file at every prompt, without starting a process. The file's time
# says when the prompt last showed, so that kaeru knows an agent that ran
# in this tab ended. fish keeps $status for the prompt around this.
function __kaeru_prompt --on-event fish_prompt
if test -n "$__kaeru_ask"
# The picker waits for the user, so it shows at the first prompt
# and not while config.fish runs.
set -g __kaeru_ask
KAERU_NEW_TAB=1 command kaeru
# Exit code 4: every tab came back in this window, so this new
# tab, which only showed the picker, is one too many.
test $status -eq 4; and exit 0
end
if test -n "$__kaeru_run"
set -l code $__kaeru_run
set -g __kaeru_run
eval $code
end
# Once the file is gone, as when kaeru forgot the tab, it stays gone
# until the folder changes.
if test "$PWD" != "$__kaeru_last_cwd"; or test -e "$__kaeru_cwd_file"
builtin pwd 2>/dev/null >"$__kaeru_cwd_file"
and set -g __kaeru_last_cwd $PWD
end
end
end
__kaeru_init
functions -e __kaeru_init