From feebba4e123a30bcad420aca981411049a7f35a8 Mon Sep 17 00:00:00 2001 From: Thomas Forgione Date: Sun, 23 Jun 2024 16:31:17 +0200 Subject: [PATCH] Working --- bin/clone-terminal | 11 +++--- bin/new-terminal | 4 +++ zsh/preserve-cwd-ssh.zsh | 76 ++++++++++++++++++++++++++++------------ 3 files changed, 61 insertions(+), 30 deletions(-) create mode 100755 bin/new-terminal diff --git a/bin/clone-terminal b/bin/clone-terminal index d978f7d..bbb2d76 100755 --- a/bin/clone-terminal +++ b/bin/clone-terminal @@ -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 +alacritty --title $uuid -e sh -c "TERMINAL_UUID=$uuid PARENT_TERMINAL=$WM_NAME exec $SHELL" + diff --git a/bin/new-terminal b/bin/new-terminal new file mode 100755 index 0000000..c7c3197 --- /dev/null +++ b/bin/new-terminal @@ -0,0 +1,4 @@ +#!/usr/bin/env bash + +UUID=$(uuidgen) +alacritty --title $UUID -e sh -c "TERMINAL_UUID=$UUID exec $SHELL" diff --git a/zsh/preserve-cwd-ssh.zsh b/zsh/preserve-cwd-ssh.zsh index 866722b..3932e14 100644 --- a/zsh/preserve-cwd-ssh.zsh +++ b/zsh/preserve-cwd-ssh.zsh @@ -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