-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathterminal-frontmost.fish
38 lines (32 loc) · 1.21 KB
/
terminal-frontmost.fish
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
# If on X, but no WINDOWID, investigate `xdotool getwindowfocus`.
# http://manpages.ubuntu.com/manpages/trusty/man1/xdotool.1.html
# https://pleasantprogrammer.com/posts/is-my-terminal-window-active.html
if test -n "$WINDOWID"
# X Windows (supports Ubuntu Terminal excluding tabs)
function terminal-frontmost -d "Returns 0 if terminal is frontmost, otherwise 1"
set -l tmp (printf "%d" (string split -m 1 -r " " (xprop -root _NET_ACTIVE_WINDOW))[2])
test "$WINDOWID" = "$tmp"
end
else if test -n "$TERM_SESSION_ID"
# macOS (supports Terminal.app and tabs)
function terminal-frontmost -d "Returns 0 if terminal is frontmost, otherwise 1"
set -l tmp (osascript \
-e 'tell application "Terminal"' \
-e ' if frontmost is true' \
-e ' repeat with w in windows' \
-e ' if (frontmost of w) is true then' \
-e ' set t to (selected tab of w)' \
-e ' return (tty of t) as string' \
-e ' end if' \
-e ' end repeat' \
-e ' end if' \
-e 'end tell' \
)
test (tty) = "$tmp"
end
else
# Unknown/other
function terminal-frontmost -d "Returns 0 if terminal is frontmost, otherwise 1"
return 0 # Always frontmost
end
end