From 0a2bb25700adb70d16273ae122afe926570e2c53 Mon Sep 17 00:00:00 2001 From: Gal Szkolnik Date: Wed, 14 Jul 2021 17:05:48 -0400 Subject: [PATCH] Added better constrol over waiting for editor --- base/Edit-MyScript.ps1 | 6 +++--- base/Edit-TextFile.ps1 | 11 ++++++++++- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/base/Edit-MyScript.ps1 b/base/Edit-MyScript.ps1 index 43f7917..2f23580 100644 --- a/base/Edit-MyScript.ps1 +++ b/base/Edit-MyScript.ps1 @@ -126,11 +126,11 @@ foreach( $local:p in $ScriptPaths ) { } } -$local:sw = [System.Diagnostics.Stopwatch]::StartNew(); +$local:EditorWaitStopWatch = [System.Diagnostics.Stopwatch]::StartNew(); Edit-TextFile -Path $ScriptPaths -$sw.Stop(); +$EditorWaitStopWatch.Stop(); -if( $sw.Elapsed.TotalSeconds -lt 3 ) { +if( $EditorWaitStopWatch.Elapsed.TotalSeconds -lt $WaitForEditor ) { Write-Host -ForegroundColor DarkGreen "When done editing, you might want to run '. Reload-MyScripts'" return } diff --git a/base/Edit-TextFile.ps1 b/base/Edit-TextFile.ps1 index 4181a5c..234a27e 100644 --- a/base/Edit-TextFile.ps1 +++ b/base/Edit-TextFile.ps1 @@ -5,6 +5,7 @@ ) begin { + $global:WaitForEditor = (($WaitForEditor, 0 -ne $null)[0], 5 -ne 0)[0] $PathForEditor = @() } process { @@ -29,7 +30,15 @@ end { if( $editor -match 'code(\.exe)?"$' ) { if( -not (Get-Process -Name 'code' -ErrorAction SilentlyContinue) ) { - Invoke-ExpressionEx -sudo:$sudo $editor + Invoke-ExpressionEx -sudo:$sudo "& $editor" + while( -not (Get-Process -Name 'code' -ErrorAction SilentlyContinue) ) { + Start-Sleep -Seconds 1 + } + Write-Host -ForegroundColor DarkCyan "Please wait, initializing editor..." + Start-Sleep -Seconds $WaitForEditor + if( Test-Path variable:EditorWaitStopWatch ) { + $EditorWaitStopWatch.Restart() + } } }