Private/Show-PercentageGraph.ps1

#function Show-PercentageGraph([int]$percent, [int]$maxSize=20) {
#
# # Default values
# $warningThreshold = 60 # percent
# $alertThreshold = 80 # percent
# $textcolor = "green" # default text color
#
# [string]$bloc = [char]9632 # Ascii code for block
# [string]$fill = "-" # Ascii code for smaller "-" [char]9472
#
# # save percentage, as values can be over 100% depending the calculation
# $percentage = $percent
#
# # to avoid the drawing the bar over 100%.
# if ($percent -gt 100) {
# $percent = 100
# }
#
# write-host -nonewline " [ "
#
# # calculate the bar value
# $barValue = [math]::floor($percent*$maxSize/100)
#
# # draw the bar using $bloc ascii char & color
# for ($i=1; $i -le $barValue; $i++) {
# if ($i -le ($warningThreshold*$maxSize/100)) {
# write-host -nonewline -foregroundcolor green $bloc
# $textcolor = "Green"
# }
# elseif ($i -le ($alertThreshold*$maxSize/100)) {
# write-host -nonewline -foregroundcolor yellow $bloc
# $textcolor = "Yellow"
# }
# else {
# write-host -nonewline -foregroundcolor red $bloc
# $textcolor = "Red"
# }
# }
#
# # calculate the bar filler
# $traitValue = $maxSize-$barValue
#
# # draw the filler after bar
# for ($i=1; $i -le $traitValue; $i++) {
# write-host -nonewline "$fill"
# }
#
# if ($percentage -lt 10) {
# write-host -nonewline " ] "
# write-host -nonewline -ForegroundColor $textcolor "$percentage%"
# }
# elseif ($percentage -ge 100) {
# write-host -nonewline " ] "
# write-host -nonewline -ForegroundColor $textcolor "$percentage%"
# }
# else {
# write-host -nonewline " ] "
# write-host -nonewline -ForegroundColor $textcolor "$percentage%"
# }
#}
#
#

function Show-PercentageGraph {
    param(
        [int]$percent,
        [int]$maxSize = 20,
        [switch]$Invert
    )
    
    # Default values
    $warningThreshold   = 60         # percent
    $alertThreshold     = 80         # percent
    $textcolor          = "green"    # default text color
    
    [string]$bloc       = [char]9632 # Ascii code for block
    [string]$fill       = "-"        # Ascii code for smaller "-" [char]9472
    
    # save percentage, as values can be over 100% depending the calculation
    $percentage = $percent
    
    # to avoid the drawing the bar over 100%.
    if ($percent -gt 100) { 
        $percent = 100
    }
    
    write-host -nonewline " [ " 
  
    # calculate the bar value
    $barValue = [math]::floor($percent*$maxSize/100)
    
    # draw the bar using $bloc ascii char & color
    for ($i=1; $i -le $barValue; $i++) {
        # Calculate current percentage position for this block
        $currentPercent = ($i / $maxSize) * 100
        
        if ($Invert) {
            # Inverted logic: high percentage = green (good), low percentage = red (bad)
            if ($currentPercent -le 40) { 
                write-host -nonewline -foregroundcolor red $bloc
                $textcolor = "Red"
            }
            elseif ($currentPercent -le 60) { 
                write-host -nonewline -foregroundcolor yellow $bloc
                $textcolor = "Yellow"
            }
            else {
                write-host -nonewline -foregroundcolor green $bloc
                $textcolor = "Green"
            }
        }
        else {
            # Normal logic: low percentage = green (good), high percentage = red (bad)
            if ($currentPercent -le $warningThreshold) { 
                write-host -nonewline -foregroundcolor green $bloc
                $textcolor = "Green"
            }
            elseif ($currentPercent -le $alertThreshold) { 
                write-host -nonewline -foregroundcolor yellow $bloc
                $textcolor = "Yellow"
            }
            else {
                write-host -nonewline -foregroundcolor red $bloc
                $textcolor = "Red"
            }
        }
    }
    
    # calculate the bar filler
    $traitValue = $maxSize-$barValue
    
    # draw the filler after bar
    for ($i=1; $i -le $traitValue; $i++) { 
        write-host -nonewline "$fill" 
    }
        
    if ($percentage -lt 10) { 
        write-host -nonewline " ] " 
        write-host -nonewline -ForegroundColor $textcolor "$percentage%" 
    } 
    elseif ($percentage -ge 100) {
        write-host -nonewline " ] " 
        write-host -nonewline -ForegroundColor $textcolor "$percentage%" 
    }
    else { 
        write-host -nonewline " ] " 
        write-host -nonewline -ForegroundColor $textcolor "$percentage%" 
    }
}

function Show-PercentageGraphSolid {
    param(
        [int]$percent,
        [int]$maxSize = 20,
        [switch]$Invert
    )
    
    # Default values
    $warningThreshold   = 60         # percent
    $alertThreshold     = 80         # percent
    $textcolor          = "green"    # default text color
    
    [string]$bloc       = [char]9632 # Ascii code for block
    [string]$fill       = "-"        # Ascii code for smaller "-" [char]9472
    
    # save percentage, as values can be over 100% depending the calculation
    $percentage = $percent
    
    # to avoid the drawing the bar over 100%.
    if ($percent -gt 100) { 
        $percent = 100
    }
    
    # Determine the color for the ENTIRE bar based on percentage
    if ($Invert) {
        # Inverted logic: high percentage = green (good), low percentage = red (bad)
        if ($percent -lt 40) {
            $barColor = "Red"
            $textcolor = "Red"
        }
        elseif ($percent -lt 60) {
            $barColor = "Yellow"
            $textcolor = "Yellow"
        }
        else {
            $barColor = "Green"
            $textcolor = "Green"
        }
    }
    else {
        # Normal logic: low percentage = green (good), high percentage = red (bad)
        if ($percent -le $warningThreshold) {
            $barColor = "Green"
            $textcolor = "Green"
        }
        elseif ($percent -le $alertThreshold) {
            $barColor = "Yellow"
            $textcolor = "Yellow"
        }
        else {
            $barColor = "Red"
            $textcolor = "Red"
        }
    }
    
    write-host -nonewline " [ " 
  
    # calculate the bar value
    $barValue = [math]::floor($percent*$maxSize/100)
    
    # draw the bar using $bloc ascii char with single color
    for ($i=1; $i -le $barValue; $i++) {
        write-host -nonewline -foregroundcolor $barColor $bloc
    }
    
    # calculate the bar filler
    $traitValue = $maxSize-$barValue
    
    # draw the filler after bar
    for ($i=1; $i -le $traitValue; $i++) { 
        write-host -nonewline "$fill" 
    }
        
    if ($percentage -lt 10) { 
        write-host -nonewline " ] " 
        write-host -nonewline -ForegroundColor $textcolor "$percentage%" 
    } 
    elseif ($percentage -ge 100) {
        write-host -nonewline " ] " 
        write-host -nonewline -ForegroundColor $textcolor "$percentage%" 
    }
    else { 
        write-host -nonewline " ] " 
        write-host -nonewline -ForegroundColor $textcolor "$percentage%" 
    }
}