blob: 572889db2162971561061ca240762fca3777289b [file] [log] [blame]
khenaidoo59ce9dd2019-11-11 13:05:32 -05001// +build !appengine,!js,windows
2
3package logrus
4
5import (
6 "io"
7 "os"
8 "syscall"
khenaidoo26721882021-08-11 17:42:52 -04009
10 sequences "github.com/konsorten/go-windows-terminal-sequences"
khenaidoo59ce9dd2019-11-11 13:05:32 -050011)
12
khenaidoo26721882021-08-11 17:42:52 -040013func initTerminal(w io.Writer) {
14 switch v := w.(type) {
15 case *os.File:
16 sequences.EnableVirtualTerminalProcessing(syscall.Handle(v.Fd()), true)
17 }
18}
19
khenaidoo59ce9dd2019-11-11 13:05:32 -050020func checkIfTerminal(w io.Writer) bool {
khenaidoo26721882021-08-11 17:42:52 -040021 var ret bool
khenaidoo59ce9dd2019-11-11 13:05:32 -050022 switch v := w.(type) {
23 case *os.File:
24 var mode uint32
25 err := syscall.GetConsoleMode(syscall.Handle(v.Fd()), &mode)
khenaidoo26721882021-08-11 17:42:52 -040026 ret = (err == nil)
khenaidoo59ce9dd2019-11-11 13:05:32 -050027 default:
khenaidoo26721882021-08-11 17:42:52 -040028 ret = false
khenaidoo59ce9dd2019-11-11 13:05:32 -050029 }
khenaidoo26721882021-08-11 17:42:52 -040030 if ret {
31 initTerminal(w)
32 }
33 return ret
khenaidoo59ce9dd2019-11-11 13:05:32 -050034}