khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 1 | // +build !appengine,!js,windows |
2 | |||||
3 | package logrus | ||||
4 | |||||
5 | import ( | ||||
6 | "io" | ||||
7 | "os" | ||||
8 | "syscall" | ||||
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 9 | |
10 | sequences "github.com/konsorten/go-windows-terminal-sequences" | ||||
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 11 | ) |
12 | |||||
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 13 | func initTerminal(w io.Writer) { |
14 | switch v := w.(type) { | ||||
15 | case *os.File: | ||||
16 | sequences.EnableVirtualTerminalProcessing(syscall.Handle(v.Fd()), true) | ||||
17 | } | ||||
18 | } | ||||
19 | |||||
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 20 | func checkIfTerminal(w io.Writer) bool { |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 21 | var ret bool |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 22 | switch v := w.(type) { |
23 | case *os.File: | ||||
24 | var mode uint32 | ||||
25 | err := syscall.GetConsoleMode(syscall.Handle(v.Fd()), &mode) | ||||
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 26 | ret = (err == nil) |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 27 | default: |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 28 | ret = false |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 29 | } |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 30 | if ret { |
31 | initTerminal(w) | ||||
32 | } | ||||
33 | return ret | ||||
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 34 | } |