-
Notifications
You must be signed in to change notification settings - Fork 3
/
clone-term.nix
47 lines (40 loc) · 1.12 KB
/
clone-term.nix
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# From: https://forge.lel.lol/patrick/nix-config/src/commit/77e9d0eaf356537131d33389cca15ffdd7bbefc2/pkgs/scripts/clone-term.nix
{
writeShellApplication,
ps,
procps,
xdotool,
jq,
}:
writeShellApplication {
name = "clone-term";
runtimeInputs = [ps procps xdotool jq];
text = ''
if [[ ''${XDG_CURRENT_DESKTOP-} == sway ]]; then
PAREN=$(swaymsg -t get_tree | jq '.. | select(.type?) | select(.focused==true).pid')
elif [[ ''${XDG_CURRENT_DESKTOP-} == Hyprland ]]; then
PAREN=$(hyprctl activewindow -j | jq '.pid')
else
PAREN=$(xdotool getwindowfocus getwindowpid)
fi
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")"
'';
}