2

From this question, I can tell, from a running program, if I am in a screen (or tmux screen) by looking at the $TERM variable.

But how can I tell if the screen is actually visible? In tmux, I can have multiple screens, and at the bottom a tab bar is displayed, with a "tab" for each screen. The useful part of this is that if there is activity in another tab, the appearance of that tab changes if there is output. For the purpose of this question, I don't care if the screen is actually visible to the user (another window might be overlapping it), merely if it is the active and visible screen within tmux.

It would be useful for a program running in one of these tabs to determine whether or not its screen is currently the "active" (visible) one, and to use this to moderate its output. In this way, it can be more verbose when visible, and only output more important stuff when it is not, so as to avoid needless switching between tabs due to the activity highlighting showing up on the tab. The active screen, obviously, could be switched by the user at any time.

How can an application running in a tmux screen determine when it becomes visible and not visible?

Michael
  • 9,060
  • 14
  • 61
  • 123

1 Answers1

2

You can use tmux display-message with $TMUX_PANE to show whether the current pane is active/inactive:

$ TMUX_STATUS=$( tmux display-message -p -t $TMUX_PANE -F '#F' ) $ [[ "$TMUX_STATUS" == '*' ]] && echo "window is active" window is active

Firstly, $TMUX_PANE is an environment variable exported by tmux which contains tmux's internal pane ID for the given shell.

The display-message -p command prints its output to STDOUT.

The -t $TMUX_PANE argument uses the $TMUX_PANE variable to query a specific pane (the current one, in this instance).

Finally, -f '#F' tells tmux to print only the "Current window flag" (Refer to the tmux man page, which has a list of character sequences under status-left).

AFAIK, these values are:

*, for the currently active window

-, for the previously selected window

And an empty string for windows which are neither active, nor "previous".

You'll need to poll tmux periodically from inside your program to determine whether focus has changed.

atomicstack
  • 729
  • 4
  • 8
  • excellent, thank you! i can call this any time i have trivial output and if the window isn't active i won't print it. – Michael Mar 15 '17 at 15:33