Skip to content

Commit

Permalink
Optimize battery reporting (#111)
Browse files Browse the repository at this point in the history
  • Loading branch information
rashil2000 authored Nov 6, 2021
1 parent 15ec7b3 commit f3cb9e2
Showing 1 changed file with 10 additions and 16 deletions.
26 changes: 10 additions & 16 deletions winfetch.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -856,41 +856,35 @@ function info_pkgs {

# ===== BATTERY =====
function info_battery {
$battery = Get-CimInstance Win32_Battery -CimSession $cimSession -Property BatteryStatus,EstimatedChargeRemaining,EstimatedRunTime,TimeToFullCharge
Add-Type -AssemblyName System.Windows.Forms
$battery = [System.Windows.Forms.SystemInformation]::PowerStatus

if (-not $battery) {
if ($battery.BatteryChargeStatus -eq 'NoSystemBattery') {
return @{
title = "Battery"
content = "(none)"
}
}

$power = Get-CimInstance BatteryStatus -Namespace root\wmi -CimSession $cimSession -Property Charging,PowerOnline

$status = if ($power.Charging) {
$status = if ($battery.BatteryChargeStatus -like '*Charging*') {
"Charging"
} elseif ($power.PowerOnline) {
} elseif ($battery.PowerLineStatus -like '*Online*') {
"Plugged in"
} else {
"Discharging"
}

$timeRemaining = if ($power.Charging) {
$battery.TimeToFullCharge
} else {
$battery.EstimatedRunTime
}

# don't show time remaining if windows hasn't properly reported it yet
$timeFormatted = if ($timeRemaining -and $timeRemaining -lt 71582788) {
$timeRemaining = $battery.BatteryLifeRemaining / 60
# Don't show time remaining if Windows hasn't properly reported it yet
$timeFormatted = if ($timeRemaining -ge 0) {
$hours = [math]::floor($timeRemaining / 60)
$minutes = $timeRemaining % 60
$minutes = [math]::floor($timeRemaining % 60)
", ${hours}h ${minutes}m"
}

return @{
title = "Battery"
content = get_level_info " " $batterystyle $battery.EstimatedChargeRemaining "$status$timeFormatted" -altstyle
content = get_level_info " " $batterystyle "$([math]::round($battery.BatteryLifePercent * 100))" "$status$timeFormatted" -altstyle
}
}

Expand Down

0 comments on commit f3cb9e2

Please sign in to comment.