nix-config/pkgs/scripts/clone-term.nix

52 lines
1 KiB
Nix
Raw Normal View History

2024-05-31 22:15:17 +02:00
{
writeShellApplication,
ps,
procps,
xdotool,
2024-06-17 17:26:32 +02:00
jq,
2024-05-31 22:15:17 +02:00
}:
writeShellApplication {
name = "clone-term";
2024-07-26 22:12:48 +02:00
runtimeInputs = [
ps
procps
xdotool
jq
];
2024-05-31 22:15:17 +02:00
text = ''
2024-06-19 21:17:47 +02:00
if [[ ''${XDG_CURRENT_DESKTOP-} == sway ]]; then
2024-06-17 17:26:32 +02:00
PAREN=$(swaymsg -t get_tree | jq '.. | select(.type?) | select(.focused==true).pid')
elif [[ ''${XDG_CURRENT_DESKTOP-} == Hyprland ]]; then
PAREN=$(hyprctl activewindow -j | jq '.pid')
2024-06-17 17:26:32 +02:00
else
PAREN=$(xdotool getwindowfocus getwindowpid)
fi
2024-05-31 22:15:17 +02:00
MAXDEPTH=0
SELECTED=0
function recurse() {
#shellcheck disable=SC2207
for i in $(pgrep -P "$1"); do
if [[ "$(readlink -e "/proc/''${i}/exe")" == *"zsh"* ]] && [[ $2 -gt $MAXDEPTH ]]; then
SELECTED="$i"
MAXDEPTH="$2"
fi
recurse "$i" "$(( $2 + 1 ))"
done
}
recurse "$PAREN" 1
if [[ $SELECTED == 0 ]]; then
echo "not zsh found"
exit 1
fi
# kitty should be from user env
kitty --detach -d "$(readlink "/proc/''${SELECTED}/cwd")"
'';
}