Created
June 19, 2023 13:47
-
-
Save mdgrs-mei/bb50ab4c20921b8be0be85de42118864 to your computer and use it in GitHub Desktop.
Show a moon spinner on the tab title using DynamicTitle PowerShell module
This file contains 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
#Requires -Modules DynamicTitle | |
$commandStartJob = Start-DTJobCommandPreExecutionCallback -ScriptBlock { | |
param($command) | |
(Get-Date), $command | |
} | |
$promptJob = Start-DTJobPromptCallback -ScriptBlock { | |
(Get-Date) | |
} | |
$initializationScript = { | |
$spinnerSymbols = @('π', 'π', 'π', 'π', 'π', 'π', 'π', 'π') | |
$spinnerSymbolIndex = 0 | |
} | |
$update = { | |
param($commandStartJob, $promptJob) | |
$commandStartDate, $command = Get-DTJobLatestOutput $commandStartJob | |
$commandEndDate = Get-DTJobLatestOutput $promptJob | |
if ($null -ne $commandStartDate) { | |
if (($null -eq $commandEndDate) -or ($commandEndDate -lt $commandStartDate)) { | |
$commandDuration = (Get-Date) - $commandStartDate | |
$isCommandRunning = $true | |
} | |
else { | |
$commandDuration = $commandEndDate - $commandStartDate | |
} | |
} | |
if ($command) { | |
$command = $command.Split()[0] | |
} | |
$spinner = $spinnerSymbols[0] | |
if ($commandDuration) { | |
if ($commandDuration.TotalSeconds -gt 1) { | |
$commandSegment = 'β [{0}]-β{1}' -f $command, $commandDuration.ToString('mm\:ss') | |
if ($isCommandRunning) { | |
$script:spinnerSymbolIndex = ($script:spinnerSymbolIndex + 1) % $spinnerSymbols.Count | |
$spinner = $spinnerSymbols[$script:spinnerSymbolIndex] | |
} | |
} | |
else { | |
$script:spinnerSymbolIndex = 0 | |
} | |
} | |
'{0} PowerShell {1}' -f $spinner, $commandSegment | |
} | |
$params = @{ | |
ScriptBlock = $update | |
ArgumentList = $commandStartJob, $promptJob | |
InitializationScript = $initializationScript | |
} | |
Start-DTTitle @params |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
DynamicTitle module is required. The spinner is shown only when a command takes over 1 second.