Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Tmux support #220

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 27 additions & 5 deletions ntfy/terminal.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,16 @@
from subprocess import PIPE, Popen, check_output, CalledProcessError
from sys import platform, stdout

def tmux_is_focused():
cmd = shlex.split('tmux list-panes -F "#{pane_id}:#{pane_active}:#{window_active}:#{session_attached}"')
pane = environ.get('TMUX_PANE')
p = Popen(cmd, stdout=PIPE, stderr=PIPE)
p.wait()
if p.poll() != 0:
return False
panes = p.stdout.read().decode()
return True if panes.find(pane + ':1:1:1') != -1 else False


def linux_window_is_focused():
xprop_cmd = shlex.split('xprop -root _NET_ACTIVE_WINDOW')
Expand All @@ -20,6 +30,13 @@ def linux_window_is_focused():
env_window_id = int(environ.get('WINDOWID', '0'))
return env_window_id == xprop_window_id

def get_tty():
if environ.get('TMUX'):
# tmux list-clients -F '#{client_tty}'
tprop_cmd = shlex.split("tmux list-clients -F '#{client_tty}'")
return check_output(tprop_cmd).decode().strip()
else:
return ttyname(stdout.fileno())

def osascript_tell(app, script):
p = Popen(['osascript'], stdin=PIPE, stdout=PIPE)
Expand All @@ -34,7 +51,7 @@ def darwin_iterm2_shell_is_focused():
'iTerm',
'tty of current session of current window',
)
return focused_tty == ttyname(stdout.fileno())
return focused_tty == get_tty()


def darwin_terminal_shell_is_focused():
Expand All @@ -43,7 +60,7 @@ def darwin_terminal_shell_is_focused():
'tty of (first tab of (first window whose frontmost is true) '
'whose selected is true)',
)
return focused_tty == ttyname(stdout.fileno())
return focused_tty == get_tty()


def darwin_app_shell_is_focused():
Expand All @@ -63,9 +80,14 @@ def darwin_app_shell_is_focused():


def is_focused():
retval = None

if platform.startswith('linux') and environ.get('DISPLAY'):
return linux_window_is_focused()
retval = linux_window_is_focused()
elif platform == 'darwin':
return darwin_app_shell_is_focused()
retval = darwin_app_shell_is_focused()

if environ.get('TMUX'):
return tmux_is_focused() & (retval if retval is not None else True)
else:
return False
return retval