├── .github ├── FUNDING.yml └── workflows │ └── test.yml ├── LICENSE ├── README.md ├── doc.go ├── example_test.go ├── go.mod ├── go.sum ├── go.test.sh ├── isatty_bsd.go ├── isatty_others.go ├── isatty_others_test.go ├── isatty_plan9.go ├── isatty_solaris.go ├── isatty_tcgets.go ├── isatty_windows.go └── isatty_windows_test.go /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | # These are supported funding model platforms 2 | 3 | github: # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2] 4 | patreon: mattn # Replace with a single Patreon username 5 | open_collective: # Replace with a single Open Collective username 6 | ko_fi: # Replace with a single Ko-fi username 7 | tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel 8 | community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry 9 | liberapay: # Replace with a single Liberapay username 10 | issuehunt: # Replace with a single IssueHunt username 11 | otechie: # Replace with a single Otechie username 12 | custom: # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2'] 13 | -------------------------------------------------------------------------------- /.github/workflows/test.yml: -------------------------------------------------------------------------------- 1 | name: test 2 | on: 3 | push: 4 | pull_request: 5 | 6 | jobs: 7 | test: 8 | runs-on: ${{ matrix.os }} 9 | strategy: 10 | fail-fast: false 11 | matrix: 12 | os: 13 | - ubuntu-latest 14 | - macos-latest 15 | - windows-latest 16 | go: 17 | - '1.20' 18 | - '1.19' 19 | - '1.18' 20 | - '1.17' 21 | steps: 22 | - uses: actions/setup-go@v2 23 | with: 24 | go-version: ${{ matrix.go }} 25 | - uses: actions/checkout@v2 26 | - name: test 27 | shell: bash 28 | run: | 29 | ./go.test.sh 30 | - name: upload coverage report 31 | uses: codecov/codecov-action@v2 32 | with: 33 | env_vars: OS,GO 34 | env: 35 | OS: ${{ matrix.os }} 36 | GO: ${{ matrix.go }} 37 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) Yasuhiro MATSUMOTO 2 | 3 | MIT License (Expat) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # go-isatty 2 | 3 | [![Godoc Reference](https://godoc.org/github.com/mattn/go-isatty?status.svg)](http://godoc.org/github.com/mattn/go-isatty) 4 | [![Codecov](https://codecov.io/gh/mattn/go-isatty/branch/master/graph/badge.svg)](https://codecov.io/gh/mattn/go-isatty) 5 | [![Coverage Status](https://coveralls.io/repos/github/mattn/go-isatty/badge.svg?branch=master)](https://coveralls.io/github/mattn/go-isatty?branch=master) 6 | [![Go Report Card](https://goreportcard.com/badge/mattn/go-isatty)](https://goreportcard.com/report/mattn/go-isatty) 7 | 8 | isatty for golang 9 | 10 | ## Usage 11 | 12 | ```go 13 | package main 14 | 15 | import ( 16 | "fmt" 17 | "github.com/mattn/go-isatty" 18 | "os" 19 | ) 20 | 21 | func main() { 22 | if isatty.IsTerminal(os.Stdout.Fd()) { 23 | fmt.Println("Is Terminal") 24 | } else if isatty.IsCygwinTerminal(os.Stdout.Fd()) { 25 | fmt.Println("Is Cygwin/MSYS2 Terminal") 26 | } else { 27 | fmt.Println("Is Not Terminal") 28 | } 29 | } 30 | ``` 31 | 32 | ## Installation 33 | 34 | ``` 35 | $ go get github.com/mattn/go-isatty 36 | ``` 37 | 38 | ## License 39 | 40 | MIT 41 | 42 | ## Author 43 | 44 | Yasuhiro Matsumoto (a.k.a mattn) 45 | 46 | ## Thanks 47 | 48 | * k-takata: base idea for IsCygwinTerminal 49 | 50 | https://github.com/k-takata/go-iscygpty 51 | -------------------------------------------------------------------------------- /doc.go: -------------------------------------------------------------------------------- 1 | // Package isatty implements interface to isatty 2 | package isatty 3 | -------------------------------------------------------------------------------- /example_test.go: -------------------------------------------------------------------------------- 1 | package isatty_test 2 | 3 | import ( 4 | "fmt" 5 | "os" 6 | 7 | "github.com/mattn/go-isatty" 8 | ) 9 | 10 | func Example() { 11 | if isatty.IsTerminal(os.Stdout.Fd()) { 12 | fmt.Println("Is Terminal") 13 | } else if isatty.IsCygwinTerminal(os.Stdout.Fd()) { 14 | fmt.Println("Is Cygwin/MSYS2 Terminal") 15 | } else { 16 | fmt.Println("Is Not Terminal") 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/mattn/go-isatty 2 | 3 | go 1.15 4 | 5 | require golang.org/x/sys v0.6.0 6 | -------------------------------------------------------------------------------- /go.sum: -------------------------------------------------------------------------------- 1 | golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ= 2 | golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 3 | -------------------------------------------------------------------------------- /go.test.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | set -e 4 | echo "" > coverage.txt 5 | 6 | for d in $(go list ./... | grep -v vendor); do 7 | go test -race -coverprofile=profile.out -covermode=atomic "$d" 8 | if [ -f profile.out ]; then 9 | cat profile.out >> coverage.txt 10 | rm profile.out 11 | fi 12 | done 13 | -------------------------------------------------------------------------------- /isatty_bsd.go: -------------------------------------------------------------------------------- 1 | //go:build (darwin || freebsd || openbsd || netbsd || dragonfly || hurd) && !appengine && !tinygo 2 | // +build darwin freebsd openbsd netbsd dragonfly hurd 3 | // +build !appengine 4 | // +build !tinygo 5 | 6 | package isatty 7 | 8 | import "golang.org/x/sys/unix" 9 | 10 | // IsTerminal return true if the file descriptor is terminal. 11 | func IsTerminal(fd uintptr) bool { 12 | _, err := unix.IoctlGetTermios(int(fd), unix.TIOCGETA) 13 | return err == nil 14 | } 15 | 16 | // IsCygwinTerminal return true if the file descriptor is a cygwin or msys2 17 | // terminal. This is also always false on this environment. 18 | func IsCygwinTerminal(fd uintptr) bool { 19 | return false 20 | } 21 | -------------------------------------------------------------------------------- /isatty_others.go: -------------------------------------------------------------------------------- 1 | //go:build (appengine || js || nacl || tinygo || wasm) && !windows 2 | // +build appengine js nacl tinygo wasm 3 | // +build !windows 4 | 5 | package isatty 6 | 7 | // IsTerminal returns true if the file descriptor is terminal which 8 | // is always false on js and appengine classic which is a sandboxed PaaS. 9 | func IsTerminal(fd uintptr) bool { 10 | return false 11 | } 12 | 13 | // IsCygwinTerminal() return true if the file descriptor is a cygwin or msys2 14 | // terminal. This is also always false on this environment. 15 | func IsCygwinTerminal(fd uintptr) bool { 16 | return false 17 | } 18 | -------------------------------------------------------------------------------- /isatty_others_test.go: -------------------------------------------------------------------------------- 1 | //go:build !windows 2 | // +build !windows 3 | 4 | package isatty 5 | 6 | import ( 7 | "os" 8 | "testing" 9 | ) 10 | 11 | func TestTerminal(t *testing.T) { 12 | // test for non-panic 13 | t.Log("os.Stdout:", IsTerminal(os.Stdout.Fd())) 14 | } 15 | 16 | func TestCygwinPipeName(t *testing.T) { 17 | if IsCygwinTerminal(os.Stdout.Fd()) { 18 | t.Fatal("should be false always") 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /isatty_plan9.go: -------------------------------------------------------------------------------- 1 | //go:build plan9 2 | // +build plan9 3 | 4 | package isatty 5 | 6 | import ( 7 | "syscall" 8 | ) 9 | 10 | // IsTerminal returns true if the given file descriptor is a terminal. 11 | func IsTerminal(fd uintptr) bool { 12 | path, err := syscall.Fd2path(int(fd)) 13 | if err != nil { 14 | return false 15 | } 16 | return path == "/dev/cons" || path == "/mnt/term/dev/cons" 17 | } 18 | 19 | // IsCygwinTerminal return true if the file descriptor is a cygwin or msys2 20 | // terminal. This is also always false on this environment. 21 | func IsCygwinTerminal(fd uintptr) bool { 22 | return false 23 | } 24 | -------------------------------------------------------------------------------- /isatty_solaris.go: -------------------------------------------------------------------------------- 1 | //go:build solaris && !appengine 2 | // +build solaris,!appengine 3 | 4 | package isatty 5 | 6 | import ( 7 | "golang.org/x/sys/unix" 8 | ) 9 | 10 | // IsTerminal returns true if the given file descriptor is a terminal. 11 | // see: https://src.illumos.org/source/xref/illumos-gate/usr/src/lib/libc/port/gen/isatty.c 12 | func IsTerminal(fd uintptr) bool { 13 | _, err := unix.IoctlGetTermio(int(fd), unix.TCGETA) 14 | return err == nil 15 | } 16 | 17 | // IsCygwinTerminal return true if the file descriptor is a cygwin or msys2 18 | // terminal. This is also always false on this environment. 19 | func IsCygwinTerminal(fd uintptr) bool { 20 | return false 21 | } 22 | -------------------------------------------------------------------------------- /isatty_tcgets.go: -------------------------------------------------------------------------------- 1 | //go:build (linux || aix || zos) && !appengine && !tinygo 2 | // +build linux aix zos 3 | // +build !appengine 4 | // +build !tinygo 5 | 6 | package isatty 7 | 8 | import "golang.org/x/sys/unix" 9 | 10 | // IsTerminal return true if the file descriptor is terminal. 11 | func IsTerminal(fd uintptr) bool { 12 | _, err := unix.IoctlGetTermios(int(fd), unix.TCGETS) 13 | return err == nil 14 | } 15 | 16 | // IsCygwinTerminal return true if the file descriptor is a cygwin or msys2 17 | // terminal. This is also always false on this environment. 18 | func IsCygwinTerminal(fd uintptr) bool { 19 | return false 20 | } 21 | -------------------------------------------------------------------------------- /isatty_windows.go: -------------------------------------------------------------------------------- 1 | //go:build windows && !appengine 2 | // +build windows,!appengine 3 | 4 | package isatty 5 | 6 | import ( 7 | "errors" 8 | "strings" 9 | "syscall" 10 | "unicode/utf16" 11 | "unsafe" 12 | ) 13 | 14 | const ( 15 | objectNameInfo uintptr = 1 16 | fileNameInfo = 2 17 | fileTypePipe = 3 18 | ) 19 | 20 | var ( 21 | kernel32 = syscall.NewLazyDLL("kernel32.dll") 22 | ntdll = syscall.NewLazyDLL("ntdll.dll") 23 | procGetConsoleMode = kernel32.NewProc("GetConsoleMode") 24 | procGetFileInformationByHandleEx = kernel32.NewProc("GetFileInformationByHandleEx") 25 | procGetFileType = kernel32.NewProc("GetFileType") 26 | procNtQueryObject = ntdll.NewProc("NtQueryObject") 27 | ) 28 | 29 | func init() { 30 | // Check if GetFileInformationByHandleEx is available. 31 | if procGetFileInformationByHandleEx.Find() != nil { 32 | procGetFileInformationByHandleEx = nil 33 | } 34 | } 35 | 36 | // IsTerminal return true if the file descriptor is terminal. 37 | func IsTerminal(fd uintptr) bool { 38 | var st uint32 39 | r, _, e := syscall.Syscall(procGetConsoleMode.Addr(), 2, fd, uintptr(unsafe.Pointer(&st)), 0) 40 | return r != 0 && e == 0 41 | } 42 | 43 | // Check pipe name is used for cygwin/msys2 pty. 44 | // Cygwin/MSYS2 PTY has a name like: 45 | // \{cygwin,msys}-XXXXXXXXXXXXXXXX-ptyN-{from,to}-master 46 | func isCygwinPipeName(name string) bool { 47 | token := strings.Split(name, "-") 48 | if len(token) < 5 { 49 | return false 50 | } 51 | 52 | if token[0] != `\msys` && 53 | token[0] != `\cygwin` && 54 | token[0] != `\Device\NamedPipe\msys` && 55 | token[0] != `\Device\NamedPipe\cygwin` { 56 | return false 57 | } 58 | 59 | if token[1] == "" { 60 | return false 61 | } 62 | 63 | if !strings.HasPrefix(token[2], "pty") { 64 | return false 65 | } 66 | 67 | if token[3] != `from` && token[3] != `to` { 68 | return false 69 | } 70 | 71 | if token[4] != "master" { 72 | return false 73 | } 74 | 75 | return true 76 | } 77 | 78 | // getFileNameByHandle use the undocomented ntdll NtQueryObject to get file full name from file handler 79 | // since GetFileInformationByHandleEx is not available under windows Vista and still some old fashion 80 | // guys are using Windows XP, this is a workaround for those guys, it will also work on system from 81 | // Windows vista to 10 82 | // see https://stackoverflow.com/a/18792477 for details 83 | func getFileNameByHandle(fd uintptr) (string, error) { 84 | if procNtQueryObject == nil { 85 | return "", errors.New("ntdll.dll: NtQueryObject not supported") 86 | } 87 | 88 | var buf [4 + syscall.MAX_PATH]uint16 89 | var result int 90 | r, _, e := syscall.Syscall6(procNtQueryObject.Addr(), 5, 91 | fd, objectNameInfo, uintptr(unsafe.Pointer(&buf)), uintptr(2*len(buf)), uintptr(unsafe.Pointer(&result)), 0) 92 | if r != 0 { 93 | return "", e 94 | } 95 | return string(utf16.Decode(buf[4 : 4+buf[0]/2])), nil 96 | } 97 | 98 | // IsCygwinTerminal() return true if the file descriptor is a cygwin or msys2 99 | // terminal. 100 | func IsCygwinTerminal(fd uintptr) bool { 101 | if procGetFileInformationByHandleEx == nil { 102 | name, err := getFileNameByHandle(fd) 103 | if err != nil { 104 | return false 105 | } 106 | return isCygwinPipeName(name) 107 | } 108 | 109 | // Cygwin/msys's pty is a pipe. 110 | ft, _, e := syscall.Syscall(procGetFileType.Addr(), 1, fd, 0, 0) 111 | if ft != fileTypePipe || e != 0 { 112 | return false 113 | } 114 | 115 | var buf [2 + syscall.MAX_PATH]uint16 116 | r, _, e := syscall.Syscall6(procGetFileInformationByHandleEx.Addr(), 117 | 4, fd, fileNameInfo, uintptr(unsafe.Pointer(&buf)), 118 | uintptr(len(buf)*2), 0, 0) 119 | if r == 0 || e != 0 { 120 | return false 121 | } 122 | 123 | l := *(*uint32)(unsafe.Pointer(&buf)) 124 | return isCygwinPipeName(string(utf16.Decode(buf[2 : 2+l/2]))) 125 | } 126 | -------------------------------------------------------------------------------- /isatty_windows_test.go: -------------------------------------------------------------------------------- 1 | //go:build windows 2 | // +build windows 3 | 4 | package isatty 5 | 6 | import ( 7 | "testing" 8 | ) 9 | 10 | func TestCygwinPipeName(t *testing.T) { 11 | tests := []struct { 12 | name string 13 | result bool 14 | }{ 15 | {``, false}, 16 | {`\msys-`, false}, 17 | {`\cygwin-----`, false}, 18 | {`\msys-x-PTY5-pty1-from-master`, false}, 19 | {`\cygwin-x-PTY5-from-master`, false}, 20 | {`\cygwin-x-pty2-from-toaster`, false}, 21 | {`\cygwin--pty2-from-master`, false}, 22 | {`\\cygwin-x-pty2-from-master`, false}, 23 | {`\cygwin-x-pty2-from-master-`, true}, // for the feature 24 | {`\cygwin-e022582115c10879-pty4-from-master`, true}, 25 | {`\msys-e022582115c10879-pty4-to-master`, true}, 26 | {`\cygwin-e022582115c10879-pty4-to-master`, true}, 27 | {`\Device\NamedPipe\cygwin-e022582115c10879-pty4-from-master`, true}, 28 | {`\Device\NamedPipe\msys-e022582115c10879-pty4-to-master`, true}, 29 | {`Device\NamedPipe\cygwin-e022582115c10879-pty4-to-master`, false}, 30 | } 31 | 32 | for _, test := range tests { 33 | want := test.result 34 | got := isCygwinPipeName(test.name) 35 | if want != got { 36 | t.Fatalf("isatty(%q): got %v, want %v:", test.name, got, want) 37 | } 38 | } 39 | } 40 | --------------------------------------------------------------------------------