blob: 572889db2162971561061ca240762fca3777289b [file] [log] [blame]
khenaidooab1f7bd2019-11-14 14:00:27 -05001// +build !appengine,!js,windows
2
3package logrus
4
5import (
6 "io"
7 "os"
8 "syscall"
khenaidood948f772021-08-11 17:49:24 -04009
10 sequences "github.com/konsorten/go-windows-terminal-sequences"
khenaidooab1f7bd2019-11-14 14:00:27 -050011)
12
khenaidood948f772021-08-11 17:49:24 -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
khenaidooab1f7bd2019-11-14 14:00:27 -050020func checkIfTerminal(w io.Writer) bool {
khenaidood948f772021-08-11 17:49:24 -040021 var ret bool
khenaidooab1f7bd2019-11-14 14:00:27 -050022 switch v := w.(type) {
23 case *os.File:
24 var mode uint32
25 err := syscall.GetConsoleMode(syscall.Handle(v.Fd()), &mode)
khenaidood948f772021-08-11 17:49:24 -040026 ret = (err == nil)
khenaidooab1f7bd2019-11-14 14:00:27 -050027 default:
khenaidood948f772021-08-11 17:49:24 -040028 ret = false
khenaidooab1f7bd2019-11-14 14:00:27 -050029 }
khenaidood948f772021-08-11 17:49:24 -040030 if ret {
31 initTerminal(w)
32 }
33 return ret
khenaidooab1f7bd2019-11-14 14:00:27 -050034}