diff --git a/termenv_unix.go b/termenv_unix.go index 2aebdfa..35211da 100644 --- a/termenv_unix.go +++ b/termenv_unix.go @@ -18,6 +18,15 @@ const ( OSCTimeout = 5 * time.Second ) +func isForeground(fd int) bool { + pgrp, err := unix.IoctlGetInt(fd, unix.TIOCGPGRP) + if err != nil { + return false + } + + return pgrp == unix.Getpgrp() +} + func colorProfile() Profile { term := os.Getenv("TERM") colorTerm := os.Getenv("COLORTERM") @@ -215,6 +224,11 @@ func termStatusReport(sequence int) (string, error) { return "", ErrStatusReport } + // if in background, we can't control the terminal + if !isForeground(unix.Stdout) { + return "", ErrStatusReport + } + t, err := unix.IoctlGetTermios(unix.Stdout, tcgetattr) if err != nil { return "", ErrStatusReport