fix: tmux 脱离后任务不再被中断
bubbletea TUI 退出时检查任务是否仍在运行, 若仍在运行则回退到无 UI 阻塞等待模式, 而不是调用 AbortSilent() 杀死 coordinator。
This commit is contained in:
31
tui/app.go
31
tui/app.go
@@ -1,10 +1,13 @@
|
|||||||
package tui
|
package tui
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
"os/signal"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"syscall"
|
||||||
|
|
||||||
tea "github.com/charmbracelet/bubbletea"
|
tea "github.com/charmbracelet/bubbletea"
|
||||||
"github.com/voocel/ainovel-cli/app"
|
"github.com/voocel/ainovel-cli/app"
|
||||||
@@ -21,11 +24,37 @@ func Run(cfg app.Config, refs tools.References, prompts app.Prompts, styles map[
|
|||||||
rt.AskUser().SetHandler(bridge.handler)
|
rt.AskUser().SetHandler(bridge.handler)
|
||||||
restoreLog := redirectLogger(rt.Dir())
|
restoreLog := redirectLogger(rt.Dir())
|
||||||
defer restoreLog()
|
defer restoreLog()
|
||||||
defer rt.Close()
|
|
||||||
|
|
||||||
m := NewModel(rt, bridge)
|
m := NewModel(rt, bridge)
|
||||||
p := tea.NewProgram(m, tea.WithAltScreen(), tea.WithMouseCellMotion())
|
p := tea.NewProgram(m, tea.WithAltScreen(), tea.WithMouseCellMotion())
|
||||||
_, err = p.Run()
|
_, err = p.Run()
|
||||||
|
|
||||||
|
// bubbletea 退出后,检查任务是否仍在运行
|
||||||
|
snap := rt.Snapshot()
|
||||||
|
if snap.IsRunning {
|
||||||
|
// 任务仍在运行(可能是 tmux detach 导致 TUI 退出),
|
||||||
|
// 不中断任务,回退到无 UI 阻塞等待模式
|
||||||
|
fmt.Println("\n[TUI 已退出,任务仍在后台运行中...]")
|
||||||
|
fmt.Printf("[小说: %s | 阶段: %s | 进度: %d/%d 章]\n",
|
||||||
|
snap.NovelName, snap.Phase, snap.CompletedCount, snap.TotalChapters)
|
||||||
|
fmt.Println("[等待任务完成,按 Ctrl+C 强制中断]")
|
||||||
|
|
||||||
|
// 监听中断信号
|
||||||
|
sigCh := make(chan os.Signal, 1)
|
||||||
|
signal.Notify(sigCh, syscall.SIGINT, syscall.SIGTERM)
|
||||||
|
|
||||||
|
select {
|
||||||
|
case <-rt.Done():
|
||||||
|
fmt.Println("\n[任务已完成!]")
|
||||||
|
case <-sigCh:
|
||||||
|
fmt.Println("\n[收到中断信号,正在停止...]")
|
||||||
|
rt.Close()
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// 任务未在运行,正常关闭
|
||||||
|
rt.Close()
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user