Strip ANSI escapes from managed ComfyUI output in the log; spawn child with NO_COLOR/TERM=dumb

This commit is contained in:
mram
2026-09-21 22:29:41 +02:00
parent 5b752d5637
commit 45bc9fe27c
2 changed files with 30 additions and 3 deletions
+16 -3
View File
@@ -13,6 +13,7 @@ import (
"os"
"os/exec"
"path/filepath"
"regexp"
"runtime"
"strings"
"sync"
@@ -185,6 +186,9 @@ func (p *Process) EnsureRunning() error {
p.external = false
cmd := exec.Command(p.argv[0], p.argv[1:]...)
cmd.Dir = p.dir
// Ask the child not to colorize (ComfyUI ignores this and colors
// anyway, so pipeLog also strips escape sequences).
cmd.Env = append(os.Environ(), "NO_COLOR=1", "TERM=dumb")
stdout, err := cmd.StdoutPipe()
if err != nil {
return err
@@ -278,7 +282,9 @@ func (p *Process) WatchIdle(ctx context.Context, idleTimeout time.Duration, gpuI
}
// pipeLog forwards one child output stream to the log at INFO, line by
// line, prefixed with the process name.
// line, prefixed with the process name. ANSI escape sequences are
// stripped: ComfyUI colorizes unconditionally, and the escapes only
// render as garbage in a log file.
func (p *Process) pipeLog(r io.Reader) {
buf := make([]byte, 4096)
var line string
@@ -290,18 +296,25 @@ func (p *Process) pipeLog(r io.Reader) {
if i < 0 {
break
}
p.log.Info(p.name + ": " + strings.TrimRight(line[:i], "\r"))
p.log.Info(p.name + ": " + stripANSI(strings.TrimRight(line[:i], "\r")))
line = line[i+1:]
}
if err != nil {
if strings.TrimSpace(line) != "" {
p.log.Info(p.name + ": " + line)
p.log.Info(p.name + ": " + stripANSI(line))
}
return
}
}
}
// ansiPattern matches CSI escape sequences (colors, cursor moves, …).
var ansiPattern = regexp.MustCompile("\x1b\\[[0-9;?]*[a-zA-Z]")
func stripANSI(s string) string {
return ansiPattern.ReplaceAllString(s, "")
}
// stopTree kills cmd's process, including its children on Windows (python
// launchers tend to spawn some). The Wait goroutine reaps it.
func stopTree(cmd *exec.Cmd) {