Cychih
Cychih

Reputation: 402

Tmux conditional color setting

Is it possible to set window color depends on the other window-option?

When a windows has synchronize-panes enabled, I don't want to accidentally press C-d, or all panes will be closed.

So what I'm trying to do is to change window color on statusline based on synchronize-panes: (the following config doesn't work, though)

bind-key S setw synchronize-panes \; \  # toggles the option
           set -w window-status-bg '#{?pane_synchronized,yellow,default}' \; \  # error: bad color
           set -w window-status-current-fg '#{?pane_synchronized,yellow,default}'  # error: bad color

The most possible solution I can thought of is to use if-shell, but I prefer not to fork a shell just to read option of itself, if possible.


EDIT: This if-shell solution works for me on tmux 2.7

My statusline cyan colored, if synchronize-panes is enabled, cyan becomes yellow.

bind-key S setw synchronize-panes \; \
           if-shell '[ #{pane_synchronized} -eq 1 ]' \
               'set -w window-status-style fg=black,bg=yellow ; set -w window-status-current-style fg=yellow,bg=black' \
               'set -w window-status-style fg=black,bg=cyan ; set -w window-status-current-style fg=cyan,bg=black'

EDIT: Problem solved, my setting is now changed to this:

bind-key S setw synchronize-panes

sync_ind_colour="#{?pane_synchronized,yellow,cyan}"
set -g window-status-format "#[fg=black,bg=${sync_ind_colour}][#I#{?#{!=:#W,},:,}#W]"
set -g window-status-current-format "#[fg=${sync_ind_colour},bg=black][#I#{?#{!=:#W,},:,}#W]"

Looks a little bit scary but it's still readable.

Upvotes: 7

Views: 3660

Answers (1)

Achilleas
Achilleas

Reputation: 236

It shouldn't be necessary to use if-shell for this. You can use conditionals in format options, but not in styles. The following minimal configuration should do what you want.

# toggle pane synchronisation mode
bind-key S setw synchronize-panes

# Variables
sync_ind_colour="#{?pane_synchronized,yellow,cyan}"

# status format
setw -g window-status-format "#[fg=black,bg=${sync_ind_colour}]#I #W"
setw -g window-status-current-format "#[fg=${sync_ind_colour},bg=black][#I #W]"

Note that I set the text of the window status to #I #W (and [#I #W] for active) as an example, but that's irrelevant to the question.

It's also not necessary to use a variable (sync_ind_colour, synchronise indicator colour), but it's simpler than defining the same conditional in both the window-status-format and the window-status-current-format variables.

Upvotes: 11

Related Questions