1

I want to pad a format string to a certain length. For example, the Tmux Battery plugin introduces the battery_percentage format string. I can use this in the status bar as #{battery_percentage}. The battery percentage values can be:

  1. Between 0% and 9% (One Digit).
  2. Between 11% and 99% (Two Digits).
  3. Exactly 100% (Three Digits).

I want the format string to always be displayed 3 digits, padded with spaces at the end, how can I achieve that?

I saw that there is the format #{pN:variable} in this page, but it did not work when I tried to use it with format strings, even though at the end they are variables. Maybe I just did not know how to use it, I don't know...

Ameer Taweel
  • 949
  • 1
  • 11
  • 37
  • @meuh Thanks man, you gave me the key for the solution, I passed the command to awk and padded it there. And put the single command in tmux configuration. Please post this as an answer so I can accept it... – Ameer Taweel Jan 14 '21 at 18:47

1 Answers1

1

Looking at the plugin startup code, battery.tmux, you can see that ${battery_percentage} is actually converted to #($CURRENT_DIR/scripts/battery_percentage.sh), which is a request to run a script. I don't know if #{p3:#(...)} can be made to work since this is not a simple variable.

You could always edit the plugin shell script to return the padded string (assuming tmux keeps the leading spaces).

meuh
  • 11,500
  • 2
  • 29
  • 45