diff --git a/scripts/explorer-media-rename.ps1 b/scripts/explorer-media-rename.ps1 index 4ce9992..70d6012 100644 --- a/scripts/explorer-media-rename.ps1 +++ b/scripts/explorer-media-rename.ps1 @@ -179,54 +179,78 @@ function Invoke-Worker { try { $Host.UI.RawUI.WindowTitle = 'SceneMarshal 队列' } catch { } Write-WorkerLog ("worker 启动(stdin重定向={0} stdout重定向={1})" -f ` [Console]::IsInputRedirected, [Console]::IsOutputRedirected) - Write-Host '📋 SceneMarshal 队列已启动(处理完即退出)' -ForegroundColor Cyan + Write-Host '📋 SceneMarshal 队列已启动' -ForegroundColor Cyan Write-Host '' $done = 0 $failed = 0 $cancelled = 0 + $summaryShown = $false + $closeDeadline = $null while ($true) { $item = Take-QueueItem - if ($null -eq $item) { break } # 队列空就退出,不再等待新任务 - $name = Split-Path -Leaf $item.path - Write-Host ('─── [{0}] {1}' -f $item.action, $name) -ForegroundColor Yellow - Write-WorkerLog ("开始处理 {0} :: {1}" -f $item.action, $item.path) - $started = Get-Date + if ($null -ne $item) { + $summaryShown = $false + $closeDeadline = $null + $name = Split-Path -Leaf $item.path + Write-Host ('─── [{0}] {1}' -f $item.action, $name) -ForegroundColor Yellow + Write-WorkerLog ("开始处理 {0} :: {1}" -f $item.action, $item.path) + $started = Get-Date + try { + $code = Invoke-Action $item.action $item.path + } + catch { + Write-Host (" ❌ 异常: {0}" -f $_.Exception.Message) -ForegroundColor Red + $code = 1 + } + $cost = [int]((Get-Date) - $started).TotalSeconds + if ($code -eq 0) { + $done++ + Write-Host (" ✅ 完成,用时 {0}s" -f $cost) -ForegroundColor Green + Write-WorkerLog ("完成 {0}({1}s)" -f $item.path, $cost) + } + elseif ($code -eq 2) { + # 退出码 2 约定为「用户主动取消」,不算失败 + $cancelled++ + Write-Host (" ⏭ 已取消(未改动任何文件),用时 {0}s" -f $cost) -ForegroundColor DarkYellow + Write-WorkerLog ("已取消 {0}" -f $item.path) + } + else { + $failed++ + Write-Host (" ⚠️ 失败(退出码 {0}),用时 {1}s" -f $code, $cost) -ForegroundColor Red + Write-WorkerLog ("失败 {0}(code={1}, {2}s)" -f $item.path, $code, $cost) + } + Write-Host '' + continue + } + + # 队列已空:只显示一次汇总,然后等按键关闭窗口 + if (-not $summaryShown) { + Write-Host ('📊 本次共处理 {0} 项:成功 {1},取消 {2},失败 {3}' -f ` + ($done + $cancelled + $failed), $done, $cancelled, $failed) -ForegroundColor Cyan + Write-Host ' 按任意键(Esc / Enter)关闭窗口;期间有新任务会接着处理(2 分钟无操作自动关闭)' -ForegroundColor DarkGray + Write-WorkerLog ("队列处理完毕:成功 {0},取消 {1},失败 {2}" -f $done, $cancelled, $failed) + $summaryShown = $true + $closeDeadline = (Get-Date).AddSeconds(120) + } + + $pressed = $false try { - $code = Invoke-Action $item.action $item.path + if ($Host.UI.RawUI.KeyAvailable) { + $null = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown') + $pressed = $true + } } - catch { - Write-Host (" ❌ 异常: {0}" -f $_.Exception.Message) -ForegroundColor Red - $code = 1 - } - $cost = [int]((Get-Date) - $started).TotalSeconds - if ($code -eq 0) { - $done++ - Write-Host (" ✅ 完成,用时 {0}s" -f $cost) -ForegroundColor Green - Write-WorkerLog ("完成 {0}({1}s)" -f $item.path, $cost) - } - elseif ($code -eq 2) { - # 退出码 2 约定为「用户主动取消」,不算失败 - $cancelled++ - Write-Host (" ⏭ 已取消(未改动任何文件),用时 {0}s" -f $cost) -ForegroundColor DarkYellow - Write-WorkerLog ("已取消 {0}" -f $item.path) - } - else { - $failed++ - Write-Host (" ⚠️ 失败(退出码 {0}),用时 {1}s" -f $code, $cost) -ForegroundColor Red - Write-WorkerLog ("失败 {0}(code={1}, {2}s)" -f $item.path, $code, $cost) - } - Write-Host '' + catch { } + if ($pressed) { break } + if ($null -ne $closeDeadline -and (Get-Date) -ge $closeDeadline) { break } + Start-Sleep -Milliseconds 150 } - $total = $done + $cancelled + $failed - Write-Host ('📊 本次共处理 {0} 项:成功 {1},取消 {2},失败 {3}' -f $total, $done, $cancelled, $failed) -ForegroundColor Cyan - Write-Host ' 队列已空,窗口即将关闭(详细记录见 worker.log)' -ForegroundColor DarkGray - Write-WorkerLog ("worker 退出:成功 {0},取消 {1},失败 {2}" -f $done, $cancelled, $failed) + Write-WorkerLog 'worker 退出' $workerLock.Close() - if ($total -gt 0) { Start-Sleep -Seconds 2 } # 留两秒给汇总留在屏幕上 return 0 }