khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 1 | // +build !appengine,!js,windows |
2 | |||||
3 | package logrus | ||||
4 | |||||
5 | import ( | ||||
6 | "io" | ||||
7 | "os" | ||||
8 | "syscall" | ||||
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 9 | |
10 | sequences "github.com/konsorten/go-windows-terminal-sequences" | ||||
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 11 | ) |
12 | |||||
khenaidoo | 2672188 | 2021-08-11 17:42:52 -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 | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 20 | func checkIfTerminal(w io.Writer) bool { |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 21 | var ret bool |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -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 | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 26 | ret = (err == nil) |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 27 | default: |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 28 | ret = false |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 29 | } |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 30 | if ret { |
31 | initTerminal(w) | ||||
32 | } | ||||
33 | return ret | ||||
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 34 | } |