Skip to content

Instantly share code, notes, and snippets.

@quonic
Last active May 28, 2023 20:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save quonic/59740fad470ed7edcdd7c596beae0b7c to your computer and use it in GitHub Desktop.
Save quonic/59740fad470ed7edcdd7c596beae0b7c to your computer and use it in GitHub Desktop.
An alternative to Start-Sleep that adds a progress bar and wait wheel using the FiraCode font. If not running in VSCode and FiraCode font is not setup, then it will fallback to ASCII.
function Start-SleepProgressBar {
<#
.SYNOPSIS
A Start-Sleep alternative that prints a progress bar with a spinning wheel at the end.
.DESCRIPTION
A Start-Sleep alternative that prints a progress bar with a spinning wheel at the end.
See: https://github.com/tonsky/FiraCode
.PARAMETER Seconds
Duration in seconds to wait.
.PARAMETER NoBar
Only displays a spinning wheel/line
.PARAMETER Force
Forces the display with FiraCode font specific Unicode.
.EXAMPLE
Start-SleepProgressBar -Seconds 10
A Start-Sleep alternative that prints a progress bar with a spinning wheel/line at the end.
Sample with out FiraCode font Output:
[################....................] -
Sample with FiraCode font Output:
 
.EXAMPLE
Start-SleepProgressBar -Seconds 10 -Force
Skip VSCode font detection.
A Start-Sleep alternative that prints a progress bar with a spinning wheel at the end.
Sample with FiraCode font Output:
 
#>
[CmdletBinding()]
param (
[Parameter(Mandatory)]
[Alias("WaitSeconds")]
$Seconds,
[switch]
$NoBar,
[switch]
$Force
)
begin {
$IsFiraCode = if ($env:TERM_PROGRAM -eq 'vscode' -and (Test-Path -Path "$env:APPDATA\Code\User\settings.json")) {
$VSCodeSettings = Get-Content -Path $env:APPDATA\Code\User\settings.json | ConvertFrom-Json
if ($VSCodeSettings."editor.fontFamily" -like "*Fira Code*") {
$true
}
else {
$false
}
}
else {
$false
}
$IsFiraCode = if ($Force) {
$true
}
else {
$IsFiraCode
}
# https://github.com/tonsky/FiraCode
$Progress = @(
if ($IsFiraCode) {
[char]0xEE06 # 
[char]0xEE07 # 
[char]0xEE08 # 
[char]0xEE09 # 
[char]0xEE0A # 
[char]0xEE0B # 
}
else {
"-"
"\"
"|"
"/"
}
)
function Get-ProgressBar {
param (
[Parameter(ValueFromPipeline)]
$Percentage,
$MaxLength = $($PSCmdlet.Host.UI.RawUI.WindowSize.Width - 2)
)
begin {
if ($IsFiraCode) {
$EmptyBarStart = [char]0xEE00 # 
$EmptyBar = [char]0xEE01 # 
$EmptyBarEnd = [char]0xEE02 # 
$FullBarStart = [char]0xEE03 # 
$FullBar = [char]0xEE04 # 
$FullBarEnd = [char]0xEE05 # 
}
else {
$EmptyBarStart = "[" # 
$EmptyBar = ":" # 
$EmptyBarEnd = "]" # 
$FullBarStart = "[" # 
$FullBar = "#" # 
$FullBarEnd = "]" # 
}
}
process {
$CurrentProgress = [System.Math]::Round(($Percentage / 100 * $MaxLength), 0)
$Bar = if ($CurrentProgress -ge ($MaxLength - 1)) {
"$FullBarStart"
"$FullBar" * ($MaxLength - 4)
"$FullBarEnd"
}
elseif ($CurrentProgress -gt 1) {
"$FullBarStart"
if (($CurrentProgress - 2) -ge 0) {
"$FullBar" * ($CurrentProgress - 2)
}
if (($CurrentProgress - 2) -lt $MaxLength -and ($MaxLength - $CurrentProgress - 2) -gt 0) {
"$EmptyBar" * ($MaxLength - $CurrentProgress - 2)
}
"$EmptyBarEnd"
}
else {
"$EmptyBarStart"
"$EmptyBar" * ($MaxLength - 4)
"$EmptyBarEnd"
}
$Bar -join ''
}
}
}
process {
$StartTime = Get-Date
function GetTime {
param (
$StartTime,
$Seconds
)
$(New-TimeSpan -Start $StartTime -End $(Get-Date))
}
while ($(GetTime($StartTime)).Seconds -lt $Seconds) {
for ($i = 0; $i -lt $Progress.Count; $i++) {
$ProgressBar = Get-ProgressBar -Percentage $($(GetTime($StartTime)).TotalSeconds / $Seconds * 100)
if ($NoBar) {
Write-Host -NoNewline -Object "`r$($Progress[$i])"
}
else {
Write-Host -NoNewline -Object "`r$($ProgressBar) $($Progress[$i])"
}
Start-Sleep -Milliseconds 100
}
}
Write-Host " "
# Replace with below to print a complete status
# Write-Host "`rDone!"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment