Update write-value.ps1

This commit is contained in:
Markus Fleschutz 2024-12-03 14:20:27 +01:00
parent 724da873d2
commit 2b6b3dc32e

View File

@ -1,78 +1,64 @@
<#
.SYNOPSIS
Writes a value with value range
Writes a value with unit and range
.DESCRIPTION
This PowerShell script writes the given value with value range to the console.
This PowerShell script writes the given value with the unit and the value range to the console.
.PARAMETER value
Specifies the value
.EXAMPLE
PS> ./write-value.ps1 0.5 Mach 0 10
[00.5 Mach10]
PS> ./write-value.ps1 95.0 "°C" 0 10 90 100
[0--------95°C-100]
.LINK
https://github.com/fleschutz/PowerShell
.NOTES
Author: Markus Fleschutz | License: CC0
#>
param([float]$value = 0.5, [string]$unit = "Mach", [float]$redMin = 0.0, [float]$redMax = 10.0)
param([float]$value = 0.5, [string]$unit = "Mach", [float]$redMin, [float]$yellowMin, [float]$yellowMax, [float]$redMax)
function WriteLine([float]$count) {
while ($count -ge 1.0) {
Write-Host "" -noNewline
$count -= 1.0
}
return $count
}
function WriteBlock([float]$value, [string]$unit, [float]$redMin, [float]$yellowMin, [float]$yellowMax, [float]$redMax) {
$text = "[$redMin $($value)$unit $redMax]"
[float]$left = 20.0 - $text.Length
function WriteValueInRange([float]$value, [string]$unit, [float]$redMin, [float]$yellowMin, [float]$yellowMax, [float]$redMax) {
$line = "------------------------------------------------"
$text = "[$redMin$($value)$unit $redMax]"
[float]$total = 20.0 - $text.Length
if ($value -gt $redMax) {
Write-Host "[$redMin" -noNewline
$rest = WriteLine $left
Write-Host "$redMax]" -noNewline
Write-Host " $($value)$unit " -noNewline -foregroundColor red
Write-Host "[$redMin$($line.Substring(0, $total))$redMax]" -noNewline
Write-Host "$($value)$unit " -noNewline -foregroundColor red
} elseif ($value -lt $redMin) {
Write-Host "$($value)$unit" -noNewline -foregroundColor red
Write-Host " [$redMin" -noNewline
$rest = WriteLine $left
Write-Host "$redMax] " -noNewline
} elseif (($value -le $yellowMin) -or ($value -ge $yellowMax)) {
[float]$percent = (($value - $redMin) * $left) / ($redMax - $redMin)
Write-Host "[$redMin" -noNewline
$rest = WriteLine $percent
Write-Host "$($value)$unit" -noNewline -foregroundColor yellow
$rest = WriteLine ($left - $percent + $rest + 1.0)
Write-Host "$redMax] " -noNewline
Write-Host "[$redMin$($line.Substring(0, $total))$redMax] " -noNewline
} else {
[float]$percent = (($value - $redMin) * $left) / ($redMax - $redMin)
Write-Host "[$redMin" -noNewline
$rest = WriteLine $percent
Write-Host "$($value)$unit" -noNewline -foregroundColor green
$rest = WriteLine ($left - $percent + $rest + 1.0)
Write-Host "$redMax] " -noNewline
[float]$leftSide = (($value - $redMin) * $total) / ($redMax - $redMin)
if ($leftSide -lt 1.0) { $leftSide = 1.0 }
if ($leftSide -gt ($total - 1.0)) { $leftSide = $total - 1.0 }
Write-Host "[$redMin$($line.Substring(0, $leftSide))" -noNewline
if (($value -le $yellowMin) -or ($value -ge $yellowMax)) {
Write-Host "$($value)$unit" -noNewline -foregroundColor yellow
} else {
Write-Host "$($value)$unit" -noNewline -foregroundColor green
}
Write-Host "$($line.Substring(0, $total - $leftSide + 0.49))$redMax] " -noNewline
}
}
WriteBlock -3.5 "°C" 0 10 90 100
WriteValueInRange -3.5 "°C" 0 10 90 100
Write-Host "CPU too cold"
WriteBlock 5 "°C" 0 10 90 100
WriteValueInRange 5 "°C" 0 10 90 100
Write-Host "CPU quite cold"
WriteBlock 15 "°C" 0 10 90 100
WriteValueInRange 15 "°C" 0 10 90 100
Write-Host "OK"
WriteBlock 50 "°C" 0 10 90 100
WriteValueInRange 50 "°C" 0 10 90 100
Write-Host "OK"
WriteBlock 70 "°C" 0 10 90 100
WriteValueInRange 70 "°C" 0 10 90 100
Write-Host "OK"
WriteBlock 95 "°C" 0 10 90 100
WriteValueInRange 95 "°C" 0 10 90 100
Write-Host "CPU quite hot "
WriteBlock 110 "°C" 0 10 90 100
WriteValueInRange 110 "°C" 0 10 90 100
Write-Host "CPU too hot "
exit 0 # success