feat(menu): 队列处理完不再自动关窗,按任意键(Esc/Enter)关闭
- 队列清空后显示汇总并驻留窗口:按任意键关闭,2 分钟无操作才自动关闭 - 驻留期间若有新任务入队,同一个窗口会继续处理,不必重新点菜单 - 汇总把成功/取消/失败分三档计数
This commit is contained in:
@@ -179,54 +179,78 @@ function Invoke-Worker {
|
|||||||
try { $Host.UI.RawUI.WindowTitle = 'SceneMarshal 队列' } catch { }
|
try { $Host.UI.RawUI.WindowTitle = 'SceneMarshal 队列' } catch { }
|
||||||
Write-WorkerLog ("worker 启动(stdin重定向={0} stdout重定向={1})" -f `
|
Write-WorkerLog ("worker 启动(stdin重定向={0} stdout重定向={1})" -f `
|
||||||
[Console]::IsInputRedirected, [Console]::IsOutputRedirected)
|
[Console]::IsInputRedirected, [Console]::IsOutputRedirected)
|
||||||
Write-Host '📋 SceneMarshal 队列已启动(处理完即退出)' -ForegroundColor Cyan
|
Write-Host '📋 SceneMarshal 队列已启动' -ForegroundColor Cyan
|
||||||
Write-Host ''
|
Write-Host ''
|
||||||
|
|
||||||
$done = 0
|
$done = 0
|
||||||
$failed = 0
|
$failed = 0
|
||||||
$cancelled = 0
|
$cancelled = 0
|
||||||
|
$summaryShown = $false
|
||||||
|
$closeDeadline = $null
|
||||||
|
|
||||||
while ($true) {
|
while ($true) {
|
||||||
$item = Take-QueueItem
|
$item = Take-QueueItem
|
||||||
if ($null -eq $item) { break } # 队列空就退出,不再等待新任务
|
|
||||||
|
|
||||||
$name = Split-Path -Leaf $item.path
|
if ($null -ne $item) {
|
||||||
Write-Host ('─── [{0}] {1}' -f $item.action, $name) -ForegroundColor Yellow
|
$summaryShown = $false
|
||||||
Write-WorkerLog ("开始处理 {0} :: {1}" -f $item.action, $item.path)
|
$closeDeadline = $null
|
||||||
$started = Get-Date
|
$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 {
|
try {
|
||||||
$code = Invoke-Action $item.action $item.path
|
if ($Host.UI.RawUI.KeyAvailable) {
|
||||||
|
$null = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown')
|
||||||
|
$pressed = $true
|
||||||
|
}
|
||||||
}
|
}
|
||||||
catch {
|
catch { }
|
||||||
Write-Host (" ❌ 异常: {0}" -f $_.Exception.Message) -ForegroundColor Red
|
if ($pressed) { break }
|
||||||
$code = 1
|
if ($null -ne $closeDeadline -and (Get-Date) -ge $closeDeadline) { break }
|
||||||
}
|
Start-Sleep -Milliseconds 150
|
||||||
$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 ''
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$total = $done + $cancelled + $failed
|
Write-WorkerLog 'worker 退出'
|
||||||
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)
|
|
||||||
$workerLock.Close()
|
$workerLock.Close()
|
||||||
if ($total -gt 0) { Start-Sleep -Seconds 2 } # 留两秒给汇总留在屏幕上
|
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user