Skip to content

Commit e3999dc

Browse files
committed
Improve battery bar colors
1 parent 8d4f687 commit e3999dc

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
@@ -296,21 +296,33 @@ $COLUMNS = $imgwidth
296296

297297
# ===== UTILITY FUNCTIONS =====
298298
function get_percent_bar {
299-
param ([Parameter(Mandatory)][int]$percent)
299+
param (
300+
[Parameter(Mandatory)][int]$percent,
301+
[bool]$reverseColor
302+
)
300303

301304
if ($percent -gt 100) { $percent = 100 }
302305
elseif ($percent -lt 0) { $percent = 0 }
303306

304307
$x = [char]9632
305308
$bar = $null
306-
307-
$bar += "$e[97m[ $e[0m"
308-
for ($i = 1; $i -le ($barValue = ([math]::round($percent / 10))); $i++) {
309-
if ($i -le 6) { $bar += "$e[32m$x$e[0m" }
310-
elseif ($i -le 8) { $bar += "$e[93m$x$e[0m" }
311-
else { $bar += "$e[91m$x$e[0m" }
309+
$barValue = ([math]::round($percent / 10))
310+
311+
$bar += "$e[97m[ "
312+
if ($reverseColor) {
313+
if ($barValue -le 2) { $bar += "$e[91m" }
314+
elseif ($barValue -le 4) { $bar += "$e[93m" }
315+
else { $bar += "$e[32m" }
316+
for ($i = 1; $i -le $barValue; $i++) { $bar += "$x" }
317+
} else {
318+
for ($i = 1; $i -le $barValue; $i++) {
319+
if ($i -le 6) { $bar += "$e[32m$x$e[0m" }
320+
elseif ($i -le 8) { $bar += "$e[93m$x$e[0m" }
321+
else { $bar += "$e[91m$x$e[0m" }
322+
}
312323
}
313-
for ($i = 1; $i -le (10 - $barValue); $i++) { $bar += "$e[97m-$e[0m" }
324+
$bar += "$e[97m"
325+
for ($i = 1; $i -le (10 - $barValue); $i++) { $bar += "-" }
314326
$bar += "$e[97m ]$e[0m"
315327

316328
return $bar
@@ -322,13 +334,14 @@ function get_level_info {
322334
[string]$style,
323335
[int]$percentage,
324336
[string]$text,
325-
[switch]$altstyle
337+
[switch]$altstyle,
338+
[switch]$reverseColor
326339
)
327340

328341
switch ($style) {
329-
'bar' { return "$barprefix$(get_percent_bar $percentage)" }
330-
'textbar' { return "$text $(get_percent_bar $percentage)" }
331-
'bartext' { return "$barprefix$(get_percent_bar $percentage) $text" }
342+
'bar' { return "$barprefix$(get_percent_bar $percentage $reverseColor)" }
343+
'textbar' { return "$text $(get_percent_bar $percentage $reverseColor)" }
344+
'bartext' { return "$barprefix$(get_percent_bar $percentage $reverseColor) $text" }
332345
default { if ($altstyle) { return "$percentage% ($text)" } else { return "$text ($percentage%)" }}
333346
}
334347
}
@@ -944,7 +957,7 @@ function info_battery {
944957

945958
return @{
946959
title = "Battery"
947-
content = get_level_info " " $batterystyle "$([math]::round($battery.BatteryLifePercent * 100))" "$status$timeFormatted" -altstyle
960+
content = get_level_info " " $batterystyle "$([math]::round($battery.BatteryLifePercent * 100))" "$status$timeFormatted" -altstyle -reverseColor
948961
}
949962
}
950963

0 commit comments

Comments
 (0)