Skip to content

Commit

Permalink
fix: allow to termStatusReport on a PTY
Browse files Browse the repository at this point in the history
A pty is not in foreground, but it can do the IOCTL ops we need.

My understanding might be wrong, but I think that if the term cannot do
the TCGETS it will fail the same way it would when checking if its in
foreground, so maybe that check is even needed?

refs charmbracelet/wish#262
  • Loading branch information
caarlos0 committed May 2, 2024
1 parent 3457830 commit f46a45a
Show file tree
Hide file tree
Showing 4 changed files with 2 additions and 40 deletions.
6 changes: 2 additions & 4 deletions output.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@ import (
"sync"
)

var (
// output is the default global output.
output = NewOutput(os.Stdout)
)
// output is the default global output.
var output = NewOutput(os.Stdout)

// File represents a file descriptor.
//
Expand Down
13 changes: 0 additions & 13 deletions termenv_posix.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,3 @@
// +build darwin dragonfly freebsd linux netbsd openbsd zos

package termenv

import (
"golang.org/x/sys/unix"
)

func isForeground(fd int) bool {
pgrp, err := unix.IoctlGetInt(fd, unix.TIOCGPGRP)
if err != nil {
return false
}

return pgrp == unix.Getpgrp()
}
18 changes: 0 additions & 18 deletions termenv_solaris.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,3 @@
// +build solaris illumos

package termenv

import (
"golang.org/x/sys/unix"
)

func isForeground(fd int) bool {
pgrp, err := unix.IoctlGetInt(fd, unix.TIOCGPGRP)
if err != nil {
return false
}

g, err := unix.Getpgrp()
if err != nil {
return false
}

return pgrp == g
}
5 changes: 0 additions & 5 deletions termenv_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,11 +238,6 @@ func (o Output) termStatusReport(sequence int) (string, error) {

if !o.unsafe {
fd := int(tty.Fd())
// if in background, we can't control the terminal
if !isForeground(fd) {
return "", ErrStatusReport
}

t, err := unix.IoctlGetTermios(fd, tcgetattr)
if err != nil {
return "", fmt.Errorf("%s: %s", ErrStatusReport, err)
Expand Down

0 comments on commit f46a45a

Please sign in to comment.