Last active
August 1, 2026 06:57
-
-
Save scriptingstudio/12dbe0baca9a677b233d05a3cf645e10 to your computer and use it in GitHub Desktop.
Colorful percent bar demo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function New-PercentBar { | |
| [cmdletbinding()] | |
| param ( | |
| [parameter(ValueFromPipeline)] | |
| [alias('inputobject')][decimal[]] $value, | |
| [alias('range')][decimal] $scale, | |
| [int] $barlength, | |
| [consolecolor] $color, | |
| [switch] $hint | |
| ) | |
| begin { | |
| if ($barlength -lt 10) {$barlength = 10} | |
| $colmap = @{ | |
| Black = "[0;30m" | |
| DarkBlue = "[38;5;21m" | |
| DarkGreen = "[38;5;70m" | |
| DarkCyan = "[38;5;39m" | |
| DarkRed = "[38;5;160m" | |
| DarkMagenta = "[38;5;88m" | |
| DarkYellow = "[38;5;179m" | |
| Gray = "[38;5;250m" | |
| DarkGray = "[38;5;244m" | |
| Blue = "[0;34m" | |
| Green = "[0;32m" | |
| Cyan = "[0;36m" | |
| Red = "[0;31m" | |
| Magenta = "[0;35m" | |
| Yellow = "[0;33m" | |
| White = "[0;37m" | |
| } | |
| $user = if ($null -ne $color) { | |
| $colmap[[string]$color] | |
| } else { | |
| $colmap['cyan'] | |
| } | |
| $barcolor = "$([char]27)$user" | |
| $end = "$([char]27)[0m" | |
| } | |
| process { | |
| if ($null -eq $scale) {return} | |
| $value | ForEach-Object { | |
| #if ($_ -gt $scale) {} | |
| [int]$unit = $_/$scale * $barlength | |
| $bar = $barcolor,('■'*$unit),$end -join '' | |
| if ($hint) {'{0,-6:P1} {1}' -f ($_/$scale),$bar} else {$bar} | |
| } | |
| } | |
| } # END New-PercentBar | |
| 566,135,'1077' | New-PercentBar -scale 1200 -barwidth 26 -color darkred -hint | |
| New-PercentBar -scale 1200 -value 566 -barwidth 25 | |
| New-PercentBar -scale 1200 -value 134 -barwidth 25 -hint -color DarkCyan | |
| New-PercentBar -scale 1200 -value 1041 -barwidth 25 -hint -color green |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment