github.com/richardwilkes/toolbox@v1.121.0/xio/term/terminal_unix.go (about)

     1  // Copyright (c) 2016-2024 by Richard A. Wilkes. All rights reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the Mozilla Public
     4  // License, version 2.0. If a copy of the MPL was not distributed with
     5  // this file, You can obtain one at http://mozilla.org/MPL/2.0/.
     6  //
     7  // This Source Code Form is "Incompatible With Secondary Licenses", as
     8  // defined by the Mozilla Public License, version 2.0.
     9  
    10  //go:build darwin || dragonfly || freebsd || linux || netbsd || openbsd
    11  
    12  package term
    13  
    14  import (
    15  	"io"
    16  	"os"
    17  	"syscall"
    18  	"unsafe"
    19  )
    20  
    21  // IsTerminal returns true if the writer's file descriptor is a terminal.
    22  func IsTerminal(f io.Writer) bool {
    23  	var termios syscall.Termios
    24  	switch v := f.(type) {
    25  	case *os.File:
    26  		_, _, errno := syscall.Syscall6(syscall.SYS_IOCTL, v.Fd(), ioctlReadTermios, uintptr(unsafe.Pointer(&termios)), 0, 0, 0)
    27  		return errno == 0
    28  	default:
    29  		return false
    30  	}
    31  }