Skip to content

Instantly share code, notes, and snippets.

@mdgrs-mei
Created June 19, 2023 13:47
Show Gist options
  • Save mdgrs-mei/bb50ab4c20921b8be0be85de42118864 to your computer and use it in GitHub Desktop.
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
#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
@mdgrs-mei
Copy link
Author

mdgrs-mei commented Jun 19, 2023

DynamicTitle module is required. The spinner is shown only when a command takes over 1 second.

MoonSpinner

@MrBisquit
Copy link

Nice

@mdgrs-mei
Copy link
Author

@MrBisquit Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment