From 750c9463c14b91414232ee2a2eac9a904b8b869e Mon Sep 17 00:00:00 2001 From: Rokosun <79040025+futureisfoss@users.noreply.github.com> Date: Thu, 19 Jan 2023 17:33:46 +0000 Subject: [PATCH] Add the exclude variable, clean code --- .local/bin/sd | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/.local/bin/sd b/.local/bin/sd index ae14232c..5e3941a2 100755 --- a/.local/bin/sd +++ b/.local/bin/sd @@ -3,7 +3,16 @@ # Open a terminal window in the same directory as the currently active window. PID=$(xprop -id "$(xprop -root | sed -n "/_NET_ACTIVE_WINDOW/ s/^.*# // p")" | sed -n "/PID/ s/^.*= // p") -PID=$(pstree -lpATna "$PID" | grep -v '\-\(lf,[0-9]\+ -server\|(st-urlhandler,[0-9]\+)\|xclip,[0-9]\+\)' | sed -n '$s/.*,\([0-9]\+\).*/\1/p') + +# Some programs don't expose their current working directory at /proc/"$PID"/cwd, so we have to exclude them. +exclude='lf -server +st-urlhandler +less +git +xclip' + +regexFormat=$(echo "$exclude" | sed 's|^\(\S\+\)\(.*\)$|\1,[0-9]\\+\2|; $! s/$/\\|/' | tr -d '\n') +PID=$(pstree -lpATna "$PID" | grep -v "\-\($regexFormat\)" | sed -n '$s/.*,\([0-9]\+\).*/\1/p') cwd=$(readlink /proc/"$PID"/cwd) [ "$PWD" != "$cwd" ] && [ -d "$cwd" ] && { cd "$cwd" || exit 1; } "$TERMINAL"