From 050e42e42435d221e7ece0eecfcf1990e8804ae1 Mon Sep 17 00:00:00 2001 From: Ryan Fowler Date: Thu, 20 Mar 2025 07:21:55 -0700 Subject: [PATCH] Refine logic to check if file is a terminal --- go.mod | 1 - go.sum | 2 -- internal/core/term_unix.go | 10 ++++++++++ internal/core/term_unix_bsd.go | 7 +++++++ internal/core/term_unix_other.go | 7 +++++++ internal/core/term_windows.go | 11 +++++++++++ internal/core/vars.go | 6 ++---- 7 files changed, 37 insertions(+), 7 deletions(-) create mode 100644 internal/core/term_unix.go create mode 100644 internal/core/term_unix_bsd.go create mode 100644 internal/core/term_unix_other.go create mode 100644 internal/core/term_windows.go diff --git a/go.mod b/go.mod index 0c3da0a..b1769a3 100644 --- a/go.mod +++ b/go.mod @@ -5,5 +5,4 @@ go 1.24.1 require ( golang.org/x/image v0.25.0 golang.org/x/sys v0.31.0 - golang.org/x/term v0.30.0 ) diff --git a/go.sum b/go.sum index 113e948..cdba557 100644 --- a/go.sum +++ b/go.sum @@ -2,5 +2,3 @@ golang.org/x/image v0.25.0 h1:Y6uW6rH1y5y/LK1J8BPWZtr6yZ7hrsy6hFrXjgsc2fQ= golang.org/x/image v0.25.0/go.mod h1:tCAmOEGthTtkalusGp1g3xa2gke8J6c2N565dTyl9Rs= golang.org/x/sys v0.31.0 h1:ioabZlmFYtWhL+TRYpcnNlLwhyxaM9kWTDEmfnprqik= golang.org/x/sys v0.31.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k= -golang.org/x/term v0.30.0 h1:PQ39fJZ+mfadBm0y5WlL4vlM7Sx1Hgf13sMIY2+QS9Y= -golang.org/x/term v0.30.0/go.mod h1:NYYFdzHoI5wRh/h5tDMdMqCqPJZEuNqVR5xJLd/n67g= diff --git a/internal/core/term_unix.go b/internal/core/term_unix.go new file mode 100644 index 0000000..60504dd --- /dev/null +++ b/internal/core/term_unix.go @@ -0,0 +1,10 @@ +//go:build unix + +package core + +import "golang.org/x/sys/unix" + +func isTerminal(fd int) bool { + _, err := unix.IoctlGetTermios(fd, ioctlTermiosReq) + return err == nil +} diff --git a/internal/core/term_unix_bsd.go b/internal/core/term_unix_bsd.go new file mode 100644 index 0000000..3183163 --- /dev/null +++ b/internal/core/term_unix_bsd.go @@ -0,0 +1,7 @@ +//go:build darwin || dragonfly || freebsd || netbsd || openbsd + +package core + +import "golang.org/x/sys/unix" + +const ioctlTermiosReq = unix.TIOCGETA diff --git a/internal/core/term_unix_other.go b/internal/core/term_unix_other.go new file mode 100644 index 0000000..62adc40 --- /dev/null +++ b/internal/core/term_unix_other.go @@ -0,0 +1,7 @@ +//go:build aix || linux || solaris || zos + +package core + +import "golang.org/x/sys/unix" + +const ioctlTermiosReq = unix.TCGETS diff --git a/internal/core/term_windows.go b/internal/core/term_windows.go new file mode 100644 index 0000000..0193fe1 --- /dev/null +++ b/internal/core/term_windows.go @@ -0,0 +1,11 @@ +//go:build windows + +package core + +import "golang.org/x/sys/windows" + +func isTerminal(fd int) bool { + var st uint32 + err := windows.GetConsoleMode(windows.Handle(fd), &st) + return err == nil +} diff --git a/internal/core/vars.go b/internal/core/vars.go index 700fd78..66e2188 100644 --- a/internal/core/vars.go +++ b/internal/core/vars.go @@ -4,8 +4,6 @@ import ( "encoding/json" "os" "runtime/debug" - - "golang.org/x/term" ) var ( @@ -20,8 +18,8 @@ var ( func init() { // Determine if stderr and stdout are TTYs. - IsStderrTerm = term.IsTerminal(int(os.Stderr.Fd())) - IsStdoutTerm = term.IsTerminal(int(os.Stdout.Fd())) + IsStderrTerm = isTerminal(int(os.Stderr.Fd())) + IsStdoutTerm = isTerminal(int(os.Stdout.Fd())) // Set executable version and user-agent. Version = getVersion()