diff options
author | 2025-02-13 14:13:49 +0530 | |
---|---|---|
committer | 2025-02-13 14:13:49 +0530 | |
commit | 8a2e1006b3b272126332aa064f3ad95387129544 (patch) | |
tree | 944c80ac612a65980d94a54ba11b6c7102037ecf /.local/bin/sd | |
parent | dcbb16d8b08ff5956abef5e6478b59df2e93ad35 (diff) | |
download | dotfiles-8a2e1006b3b272126332aa064f3ad95387129544.tar.gz dotfiles-8a2e1006b3b272126332aa064f3ad95387129544.tar.bz2 dotfiles-8a2e1006b3b272126332aa064f3ad95387129544.zip |
Diffstat (limited to '.local/bin/sd')
-rwxr-xr-x | .local/bin/sd | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/.local/bin/sd b/.local/bin/sd new file mode 100755 index 0000000..a0ff84c --- /dev/null +++ b/.local/bin/sd @@ -0,0 +1,22 @@ +#!/bin/sh + +# Open a terminal window in the same directory as the currently active window. + +windowPID=$(xprop -id "$(xprop -root | sed -n "/_NET_ACTIVE_WINDOW/ s/^.*# // p")" | sed -n "/PID/ s/^.*= // p") +PIDlist=$(pstree -lpATna "$windowPID" | sed -En 's/.*,([0-9]+).*/\1/p' | tac) +for PID in $PIDlist; do + cmdline=$(ps -o args= -p "$PID") + process_group_leader=$(ps -o comm= -p "$(ps -o pgid= -p "$PID" | tr -d ' ')") + cwd=$(readlink /proc/"$PID"/cwd) + # zsh and lf won't be ignored even if it shows ~ or / + case "$cmdline" in + 'lf -server') continue ;; + "${SHELL##*/}"|'lf'|'lf '*) break ;; + esac + # git (and its sub-processes) will show the root of a repository instead of the actual cwd, so they're ignored + [ "$process_group_leader" = 'git' ] || [ ! -d "$cwd" ] && continue + # This is to ignore programs that show ~ or / instead of the actual working directory + [ "$cwd" != "$HOME" ] && [ "$cwd" != '/' ] && break +done +[ "$PWD" != "$cwd" ] && [ -d "$cwd" ] && { cd "$cwd" || exit 1; } +"$TERMINAL" |