gpakosz / .tmux

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

Battery status not working when not using gradient or heat for the battery bar #35

Closed luisdavim closed 8 years ago

luisdavim commented 8 years ago

Hi,

I've just upgraded my ubuntu installation from 16.04 to 16.10 and the battery status is now missing, running:

cut -c3- ~/.tmux.conf | sh -s _battery

Returns:

sh: 343: [: Illegal number: %d9.7
sh: 345: Illegal number: %d9.7
cat /sys/class/power_supply/BAT0/capacity

returns 97.

gpakosz commented 8 years ago

Works for me.

image

gpakosz commented 8 years ago

Double check upgrading didn't break awk.

luisdavim commented 8 years ago

Hi,

I got it working with these changes:

diff --git a/.tmux.conf b/.tmux.conf
index 2e96dcf..4ae9e5a 100644
--- a/.tmux.conf
+++ b/.tmux.conf
@@ -319,7 +319,7 @@ run 'cut -c3- ~/.tmux.conf | sh -s _apply_configuration'
 #     palette=$(echo "$palette" | awk -v n="$battery_bar_length" '{ for (i = 0; i < n; ++i) printf $(1 + (i * NF / n))" " }')
 #     eval set -- "$palette"
 #
-#     full=$(awk "BEGIN { printf \"%.0f\", ($charge) * $battery_bar_length }")
+#     full=$(awk 'BEGIN { printf "%.0f", ($charge) * $battery_bar_length }')
 #     battery_bar="#[bg=$battery_bg]"
 #     # shellcheck disable=SC2046
 #     [ "$full" -gt 0 ] && \
@@ -337,7 +337,7 @@ run 'cut -c3- ~/.tmux.conf | sh -s _apply_configuration'
 #     battery_empty_fg=$2
 #     battery_bg=$3
 #
-#     full=$(awk "BEGIN { print \"%d\" ($charge) * $battery_bar_length }")
+#     full=$(awk 'BEGIN { printf "%d", ($charge) * $battery_bar_length }')
 #     [ x"$battery_bg" != x"none" ] && \
 #       battery_bar="#[bg=$battery_bg]"
 #     [ "$full" -gt 0 ] && \
@@ -348,6 +348,7 @@ run 'cut -c3- ~/.tmux.conf | sh -s _apply_configuration'
 #       battery_bar="$battery_bar#[fg=$battery_empty_fg]"
 #   fi
 #
+#     battery_vbar_bg='none'
 #   if echo "$battery_vbar_palette" | grep -q -E '^heat|gradient(,[#a-z0-9]{7,9})?$'; then
 #     # shellcheck disable=SC2086
 #     { set -f; IFS=,; set -- $battery_vbar_palette; unset IFS; set +f; }
gpakosz commented 8 years ago

I disagree with the first two changes. I don't understand how it can even work since $charge won't even be expanded inside single quotes

luisdavim commented 8 years ago

it's expanded by awk not bash

luisdavim commented 8 years ago

but the problem is not the quotes, it's the print instead of printf and the battery_vbar_bg only being initialized inside of the if.

luisdavim commented 8 years ago

This also works:

diff --git a/.tmux.conf b/.tmux.conf
index 2e96dcf..55c0732 100644
--- a/.tmux.conf
+++ b/.tmux.conf
@@ -337,7 +337,7 @@ run 'cut -c3- ~/.tmux.conf | sh -s _apply_configuration'
 #     battery_empty_fg=$2
 #     battery_bg=$3
 #
-#     full=$(awk "BEGIN { print \"%d\" ($charge) * $battery_bar_length }")
+#     full=$(awk "BEGIN { printf \"%d\", ($charge) * $battery_bar_length }")
 #     [ x"$battery_bg" != x"none" ] && \
 #       battery_bar="#[bg=$battery_bg]"
 #     [ "$full" -gt 0 ] && \
@@ -348,6 +348,7 @@ run 'cut -c3- ~/.tmux.conf | sh -s _apply_configuration'
 #       battery_bar="$battery_bar#[fg=$battery_empty_fg]"
 #   fi
 #
+#     battery_vbar_bg='none'
 #   if echo "$battery_vbar_palette" | grep -q -E '^heat|gradient(,[#a-z0-9]{7,9})?$'; then
 #     # shellcheck disable=SC2086
 #     { set -f; IFS=,; set -- $battery_vbar_palette; unset IFS; set +f; }
gpakosz commented 8 years ago

It's expanded by awk not bash

NO. The way it works is it's expanded by Bash. Then expansion is given to Awk. Just try it for yourself...

#!/bin/sh

charge="50 / 100"
battery_bar_length="10"
set -x
full=$(awk "BEGIN { printf \"%.0f\", ($charge) * $battery_bar_length }")
set +x

echo "$full"

echo ---

set -x
full=$(awk 'BEGIN { printf "%.0f", ($charge) * $battery_bar_length }')
set +x
echo "$full"

gives

$ ./test.sh
++ awk 'BEGIN { printf "%.0f", (50 / 100) * 10 }'
+ full=5
+ set +x
5
---
++ awk 'BEGIN { printf "%.0f", ($charge) * $battery_bar_length }'
+ full=0
+ set +x
0

Also, it has to be printf to truncate the decimal part

gpakosz commented 8 years ago

Which version of awk are you using?

luisdavim commented 8 years ago
$ awk -V
GNU Awk 4.1.3, API: 1.1 (GNU MPFR 3.1.5, GNU MP 6.1.1)
gpakosz commented 8 years ago

Again, it works for me with a stock 16.10 installed.

$ vagrant init ubuntu/yakkety64; vagrant up --provider virtualbox
$ vagrant ssh
$ cd
$ git clone https://github.com/gpakosz/.tmux.git
$ ln -s .tmux/.tmux.conf
$ tmux
luisdavim commented 8 years ago

but I think you are right, I get the same result with the test script you pasted above. And I think that with my first diff I would always get a full bar. Use my second diff.

luisdavim commented 8 years ago

but that line should have printf instead of a print:

awk "BEGIN { print \"%d\" (50) * 100 }"

returns %d5000

luisdavim commented 8 years ago

I think the difference is my tmux.conf.local you must be landing on the part the has:

full=$(awk "BEGIN { printf \"%.0f\", ($charge) * $battery_bar_length }")

and I'm landing on the elif where it has the:

full=$(awk "BEGIN { printf \"%d\", ($charge) * $battery_bar_length }")

After upgrading my ubuntu I've also made some changes to my .tmux.conf.local and that might have triggered the problem, not the upgrade.

gpakosz commented 8 years ago

Try without a ~/.tmux.conf.local.

Also look around where tmux_conf_battery_bar_length is defined in your buggy .tmux.conf.local file.

luisdavim commented 8 years ago

My ~/.tmux.conf.local is valid and tmux_conf_battery_bar_length is set to 5. The problem is that on line 340 you should have a printf instead of a print and that battery_vbar_bg is only set if you are using heat or gradient. Line 340 is also never used if you use heat or gradient.

With the default ~/.tmux.conf.local everything works because you never hit that part of the code.

gpakosz commented 8 years ago

Can you tell me which palette you're using then so that I can understand the problem in detail on my side please?

luisdavim commented 8 years ago

I'm using:

tmux_conf_battery_vbar_palette='#d70000,#ff5f00,#5fff00'

you can see my full ~/.tmux.conf.local here: https://github.com/luisdavim/dotfiles/blob/master/files/tmux.conf.local

luisdavim commented 8 years ago

the battery status freezes to the last state after you apply my ~/.tmux.conf.local, it disappears if you reboot afterwards and starts working again if you apply the changes from my second diff.

gpakosz commented 8 years ago

Thanks for helping debugging this 👍

luisdavim commented 8 years ago

No problem :)

gpakosz commented 8 years ago

Meh, force pushed as I forgot to silence the linter :(

luisdavim commented 8 years ago

since you are updating stuff, can you add a .gitignore file containing:

plugins/

I use some plugins and I keep them in ~/.tmux the same as this...

gpakosz commented 8 years ago

I could but doesn't that mean you should clone my configuration elsewhere?

gpakosz commented 8 years ago

In fact, I think it's a better idea you add ~/.tmux/plugins to a global ignore file, see https://git-scm.com/docs/gitignore