From 6d4a7c5e7bb9f85c57f94c7705f04ae77b6313ad Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Sat, 16 May 2015 18:10:12 -0700 Subject: [PATCH] Add function isatty() to detect if Stdout for console is a terminal, if not disable coloring - fixes #631 Necessary when one has to pipe ``mc`` output to non terminal applications like ``less`` ``more`` etc. --- pkg/console/console.go | 9 ++++++++- pkg/console/isatty_bsd.go | 21 +++++++++++++++++++++ pkg/console/isatty_linux.go | 18 ++++++++++++++++++ pkg/console/isatty_windows.go | 20 ++++++++++++++++++++ 4 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 pkg/console/isatty_bsd.go create mode 100644 pkg/console/isatty_linux.go create mode 100644 pkg/console/isatty_windows.go diff --git a/pkg/console/console.go b/pkg/console/console.go index 86822bda..4365956a 100644 --- a/pkg/console/console.go +++ b/pkg/console/console.go @@ -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 diff --git a/pkg/console/isatty_bsd.go b/pkg/console/isatty_bsd.go new file mode 100644 index 00000000..a8adc699 --- /dev/null +++ b/pkg/console/isatty_bsd.go @@ -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 +} diff --git a/pkg/console/isatty_linux.go b/pkg/console/isatty_linux.go new file mode 100644 index 00000000..a11f9631 --- /dev/null +++ b/pkg/console/isatty_linux.go @@ -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 +} diff --git a/pkg/console/isatty_windows.go b/pkg/console/isatty_windows.go new file mode 100644 index 00000000..b0d3cedc --- /dev/null +++ b/pkg/console/isatty_windows.go @@ -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 +}