This commit is contained in:
Thomas Forgione 2024-06-23 16:31:17 +02:00
parent 9241ff0c0d
commit feebba4e12
3 changed files with 61 additions and 30 deletions

View File

@ -1,14 +1,11 @@
#!/usr/bin/env bash
# Extract parent window uuid
WINDOW_ID=$(xdotool getactivewindow)
WM_NAME=$(xprop -id $WINDOW_ID WM_NAME | cut -d '"' -f 2)
# Generate new uuid for new terminal
uuid=$(uuidgen)
if [ -f $HOME/.config/terminalscwd/$WM_NAME.ssh ]; then
cp $HOME/.config/terminalscwd/$WM_NAME.ssh $HOME/.config/terminalscwd/$uuid.ssh
alacritty --title $uuid -e sh -c "TERMINAL_UUID=$uuid PARENT_TERMINAL=$WM_NAME ssh $(cat $HOME/.config/terminalscwd/$WM_NAME.ssh)"
else
cp $HOME/.config/terminalscwd/$WM_NAME.pwd $HOME/.config/terminalscwd/$uuid.pwd
alacritty --title $uuid -e sh -c "TERMINAL_UUID=$uuid PARENT_TERMINAL=$WM_NAME exec $SHELL"
fi

4
bin/new-terminal Executable file
View File

@ -0,0 +1,4 @@
#!/usr/bin/env bash
UUID=$(uuidgen)
alacritty --title $UUID -e sh -c "TERMINAL_UUID=$UUID exec $SHELL"

View File

@ -1,36 +1,66 @@
if [ "$PRESERVE_SSH_CWD" = "true" ]; then
if [ -z $DISPLAY ]; then
if [ -z $TERMINAL_UUID ]; then
export TERMINAL_UUID=$(uuidgen)
fi
else
WINDOW_ID=$(xdotool getactivewindow)
if [ $? -eq 0 ]; then
WM_NAME=$(xprop -id $WINDOW_ID WM_NAME | cut -d '"' -f 2)
export TERMINAL_UUID=$WM_NAME
else
export TERMINAL_UUID=$(uuidgen)
fi
fi
echo -en "\e]2;$TERMINAL_UUID\a"
# Ensure the directory where we store terminal cwds and ssh exists
mkdir -p $HOME/.config/terminalscwd/
# Alias for cd: when we change directory, we write the new cwd in the file corresponding to the terminal uuid
cd() {
builtin cd $1
pwd > $HOME/.config/terminalscwd/$TERMINAL_UUID.pwd
pwd > $HOME/.config/terminalscwd/$TERMINAL_UUID.cwd
}
if [ -f $HOME/.config/terminalscwd/$PARENT_TERMINAL.pwd ]; then
cd $(cat $HOME/.config/terminalscwd/$PARENT_TERMINAL.pwd)
else
cd
fi
# Alias for ssh: when we ssh somewhere, we write it so that we know we should ssh when cloning terminal
ssh() {
echo $1 > $HOME/.config/terminalscwd/$TERMINAL_UUID.ssh
/usr/bin/ssh $@
rm $HOME/.config/terminalscwd/$TERMINAL_UUID.ssh
pwd > $HOME/.config/terminalscwd/$TERMINAL_UUID.pwd
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
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
# 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
}
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
fi