Skip to content

Commit 59d6d28

Browse files
committed
Improve battery bar colors
1 parent 72c3403 commit 59d6d28

File tree

1 file changed

+26
-13
lines changed

1 file changed

+26
-13
lines changed

winfetch.ps1

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -290,18 +290,30 @@ $COLUMNS = $imgwidth
290290

291291
# ===== UTILITY FUNCTIONS =====
292292
function get_percent_bar {
293-
param ([Parameter(Mandatory)][ValidateRange(0, 100)][int]$percent)
293+
param (
294+
[Parameter(Mandatory)][ValidateRange(0, 100)][int]$percent,
295+
[bool]$reverseColor
296+
)
294297

295298
$x = [char]9632
296299
$bar = $null
297-
298-
$bar += "$e[97m[ $e[0m"
299-
for ($i = 1; $i -le ($barValue = ([math]::round($percent / 10))); $i++) {
300-
if ($i -le 6) { $bar += "$e[32m$x$e[0m" }
301-
elseif ($i -le 8) { $bar += "$e[93m$x$e[0m" }
302-
else { $bar += "$e[91m$x$e[0m" }
300+
$barValue = ([math]::round($percent / 10))
301+
302+
$bar += "$e[97m[ "
303+
if ($reverseColor) {
304+
if ($barValue -le 2) { $bar += "$e[91m" }
305+
elseif ($barValue -le 4) { $bar += "$e[93m" }
306+
else { $bar += "$e[32m" }
307+
for ($i = 1; $i -le $barValue; $i++) { $bar += "$x" }
308+
} else {
309+
for ($i = 1; $i -le $barValue; $i++) {
310+
if ($i -le 6) { $bar += "$e[32m$x$e[0m" }
311+
elseif ($i -le 8) { $bar += "$e[93m$x$e[0m" }
312+
else { $bar += "$e[91m$x$e[0m" }
313+
}
303314
}
304-
for ($i = 1; $i -le (10 - $barValue); $i++) { $bar += "$e[97m-$e[0m" }
315+
$bar += "$e[97m"
316+
for ($i = 1; $i -le (10 - $barValue); $i++) { $bar += "-" }
305317
$bar += "$e[97m ]$e[0m"
306318

307319
return $bar
@@ -313,13 +325,14 @@ function get_level_info {
313325
[string]$style,
314326
[int]$percentage,
315327
[string]$text,
316-
[switch]$altstyle
328+
[switch]$altstyle,
329+
[switch]$reverseColor
317330
)
318331

319332
switch ($style) {
320-
'bar' { return "$barprefix$(get_percent_bar $percentage)" }
321-
'textbar' { return "$text $(get_percent_bar $percentage)" }
322-
'bartext' { return "$barprefix$(get_percent_bar $percentage) $text" }
333+
'bar' { return "$barprefix$(get_percent_bar $percentage $reverseColor)" }
334+
'textbar' { return "$text $(get_percent_bar $percentage $reverseColor)" }
335+
'bartext' { return "$barprefix$(get_percent_bar $percentage $reverseColor) $text" }
323336
default { if ($altstyle) { return "$percentage% ($text)" } else { return "$text ($percentage%)" }}
324337
}
325338
}
@@ -956,7 +969,7 @@ function info_battery {
956969

957970
return @{
958971
title = "Battery"
959-
content = get_level_info " " $batterystyle "$([math]::round($battery.BatteryLifePercent * 100))" "$status$timeFormatted" -altstyle
972+
content = get_level_info " " $batterystyle "$([math]::round($battery.BatteryLifePercent * 100))" "$status$timeFormatted" -altstyle -reverseColor
960973
}
961974
}
962975

0 commit comments

Comments
 (0)