Skip to content

Instantly share code, notes, and snippets.

@trackd
Last active April 30, 2025 20:32
Show Gist options
  • Save trackd/4fb71470d4feed4ae642c0a39476e5c3 to your computer and use it in GitHub Desktop.
Save trackd/4fb71470d4feed4ae642c0a39476e5c3 to your computer and use it in GitHub Desktop.
progress bar thing
function Write-ProgressBar {
<#
.NOTES
0 is the default state, and indicates that the progress bar should be hidden. Use this state when the command is complete, to clear out any progress state.
1: set progress value to <progress>, in the "default" state.
2: set progress value to <progress>, in the "Error" state
3: set the taskbar to the "Indeterminate" state. This is useful for commands that don't have a progress value, but are still running. This state ignores the <progress> value.
4: set progress value to <progress>, in the "Warning" state
<progress> is a number between 0 and 100, inclusive
.EXAMPLE
1..100 | % { $_ | Write-ProgressBar; sleep -Milliseconds 20 } -End { Write-ProgressBar -Clear }
.LINK
https://learn.microsoft.com/en-us/windows/terminal/tutorials/progress-bar-sequences#progress-bar-sequence-format
#>
[CmdletBinding()]
param(
[Parameter(ValueFromPipeline)]
[ValidateRange(0, 100)]
[int] $Progress = 50,
[Parameter(ValueFromPipelineByPropertyName)]
[ValidateSet('Default', 'Error', 'Spinning', 'Warning', 'Complete')]
[String] $Type = 'Default',
[Alias('Stop')]
[switch] $Clear
)
begin {
$StateId = @{
Complete = 0
Default = 1
Error = 2
Spinning = 3
Warning = 4
}
}
process {
if ($env:WEZTERM_EXECUTABLE) {
if ($Type -eq 'Warning') {
$Type = 'Error'
}
elseif ($Type -eq 'Spinning') {
$Type = 'Default'
}
}
if ($Clear.IsPresent) {
$Type = 'Complete'
}
[Console]::Write(('{0}]9;4;{1};{2}{3}' -f [char]27, $StateId[$Type], $Progress, [char]7))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment