Load plugin variables

This commit enables a tmux config to load plugin variables to be used in
tmux-nova segments.
This works by loading a file before plugin declarations that sets up a
status-left option with the variables we want to load, then after the
plugins are sourced we load those values into tmux environment
variables to be used in segments.
This commit is contained in:
Simão Neves 2023-03-05 23:09:58 +00:00
parent 7103fd78fe
commit 7b1f898145
4 changed files with 70 additions and 1 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 398 KiB

View file

@ -1,4 +1,4 @@
## Documentation
## Documentation
### Pane
@ -120,3 +120,38 @@ set -g @nova-segment-bright-colors "#282a36 #f8f8f2"
set -g @nova-segments-1-left "bleft"
set -g @nova-segments-1-right "bright"
```
### Use plugin variables
Define variables you want to load to be used in segments.
<p align="center">
<a><img src="assets/tmux-nova-plugin-variables.png" alt="screenshot"></a>
</p>
```bash
# Load plugin variables to be used in tmux-nova, separated by space
# before declaration of plugins
run-shell "~/.tmux/plugins/tmux-nova/scripts/load_plugin_variables.sh pomodoro_status music_percentage music_status artist track"
# List of plugins
set -g @plugin 'tmux-plugins/tpm'
set -g @plugin 'tmux-plugins/tmux-battery'
set -g @plugin 'robhurring/tmux-spotify'
set -g @plugin 'olimorris/tmux-pomodoro-plus'
set -g @plugin 'o0th/tmux-nova' # Must be the last on the list
set -g @nova-segment-pomodoro "#(#{pomodoro_status})"
set -g @nova-segment-pomodoro-colors "colour1 colour8"
set -g @nova-segment-battery "#(#{battery_percentage})"
set -g @nova-segment-battery-colors "colour11 colour8"
set -g @nova-segment-spotify "#(#{music_status}) #(#{artist}): #(#{track})"
set -g @nova-segment-spotify-colors "colour4 colour8"
set -g @nova-segment-time "%d/%m %H:%M"
set -g @nova-segment-time-colors "colour5 colour8"
set -g @nova-segments-0-right "pomodoro spotify battery time"
```

View file

@ -0,0 +1,14 @@
#!/usr/bin/env bash
export LC_ALL=en_US.UTF-8
tmux set-option -g status-left ""
tmux set-environment -g @nova-load-variables ""
all=""
for var in "$@"
do
tmux set-option -ga status-left "#{$var}*"
all+="$var "
done
tmux set-environment -g @nova-load-variables "$all"

View file

@ -74,6 +74,26 @@ pane_active_border_style=$(get_option "@nova-pane-active-border-style" "#44475a"
tmux set-option -g pane-border-style "fg=${pane_border_style}"
tmux set-option -g pane-active-border-style "fg=${pane_active_border_style}"
#
# load variables
#
var_names=$(tmux show-environment -g @nova-load-variables | sed 's/@nova-load-variables=//')
IFS=' ' read -r -a var_names<<< $var_names
length=${#var_names[@]}
vars=$(get_option "status-left" "")
IFS='*' read -r -a vars<<< $vars
for ((j = 0; j < ${length}; j++)); do
interpolated_var="${vars[$j]}"
var_name="${var_names[$j]}"
stripped_var=$(echo $interpolated_var | sed 's/#(//' | sed 's/)//')
tmux set-environment -g "$var_name" "$stripped_var"
done
#
# segments-0-left
#