-
Notifications
You must be signed in to change notification settings - Fork 215
/
terminal.py
65 lines (50 loc) · 1.87 KB
/
terminal.py
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
from os import environ, ttyname
from subprocess import check_output, Popen, PIPE
from sys import platform, stdout
def get_tty():
window_id = int(check_output(['xprop', '-root', '\t$0',
'_NET_ACTIVE_WINDOW']).split()[1], 16)
return int(environ['WINDOWID']) == window_id
def linux_window_is_focused():
window_id = int(check_output(['xprop', '-root', '\t$0',
'_NET_ACTIVE_WINDOW']).split()[1], 16)
return int(environ['WINDOWID']) == window_id
def osascript_tell(app, script):
p = Popen(['osascript'], stdin=PIPE, stdout=PIPE)
stdout, stderr = p.communicate(
'tell application "{}"\n{}\nend tell'.format(app, script))
return stdout.rstrip('\n')
def darwin_iterm2_shell_is_focused():
focused_tty = osascript_tell(
'iTerm',
'tty of current session of current terminal',
)
return focused_tty == ttyname(stdout.fileno())
def darwin_terminal_shell_is_focused():
focused_tty = osascript_tell(
'Terminal',
'tty of (first tab of (first window whose frontmost is true) '
'whose selected is true)',
)
return focused_tty == ttyname(stdout.fileno())
def darwin_app_shell_is_focused():
current_appid = {
'iTerm.app': 'iTerm',
'Apple_Terminal': 'Terminal',
}.get(environ.get('TERM_PROGRAM'))
focused_appid = osascript_tell(
'System Events',
'name of first application process whose frontmost is true',
)
if current_appid == focused_appid:
return {
'Terminal': darwin_terminal_shell_is_focused,
'iTerm': darwin_iterm2_shell_is_focused,
}[current_appid]()
def is_focused():
if platform.startswith('linux'):
return linux_window_is_focused()
elif platform == 'darwin':
return darwin_app_shell_is_focused()
else:
return True