Skip to content
This repository has been archived by the owner on Apr 24, 2020. It is now read-only.

Windows battery support #887

Closed
wants to merge 2 commits into from
Closed

Windows battery support #887

wants to merge 2 commits into from

Conversation

ratijas
Copy link

@ratijas ratijas commented Jun 29, 2018

Initial support for battery widget on Windows OS.

It uses WMIC output which looks pretty reliable to parse with stuff like grep and sed.

I admit there's still some work to do, like supporting systems without batteries and respecting $POWERLEVEL9K_BATTERY_LOW_THRESHOLD variable.

But it's a good starting point, and I would be glad to hear feedback from MINGW / MSYS2 users.

@dritter dritter added this to In progress in v0.7.0 via automation Sep 18, 2018
@dritter
Copy link
Member

dritter commented Oct 9, 2018

Hi @ratijas !

Thank you very much for your PR, and sorry for the delay. We've done quite some work on the next branch in the meantime. Could you update your PR?
And could you provide the output of wmic Path Win32_Battery Get /format:list and add a test for the battery segment on windows. That would be awesome.

Copy link
Member

@dritter dritter left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @ratijas ! Thanks for taking the time to put up this PR. I left a few comments.

local raw_data="$(wmic Path Win32_Battery Get /format:list | tr -d '\r' | sed -n '/=./p')"

# type bat_percent: percent as int in range 0..100
local bat_percent="$(echo "$raw_data" | grep "EstimatedChargeRemaining" | cut -d = -f 2)"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
local bat_percent="$(echo "$raw_data" | grep "EstimatedChargeRemaining" | cut -d = -f 2)"
local bat_percent
[[ "${raw_data}" =~ "EstimatedChargeRemaining=([0-9]+)" ]] && bat_percent="${match[1]}"

# type bat_percent: percent as int in range 0..100
local bat_percent="$(echo "$raw_data" | grep "EstimatedChargeRemaining" | cut -d = -f 2)"

local minutes_remaining="$(echo "$raw_data" | grep "EstimatedRunTime" | cut -d = -f 2)"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
local minutes_remaining="$(echo "$raw_data" | grep "EstimatedRunTime" | cut -d = -f 2)"
local minutes_remaining
[[ "${raw_data}" =~ "EstimatedRunTime=([0-9]+)" ]] && minutes_remaining="${match[1]}"

local remain=" ($tstring)"

local current_state=''
local battery_status="$(echo "$raw_data" | grep "BatteryStatus" | cut -d = -f 2)"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
local battery_status="$(echo "$raw_data" | grep "BatteryStatus" | cut -d = -f 2)"
local battery_status
[[ "${raw_data}" =~ "BatteryStatus=([0-9]+)" ]] && battery_status="${match[1]}"


local minutes_remaining="$(echo "$raw_data" | grep "EstimatedRunTime" | cut -d = -f 2)"
local tstring="..."
if [[ "$minutes_remaining" =~ "[[:digit:]]+" ]]; then
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This check shouldn't be necessary with my previous comment (already greps a digit)..

# https://docs.microsoft.com/ru-ru/windows/desktop/CIMWin32Prov/win32-battery

# skip empty lines AND empty values, leave only meaningful key=value pairs
local raw_data="$(wmic Path Win32_Battery Get /format:list | tr -d '\r' | sed -n '/=./p')"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Filter only meaningful key/value pairs is not necessary IMHO. We are dealing with about 30 keys here. Not very much data.. Also, we would avoid external programs as much as possible (tr and sed).

@dritter
Copy link
Member

dritter commented Nov 4, 2018

Btw. I just merged another PR that touches the battery segment. Could you update your PR?

For the records, here is a sample output of wmic Path Win32_Battery Get /format:list.

@dritter dritter mentioned this pull request Dec 18, 2018
@dritter
Copy link
Member

dritter commented Dec 18, 2018

Closing in favour of #1120

@dritter dritter closed this Dec 18, 2018
v0.7.0 automation moved this from In progress to Done Dec 18, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
v0.7.0
  
Done
Development

Successfully merging this pull request may close these issues.

None yet

2 participants