gpakosz / .tmux

🇫🇷 Oh my tmux! My self-contained, pretty & versatile tmux configuration made with ❤️
MIT License
21.73k stars 3.34k forks source link

Custom variable breaks on different systems #674

Closed mattboy9921 closed 9 months ago

mattboy9921 commented 11 months ago

I have the below code in my custom variables section. Combined with any variable that outputs a percentage number, it converts it to a single character vertical bar graph. As an example: #{bargraph #{cpu_percentage}}. On my Ubuntu 18.04 systems, this works properly. On my 22.04 systems, it always ends up using the else part of the if statement. I can use #{cpu_percentage} on its own properly but curiously, even if I try to printf "$value" it is as if the value is blank. This does not occur for any other custom variable I have. I've tried everything I could think of so perhaps it's something silly.

# bargraph() {
#   local value="$(echo "$1" | cut -d "%" -f 1)"
#
#   if [ "$(echo "$value < 25" | bc -l)" -eq 1 ]; then
#     printf '▂'
#   elif [ "$(echo "$value < 37.5" | bc -l)" -eq 1 ]; then
#     printf '▃'
#   elif [ "$(echo "$value < 50" | bc -l)" -eq 1 ]; then
#     printf '▄'
#   elif [ "$(echo "$value < 62.5" | bc -l)" -eq 1 ]; then
#     printf '▅'
#   elif [ "$(echo "$value < 75" | bc -l)" -eq 1 ]; then
#     printf '▆'
#   elif [ "$(echo "$value < 87.5" | bc -l)" -eq 1 ]; then
#     printf '▇'
#   else
#     printf "█"
#   fi
# }
mattboy9921 commented 11 months ago

So I've done some more experimenting. It looks like the issue lies in doing #{bargraph #cpu_percentage}}. It seems there is no argument being passed to the bargraph() function hence why it just defaults to the else statement. Is it that there is an issue with using a plugin's variable as an input to a custom variable is not possible on the newer OS? It works fine on the older one. I am at a loss...

Edit:

I got it to work by doing #{bargraph $(/home/matt/.tmux/plugins/tmux-cpu/scripts/cpu_percentage.sh). So it seems to be having an issue with what eventually expands to the #(somescript) format that tmux uses?

mattboy9921 commented 11 months ago

So after some more tinkering today, I was able to achieve something that works. Here is the code to be put in the custom variables section of .tmux.conf.local:

# vitals() {
#   cpu_percentage=$(/home/matt/.tmux/plugins/tmux-cpu/scripts/cpu_percentage.sh)
#   ram_percentage=$(/home/matt/.tmux/plugins/tmux_cpu/scripts/ram_percentage.sh)
#   disk_percentage=$(df -h / | awk '{print $5}' | tail -n 1)
#   bar=$(bargraph "$cpu_percentage")$(bargraph "$ram_percentage")$(bargraph "$disk_percentage")
#   echo "$bar"
# }
#
# bargraph() {
#   value="$(echo "$1" | cut -d "%" -f 1)"
#
#   if [ "$(echo "$value < 12.5" | bc -l)" -eq 1 ]; then
#     echo "▁"
#   elif [ "$(echo "$value < 25" | bc -l)" -eq 1 ]; then
#     echo "▂"
#   elif [ "$(echo "$value < 37.5" | bc -l)" -eq 1 ]; then
#     echo "▃"
#   elif [ "$(echo "$value < 50" | bc -l)" -eq 1 ]; then
#     echo "▄"
#   elif [ "$(echo "$value < 62.5" | bc -l)" -eq 1 ]; then
#     echo "▅"
#   elif [ "$(echo "$value < 75" | bc -l)" -eq 1 ]; then
#     echo "▆"
#   elif [ "$(echo "$value < 87.5" | bc -l)" -eq 1 ]; then
#     echo "▇"
#   else
#     echo "█"
#   fi
# }

Basically, since using the output of another tmux plugin variable as an argument to a custom variable isn't working on my 22.04 systems, I've instead just stored the outputs of each of the scripts from tmux-cpu and tmux-df and passed them through my bargraph function. All of this now shows by adding #{vitals} to the status bar. I suppose that while I am unconventionally using a plugin's scripts without actually calling them via the status bar variable, it does clean up what would otherwise be a long string of variables for the status bar.

gpakosz commented 10 months ago

Hello @mattboy9921 👋

Sorry for the late reply. Custom variables needs to be written in POSIX shell, as tmux runs commands using /bin/sh.

You way want to first write your custom variable script as a standalone script in its own file, and use the shellcheck tool to verify it's POSIX compliant.