-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathtty.go
More file actions
53 lines (46 loc) · 1.31 KB
/
tty.go
File metadata and controls
53 lines (46 loc) · 1.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package main
import (
"fmt"
"os"
"runtime"
"syscall"
"unsafe"
)
// isTerminal reports if the given file descriptor is attached to a terminal.
// • On Linux it calls ioctl(TCGETS).
// • On all other OSes it always returns false since colors are only implemented for Linux
func isTerminal(fd uintptr) bool {
if runtime.GOOS != "linux" {
return false // avoid platform-specific hassles
}
const ioctlReadTermios = 0x5401 // TCGETS on Linux
var termios syscall.Termios
_, _, err := syscall.Syscall6(
syscall.SYS_IOCTL,
fd,
uintptr(ioctlReadTermios),
uintptr(unsafe.Pointer(&termios)),
0, 0, 0,
)
return err == 0
}
// printColoredPrefix writes an ANSI escape sequence to set the color.
func printColoredPrefix(color string) {
fmt.Fprintf(os.Stderr, "\033[%sm", color)
}
// printColoredSuffix resets the terminal color.
func printColoredSuffix() {
fmt.Fprint(os.Stderr, "\033[0m")
}
// FprintfError writes an error message to stderr. If stderr is a TTY,
// it prints the message in bright red.
func FprintfError(format string, args ...interface{}) {
if isTerminal(os.Stderr.Fd()) {
printColoredPrefix("1;31") // bright red
fmt.Fprintf(os.Stderr, format, args...)
printColoredSuffix()
} else {
// If it's not a terminal, just print normally (no colors).
fmt.Fprintf(os.Stderr, format, args...)
}
}