dots

my dotfiles
git clone https://git.awy.one/dots
Log | Files | Refs | Submodules | README | LICENSE

sd (1012B) - View raw


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
#!/bin/sh

# Open a terminal window in the same directory as the currently active window.

windowPID=$(swaymsg --raw -t get_tree | jq -r '.. | select(type == "object" and .focused == true) | .pid')
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 yazi won't be ignored even if it shows ~ or /
  case "$cmdline" in
  'Yazi') continue ;;
  "${SHELL##*/}" | 'yazi' | 'yazi '*) 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"