1
0
Fork 0
dotfiles/.local/bin/samedir

24 lines
1020 B
Plaintext
Raw Normal View History

2022-07-04 21:36:33 +02:00
#!/bin/sh
# Open a terminal window in the same directory as the currently active window.
2023-02-22 15:33:22 +01:00
#
PIDlist=$(pstree -lpATna "$(xdotool getactivewindow getwindowpid)" | sed -En 's/.*,([0-9]+).*/\1/p' | tac)
2023-02-04 22:15:35 +01:00
for PID in $PIDlist; do
2023-02-22 15:33:22 +01:00
[ -d "/proc/$PID" ] || continue
2023-02-04 22:15:35 +01:00
cmdline=$(ps -o args= -p "$PID")
process_group_leader=$(ps -o comm= -p "$(ps -o pgid= -p "$PID" | tr -d ' ')")
2023-02-22 15:33:22 +01:00
cwd=$(readlink "/proc/$PID/cwd")
2023-02-04 22:15:35 +01:00
# zsh and lf won't be ignored even if it shows ~ or /
case "$cmdline" in
2023-02-22 15:33:22 +01:00
'lf -server') continue ;;
"${SHELL##*/}"|'lf'|'lf '*) break ;;
2023-02-04 22:15:35 +01:00
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
2023-02-22 15:33:22 +01:00
2023-02-04 22:15:35 +01:00
[ "$PWD" != "$cwd" ] && [ -d "$cwd" ] && { cd "$cwd" || exit 1; }
2023-02-22 15:33:22 +01:00
[ "$(ps -p $$ -ocomm=)" = "zsh" ] && eval '$TERMINAL &!' || "$TERMINAL" &