2024-06-21 17:28:36 +02:00
|
|
|
if [ "$PRESERVE_SSH_CWD" = "true" ]; then
|
2024-06-21 18:16:54 +02:00
|
|
|
|
2024-06-23 16:31:17 +02:00
|
|
|
# Ensure the directory where we store terminal cwds and ssh exists
|
2024-06-21 17:28:36 +02:00
|
|
|
mkdir -p $HOME/.config/terminalscwd/
|
2024-06-23 16:31:17 +02:00
|
|
|
|
|
|
|
# Alias for cd: when we change directory, we write the new cwd in the file corresponding to the terminal uuid
|
2024-06-21 17:28:36 +02:00
|
|
|
cd() {
|
|
|
|
builtin cd $1
|
2024-06-23 16:31:17 +02:00
|
|
|
pwd > $HOME/.config/terminalscwd/$TERMINAL_UUID.cwd
|
2024-06-21 17:28:36 +02:00
|
|
|
}
|
|
|
|
|
2024-06-23 16:31:17 +02:00
|
|
|
# Alias for ssh: when we ssh somewhere, we write it so that we know we should ssh when cloning terminal
|
2024-06-21 17:28:36 +02:00
|
|
|
ssh() {
|
|
|
|
echo $1 > $HOME/.config/terminalscwd/$TERMINAL_UUID.ssh
|
|
|
|
/usr/bin/ssh $@
|
|
|
|
rm $HOME/.config/terminalscwd/$TERMINAL_UUID.ssh
|
2024-06-23 16:31:17 +02:00
|
|
|
pwd > $HOME/.config/terminalscwd/$TERMINAL_UUID.cwd
|
|
|
|
}
|
|
|
|
|
|
|
|
# If terminal uuid does not exit
|
|
|
|
if [ -z $TERMINAL_UUID ]; then
|
|
|
|
|
|
|
|
# Generate one
|
|
|
|
export TERMINAL_UUID=$(uuidgen)
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Copy state for new terminal
|
2024-06-24 14:09:50 +02:00
|
|
|
if [ ! -z $PARENT_TERMINAL ]; then
|
|
|
|
if [ -f $HOME/.config/terminalscwd/$PARENT_TERMINAL.cwd ]; then
|
|
|
|
cp $HOME/.config/terminalscwd/$PARENT_TERMINAL.cwd $HOME/.config/terminalscwd/$TERMINAL_UUID.cwd
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ -f $HOME/.config/terminalscwd/$PARENT_TERMINAL.ssh ]; then
|
|
|
|
cp $HOME/.config/terminalscwd/$PARENT_TERMINAL.ssh $HOME/.config/terminalscwd/$TERMINAL_UUID.ssh
|
|
|
|
fi
|
2024-06-23 16:31:17 +02:00
|
|
|
fi
|
|
|
|
|
|
|
|
# When we're done, delete the uuid files
|
|
|
|
preserve_ssh_cleanup() {
|
|
|
|
rm -rf $HOME/.config/terminalscwd/$TERMINAL_UUID.cwd
|
|
|
|
rm -rf $HOME/.config/terminalscwd/$TERMINAL_UUID.ssh
|
2024-06-21 17:28:36 +02:00
|
|
|
}
|
2024-06-23 16:31:17 +02:00
|
|
|
|
|
|
|
trap preserve_ssh_cleanup EXIT
|
|
|
|
|
|
|
|
# If there already is a file corresponding to the current uuid
|
|
|
|
if [ -f $HOME/.config/terminalscwd/$TERMINAL_UUID.cwd ]; then
|
|
|
|
|
|
|
|
# Go the the right directory
|
|
|
|
cd $(cat $HOME/.config/terminalscwd/$TERMINAL_UUID.cwd)
|
|
|
|
|
|
|
|
else
|
|
|
|
|
|
|
|
# Use our custom alias to go to home, and set the file cwd
|
|
|
|
cd
|
|
|
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
# If we're running in an ssh session
|
|
|
|
if [ -f $HOME/.config/terminalscwd/$TERMINAL_UUID.ssh ]; then
|
|
|
|
|
|
|
|
# Run the ssh command, and exec shell in the end so the terminal doesn't exit at end of ssh
|
|
|
|
ssh $(cat $HOME/.config/terminalscwd/$TERMINAL_UUID.ssh)
|
|
|
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
2024-06-21 17:28:36 +02:00
|
|
|
fi
|