go-isatty - TTY 环境判断

go-isatty

Godoc Reference Codecov Coverage Status Go Report Card

isatty for golang

Usage

package main

import (
	"fmt"
	"github.com/mattn/go-isatty"
	"os"
)

func main() {
	if isatty.IsTerminal(os.Stdout.Fd()) {
		fmt.Println("Is Terminal")
	} else if isatty.IsCygwinTerminal(os.Stdout.Fd()) {
		fmt.Println("Is Cygwin/MSYS2 Terminal")
	} else {
		fmt.Println("Is Not Terminal")
	}
}

Installation

$ go get github.com/mattn/go-isatty

License

MIT

Author

Yasuhiro Matsumoto (a.k.a mattn)

Thanks

Owner
mattn
Long-time Golang user&contributor, Google Dev Expert for Go, and author of many Go tools, Vim plugin author. Windows hacker C#/Java/C/C++
mattn
Comments
  • Termux Android cannot find

    Termux Android cannot find "golang.org/x/sys/unix"

    On Termux Android, the lib depends to go-issaty is failing. It cannot find golang.org/x/sys/unix. I might relate to https://github.com/mattn/go-isatty/pull/28?

  • Enable go modules

    Enable go modules

    Go modules are becoming a de-facto standard in the Go community and adding go.mod files with metadata helps projects which may consume this repository as a dependency in deciding how to pin transitive dependencies and how to resolve conflicts.

    This is a result of the following commands:

    go mod init
    go get ./...
    go mod tidy
    

    in a clean Go 1.11.5 environment.

  • Interactive mode or not

    Interactive mode or not

    This is a feature request. Is it possible to determine if the go code is running in interactive mode or not? So that we can decide if we need to use the options / flag or interactive with end user for options.

  • Update go.sum after golang.org/x/sys update

    Update go.sum after golang.org/x/sys update

    PR #66 updated the golang.org/x/sys dependency in go.mod but didn't update go.sum. This leads to the following error on running go.test.sh:

    go: golang.org/x/[email protected]: missing go.sum entry; to add it:
    	go mod download golang.org/x/sys
    go: golang.org/x/[email protected]: missing go.sum entry; to add it:
    	go mod download golang.org/x/sys
    

    Fix this by running go mod download golang.org/x/sys && go mod tidy to update and clean up go.sum.

  • Terminal is not detected when redirecting the output from the shell

    Terminal is not detected when redirecting the output from the shell

    When executing a golang app while redirecting the output from the shell (./app > /tmp/my.log), IsTerminal() returns false even though the app is run from a terminal. Is there any reason for that?

  • IntelliJ console

    IntelliJ console

    Although the windows IsTerminal correctly detected when it is in a Windows terminal, it doesn't detect if it is in an IntelliJ terminal. Would this be possible to add? I honestly have no idea :(

    Thanks!

  • undefined: unix.IoctlGetTermios

    undefined: unix.IoctlGetTermios

    I'm trying to build a project that uses "golang:1.13.10-alpine" docker image inside of Amazon-kubernetes enviroment, but there is an error:

    vendor/github.com/mattn/go-isatty/isatty_tcgets.go:10:12: undefined: unix.IoctlGetTermios

    Any thoughts?

  • Update x/sys to support Risc-V

    Update x/sys to support Risc-V

    Updating projects using libraries that were recently updated to support Risc-V architecture.

    Go upstream work is tracked on: golang/go#27532

    Risc-V software support tracker on https://github.com/carlosedp/riscv-bringup

    Signed-off-by: CarlosEDP [email protected]

  • Missing method

    Missing method

    After you have added separate source for android builds, gin is failing because cannot find IsCygwinTerminal for android build.

    ../../gin-gonic/gin/logger.go:201:67: undefined: isatty.IsCygwinTerminal
    
  • use golang.org/x/sys/unix for IsTerminal on Linux

    use golang.org/x/sys/unix for IsTerminal on Linux

    Use the unix.IoctlGetTermios to implement IsTerminal on Linux. This allows to drop the special case for ppc64x and makes go-isatty support all GOARCHes that golang.org/x/sys/unix supports for Linux. Also this no longer requires to use ithe frozen syscall package.

  • please tag and version this project

    please tag and version this project

    Hello,

    Can you please tag and version this project?

    I am the Debian Maintainer for go-isatty and versioning would help Debian keep up with development.

  • Compatibility with newer golang.org/x/sys/unix

    Compatibility with newer golang.org/x/sys/unix

    When using this package (as a dependency of getsentry 0.15) with golang.org/x/sys/unix a golangci-lint returns following error

    [/go/pkg/mod/github.com/mattn/[email protected]/isatty_tcgets.go:7:8: could not import golang.org/x/sys/unix (/go/pkg/mod/golang.org/x/[email protected]/unix/syscall.go:83:23: Slice not declared by package unsafe)
    

    Go 1.17

  • Check that TCGETS is indeed the way to go

    Check that TCGETS is indeed the way to go

    Please check if TCGETS is appropriate here: https://github.com/mattn/go-isatty/blob/13e91bf4058fb93d5629deb7b2e3763ec8f4fdf8/isatty_tcgets.go#L11 See https://git.musl-libc.org/cgit/musl/patch/?id=2de85a985654d2c944931267645d9a0686242dfe for details.

  • Adjust build tags to allow building on tinygo; for #73.

    Adjust build tags to allow building on tinygo; for #73.

    Passes "tinygo test" on macos and linux. Passes cross test to windows from macos, i.e. "GOOS=windows tinygo test -c; wine64 go-isatty.test".

    Had to adjust the build tags on isatty_others.go to avoid multiple definition link failure on

    $ GOOS=windows tinygo test
    
  • Incompatible with tinygo?

    Incompatible with tinygo?

    Evidently zerolog 0.27 added colorization on windows by pulling in mattn/go-colorable, which in turn pulls in go-isatty. Great! Except that now zerolog can't be used with tinygo (on macos, at least, and possibly other operating systems).

    At least part of the problem can be traced to go-isatty:

    $ tinygo test
    # golang.org/x/sys/unix
    ../../go/pkg/mod/golang.org/x/[email protected]/unix/syscall_unix.go:466:17: Exec not declared by package syscall
    FAIL
    
  • IsCygwinTerminal doesn't work on Cygwin

    IsCygwinTerminal doesn't work on Cygwin

    👋🏻

    I have the following example, which when run on a Windows 10 VM with Cygwin configured it doesn't actually report anything other than "Is Terminal", when I would expect both that and "Is Cygwin/MSYS2 Terminal" to be displayed.

    package main
    
    import (
    	"fmt"
    	"os"
    
    	"github.com/mattn/go-isatty"
    )
    
    func main() {
    	if isatty.IsTerminal(os.Stdout.Fd()) {
    		fmt.Println("Is Terminal")
    	}
    	if isatty.IsCygwinTerminal(os.Stdout.Fd()) {
    		fmt.Println("Is Cygwin/MSYS2 Terminal")
    	}
    }
    

    Let me know if you need any more information.

    Thanks!

go-isatty - isatty for golang.

go-isatty isatty for golang Usage package main import ( "fmt" "github.com/mattn/go-isatty" "os" ) func main() { if isatty.IsTerminal(os.Stdout.F

Jan 7, 2023
Confetti in your TTY

Confetti in your TTY

Dec 26, 2022