Merge pull request #632 from harshavardhana/pr_out_add_function_isatty_to_detect_if_stdout_for_console_is_a_terminal_if_not_disable_coloring
This commit is contained in:
@@ -38,7 +38,14 @@ var (
|
||||
themesDB = map[string]Theme{"minimal": MiniTheme, "nocolor": NoColorTheme, "white": WhiteTheme}
|
||||
|
||||
// currTheme is current theme
|
||||
currThemeName = GetDefaultThemeName()
|
||||
currThemeName = func() string {
|
||||
theme := GetDefaultThemeName()
|
||||
// if not a TTY disable color
|
||||
if !isatty(os.Stdout.Fd()) {
|
||||
return "nocolor"
|
||||
}
|
||||
return theme
|
||||
}()
|
||||
|
||||
// Print prints a error message and exits
|
||||
Print = themesDB[currThemeName].Info.Print
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
// +build darwin freebsd openbsd netbsd
|
||||
|
||||
package console
|
||||
|
||||
import (
|
||||
"syscall"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
/// *BSD console functions
|
||||
//
|
||||
// http://fxr.watson.org/fxr/source/sys/ttycom.h?v=FREEBSD6;im=3#L69
|
||||
//
|
||||
const ioctlReadTermios = syscall.TIOCGETA
|
||||
|
||||
// isatty return true if the file descriptor is terminal.
|
||||
func isatty(fd uintptr) bool {
|
||||
var termios syscall.Termios
|
||||
_, _, err := syscall.Syscall6(syscall.SYS_IOCTL, fd, ioctlReadTermios, uintptr(unsafe.Pointer(&termios)), 0, 0, 0)
|
||||
return err == 0
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
// +build linux
|
||||
|
||||
package console
|
||||
|
||||
import (
|
||||
"syscall"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
// Standard ioctls from Linux - /usr/include/asm-generic/ioctls.h:#define TCGETS 0x5401
|
||||
const ioctlReadTermios = syscall.TCGETS
|
||||
|
||||
// isatty return true if the file descriptor is terminal.
|
||||
func isatty(fd uintptr) bool {
|
||||
var termios syscall.Termios
|
||||
_, _, err := syscall.Syscall6(syscall.SYS_IOCTL, fd, ioctlReadTermios, uintptr(unsafe.Pointer(&termios)), 0, 0, 0)
|
||||
return err == 0
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
// +build windows
|
||||
package console
|
||||
|
||||
import (
|
||||
"syscall"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
/// Console functions
|
||||
//
|
||||
// https://msdn.microsoft.com/en-us/library/windows/desktop/ms683167%28v=vs.85%29.aspx
|
||||
var kernel32 = syscall.NewLazyDLL("kernel32.dll") // ugly DLL loading but cannot do much on windows
|
||||
var procGetConsoleMode = kernel32.NewProc("GetConsoleMode")
|
||||
|
||||
// isatty return true if the file descriptor is terminal.
|
||||
func isatty(fd uintptr) bool {
|
||||
var st uint32
|
||||
r, _, e := syscall.Syscall(procGetConsoleMode.Addr(), 2, fd, uintptr(unsafe.Pointer(&st)), 0)
|
||||
return r != 0 && e == 0
|
||||
}
|
||||
Reference in New Issue
Block a user