Spaceship-prompt: Bad math expression

Created on 20 Oct 2017  路  33Comments  路  Source: denysdovhan/spaceship-prompt

Issue

I get the following error

spaceship_battery:37: bad math expression: operator expected at `47'

Screenshot

Provide a screenshot that show your issue

screenshot

Environment

Operating system: Ubuntu 16.04 LTS
Terminal emulator: Terminator
ZSH version: zsh 5.1.1 (x86_64-ubuntu-linux-gnu)

question

All 33 comments

Huh, I don't see that issue on my system (also ubuntu, zsh 5.1.1)

It definitely happened on one of my computers. On one of my other computers, it didn't happen.

However, this fixed it for both. If this doesn't break it on your computer, it would definitely be nice to merge in.

Could you match my version of zsh and see what happens?

I'm using 5.1.1 and I see no issue

Are you using terminator?

I'm using terminator 0.98

I have no idea why this fixed it, but it did.

I don't think the choice of terminal should affect this, it should only be related to zsh version. You mentioned you have 2 machines, can you double check the version (echo $ZSH_VERSION) on the machine with the bug?

I will check tomorrow. Both my machines dotfiles are exactly identical. I use https://github.com/jamrizzi/dotcli to keep them in sync.

One other interesting thing to note is that the -lt flag works on the machine that spaceship does not work. But, in the spaceship, when I replaced -lt with <, it fixed it. Basically, this is super confusing to debug. It could be a super edge case in the script.

I just disabled the whole battery function for myself. I think it's super weird to have my battery stats in my terminal like that anyways. I'm still not sure what the problem was, but it's super isolated and not really a problem for me now anyways.

I realize this is closed. But I wanted to comment for future me. Or others that google this. I am getting this and I am 99% certain it's because I have two batteries. The number that is reported in the error message is the percentage from the second battery. If I new zsh more, I would submit a patch, but I don't. I'm happy to help test if someone else wants to take a shot

Example of the problem and example of the output that two batteries gives.

~ acpi -b | awk '{print tolower($3)}' | tr -d '%[,;'
unknown
charging
spaceship_battery:40: bad math expression: operator expected at `23'
~ acpi -b | awk '{print $4}' | tr -d '%[,;]'
86
23
spaceship_battery:40: bad math expression: operator expected at `23'
~

Oh dang, good find. I have two batteries also.

I'm getting this error too now (spaceship_battery:56: bad math expression: operator expected at '100') on macOS 10.14.1. That happened after I connected by device to a UPS.

  if [[ $SPACESHIP_BATTERY_SHOW == 'always' ||
        $battery_percent -lt $SPACESHIP_BATTERY_THRESHOLD || # <-- this is line 56
        $SPACESHIP_BATTERY_SHOW == 'charged' && $battery_status =~ "(charged|full)" ]]; then
$ pmset -g batt
Now drawing from 'AC Power'
 -InternalBattery-0 (id=3473507)    100%; charged; 0:00 remaining present: true
 -Back-UPS RS 900G FW:879.L4 .I USB FW:L4   (id=8126464)    100%; charging present: true

$ pmset -g batt | grep -oE '[0-9]{1,3}%'
100%
100%

Basically $battery_percent is 100\n100

I am having the same issue on my laptop with two batteries connected.

@n2o What laptop is that - MacBook? On what line does it show the error? Is the on the current version of spaceship prompt?
If you're on macOS, can you copy the output of pmset -g batt please?

Laptop: Thinkpad T450S
OS: Arch Linux
Shell: urxvt
Spaceship-Version: 3.8.1
Batteries: 2

Each command ends with this message and does not show the normal char-symbol:

spaceship_battery:44: bad math expression: operator expected at `99'

Here is a short video of it
term.zip

screenshot-20181204-094038

Line 44 in battery.zsh is this version:

    [[ -z "$battery_data" ]] && return

This means line 44 inside spaceship_battery function, so it's:

elif [[ $battery_percent -lt $SPACESHIP_BATTERY_THRESHOLD ]]; then

Could you please paste the output of upower -e?

位 upower -e
/org/freedesktop/UPower/devices/line_power_AC
/org/freedesktop/UPower/devices/battery_BAT0
/org/freedesktop/UPower/devices/battery_BAT1
/org/freedesktop/UPower/devices/DisplayDevice

Could you also do that for upower -i /org/freedesktop/UPower/devices/battery_BAT0 please?

Sure

位 upower -i /org/freedesktop/UPower/devices/battery_BAT0
  native-path:          BAT0
  vendor:               SANYO
  model:                45N1773
  serial:               20253
  power supply:         yes
  updated:              Di 04 Dez 2018 12:51:48 CET (46 seconds ago)
  has history:          yes
  has statistics:       yes
  battery
    present:             yes
    rechargeable:        yes
    state:               fully-charged
    warning-level:       none
    energy:              16,64 Wh
    energy-empty:        0 Wh
    energy-full:         16,97 Wh
    energy-full-design:  23,2 Wh
    energy-rate:         0 W
    voltage:             12,244 V
    percentage:          98%
    capacity:            73,1466%
    technology:          lithium-ion
    icon-name:          'battery-full-charged-symbolic'
  History (charge):
    1543924308  98,000  fully-charged

This is very strange.

$ battery_data=$(upower -i /org/freedesktop/UPower/devices/battery_BAT0)
$ battery_percent=$(echo "$battery_data" | grep percentage | awk '{print $2}' | tr -d '%[,;]')
$ echo $battery_percent
98
$ [[ $battery_percent -lt 10 ]] && echo y || echo n
n
$ [[ $battery_percent -lt 100 ]] && echo y || echo n
y

What is the zsh version you're using? (zsh --version)
Are you using the latest spaceship-prompt from master branch?
What is the value of $SPACESHIP_BATTERY_THRESHOLD?

位 zsh --version
zsh 5.6.2 (x86_64-pc-linux-gnu)

I am on the latest commit on master

*   382b058 - (20 hours ago) Merge pull request #580 from segevfiner/patch-1 - Denys Dovhan (HEAD -> master, origin/master, origin/HEAD)
位 echo $SPACESHIP_BATTERY_THRESHOLD
10

I disabled the battery to make Spaceship work as expected

SPACESHIP_BATTERY_SHOW=false

Your commands work as expected, too :D :

位 battery_data=$(upower -i /org/freedesktop/UPower/devices/battery_BAT0)
位 battery_percent=$(echo "$battery_data" | grep percentage | awk '{print $2}' | tr -d '%[,;]')
位 echo $battery_percent
98

The next step I'd do is this -

On line 75 of sections/battery.zsh there's this part:

  # Change color based on battery percentage
  if [[ $battery_percent == 100 || $battery_status =~ "(charged|full)" ]]; then
    battery_color="green"
  elif [[ $battery_percent -lt $SPACESHIP_BATTERY_THRESHOLD ]]; then
    battery_color="red"
  else
    battery_color="yellow"
  fi

If you add set -x just before that and set +x right after it and open a new shell (or just run zsh -l), you will see the actual commands that are being executed. You might see what happened just before the error happened.

Okay, did this and this is the output:

+spaceship_battery:44> [[ $'97\n96' == 100 || $battery_status -regex-match "(charged|full)" ]]
+spaceship_battery:46> [[ $'97\n96' -lt 10spaceship_battery:46: bad math expression: operator expected at `96'
 ]]

~ +spaceship_battery:44> [[ $'97\n96' == 100 || $battery_status -regex-match "(charged|full)" ]]
+spaceship_battery:46> [[ $'97\n96' -lt 10spaceship_battery:46: bad math expression: operator expected at `96'

Huh! Does it mean that upower -e | grep battery | head -1 prints more than 1 line?!

Could you try setting set -x at the beginning of the function and set +x before the end?

$ upower -e | grep battery | head -1
/org/freedesktop/UPower/devices/battery_BAT0

Adding set -x at the beginning of the function in battery.zsh and set -x to the end produces this:

+spaceship_battery:3> [[ true == false ]]
+spaceship_battery:5> local battery_data battery_percent battery_status battery_color
+spaceship_battery:7> spaceship::exists pmset
+spaceship::exists:1> command -v pmset
+spaceship_battery:15> spaceship::exists acpi
+spaceship::exists:1> command -v acpi
+spaceship_battery:16> battery_data=+spaceship_battery:16> acpi -b
+spaceship_battery:16> battery_data=$'Battery 0: Unknown, 97%\nBattery 1: Unknown, 98%' 
+spaceship_battery:19> [[ -z $'Battery 0: Unknown, 97%\nBattery 1: Unknown, 98%' ]]
+spaceship_battery:21> battery_percent=+spaceship_battery:21> echo $'Battery 0: Unknown, 97%\nBattery 1: Unknown, 98%'
+spaceship_battery:21> battery_percent=+spaceship_battery:21> awk '{print $4}'
+spaceship_battery:21> battery_percent=$'97%\n98%' 
+spaceship_battery:24> [[ $'97%\n98%' == 0%, ]]
+spaceship_battery:26> battery_status=+spaceship_battery:26> echo $'Battery 0: Unknown, 97%\nBattery 1: Unknown, 98%'
+spaceship_battery:26> battery_status=+spaceship_battery:26> awk '{print tolower($3)}'
+spaceship_battery:26> battery_status=$'unknown,\nunknown,' 
+spaceship_battery:41> battery_percent=+spaceship_battery:41> echo $'97%\n98%'
+spaceship_battery:41> battery_percent=+spaceship_battery:41> tr -d '%[,;]'
+spaceship_battery:41> battery_percent=$'97\n98' 
+spaceship_battery:44> [[ $'97\n98' == 100 || $battery_status -regex-match "(charged|full)" ]]
+spaceship_battery:46> [[ $'97\n98' -lt 10spaceship_battery:46: bad math expression: operator expected at `98'

I don't know why I always assumed that your system used upower instead of acpi command...
This definitely helped though :)

Just pushed MR #583 that should fix this for you.

I would still prefer a solution similar to my PR ( #365 ) that I made some time back. The title should probably be updated. Here is a second attempt. This one sums the available mAh, and divides it by the total mAh and uses that as the percentage.

IMO it makes sense. Either average or Max of the available batteries.

But this is a feature. I think it's important to fix the bug that exists, unrelated to enhancing battery display.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

tbekaert picture tbekaert  路  3Comments

ChadTaljaardt picture ChadTaljaardt  路  4Comments

jwhipp picture jwhipp  路  3Comments

AlecRust picture AlecRust  路  3Comments

xhaythemx picture xhaythemx  路  3Comments