ofirgall / tmux-window-name

A plugin to name your tmux windows smartly.
MIT License
213 stars 22 forks source link

PoC: Avoid overriding users' window names #9

Closed Frederick888 closed 2 years ago

Frederick888 commented 2 years ago

This is a PoC of leveraging Tmux automatic rename feature to avoid overriding users' window names. It's nowhere near ready to be merged but hopefully can serve as an inspiration for an actual solution.

According to Tmux manual:

In addition, the last line of a shell command's output may be inserted using ‘#()’. For example, ‘#(uptime)’ will insert the system's uptime.

So instead of controlling Tmux directly from the Python script, we can simply print out the names instead and use them in automatic rename templates. I added a --window_id=<window_id> argument to the Python script and when given, it now only prints out the window name.

Then for instance, I can add this to my .tmux.conf (instead of using tmux_window_name.tmux):

bind '"' if-shell '[[ -n "$(tmux show -w automatic-rename-format)" ]]' { set -w -u automatic-rename-format; set -w automatic-rename on } { set -w automatic-rename-format "#(/path/to/tmux-window-name/scripts/rename_session_windows.py --window_id=#{window_id})"; set -w automatic-rename on }

Now I can toggle tmux-window-name using <prefix>". If I manually rename the window, automatic-rename will be set to off and hence no longer updated by our script.

By the way automatic-rename actually has 3 states: on, off and empty, which can be handy if we want to program more logic into the binding.

If we want tmux-window-name to be on by default, I believe in .tmux.conf we can simply:

set -wg automatic-rename on
set -wg automatic-rename-format "#(/path/to/tmux-window-name/scripts/rename_session_windows.py --window_id=#{window_id})"

Note though this comes with some strings attached to work smoothly:

When constructing formats, tmux does not wait for ‘#()’ commands to finish; instead, the previous result from running the same command is used, or a placeholder if the command has not been run before. If the command hasn't exited, the most recent line of output will be used, but the status line will not be updated more than once a second. Commands are executed with the tmux global environment set (see the GLOBAL AND SESSION ENVIRONMENT section).

Our script has to run fast enough to avoid '/path/to/tmux-window-name is not ready...' warnings and make sure the window names are up-to-date.

ofirgall commented 2 years ago

Hey, thanks for the contribute!

I'm aware of this issue I thought about several ways to solve it but none of them satisfy me, I opened an Issue #10 to solve this in the feature.

tmux's feature of automatic renames is buggy and clanky, actually because its so buggy I made this plugin to avoid the issues with it, unfortunately I have to decline this PR.