From 3391e6e0f972fdbfa8905304770c4a953bdcbafa Mon Sep 17 00:00:00 2001 From: Adam Karim Date: Fri, 30 Oct 2020 18:13:35 -0700 Subject: [PATCH 1/2] Allow for tmux sessions to notify as well. if TERM_PROGRAM is set, then ntfy will use both tmux status and the term status to notify background tasks. --- ntfy/terminal.py | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/ntfy/terminal.py b/ntfy/terminal.py index 17e323c..9ed8d98 100644 --- a/ntfy/terminal.py +++ b/ntfy/terminal.py @@ -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 "#D:#{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 else False + def linux_window_is_focused(): xprop_cmd = shlex.split('xprop -root _NET_ACTIVE_WINDOW') @@ -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) @@ -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(): @@ -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(): @@ -64,8 +81,13 @@ def darwin_app_shell_is_focused(): def is_focused(): 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() + + retval = False if not retval else retval + + if environ.get('TMUX'): + return retval & tmux_is_focused() else: - return False + return retval From 64e8b2f692c473596b03b7885b971ee7ac1d6c43 Mon Sep 17 00:00:00 2001 From: Adam Karim Date: Sat, 31 Oct 2020 22:51:32 -0700 Subject: [PATCH 2/2] * Verify that pane is active * Better checking for if running in tmux and on linux/darwin --- ntfy/terminal.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/ntfy/terminal.py b/ntfy/terminal.py index 9ed8d98..52dcddd 100644 --- a/ntfy/terminal.py +++ b/ntfy/terminal.py @@ -4,14 +4,14 @@ from sys import platform, stdout def tmux_is_focused(): - cmd = shlex.split('tmux list-panes -F "#D:#{window_active}:#{session_attached}"') + 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 else False + return True if panes.find(pane + ':1:1:1') != -1 else False def linux_window_is_focused(): @@ -80,14 +80,14 @@ def darwin_app_shell_is_focused(): def is_focused(): + retval = None + if platform.startswith('linux') and environ.get('DISPLAY'): retval = linux_window_is_focused() elif platform == 'darwin': retval = darwin_app_shell_is_focused() - retval = False if not retval else retval - if environ.get('TMUX'): - return retval & tmux_is_focused() + return tmux_is_focused() & (retval if retval is not None else True) else: return retval