Matteo Scandolo | a6a3aee | 2019-11-26 13:30:14 -0700 | [diff] [blame] | 1 | // +build !appengine,!js,windows |
| 2 | |
| 3 | package logrus |
| 4 | |
| 5 | import ( |
| 6 | "io" |
| 7 | "os" |
| 8 | "syscall" |
| 9 | |
| 10 | sequences "github.com/konsorten/go-windows-terminal-sequences" |
| 11 | ) |
| 12 | |
| 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 | |
| 20 | func checkIfTerminal(w io.Writer) bool { |
| 21 | var ret bool |
| 22 | switch v := w.(type) { |
| 23 | case *os.File: |
| 24 | var mode uint32 |
| 25 | err := syscall.GetConsoleMode(syscall.Handle(v.Fd()), &mode) |
| 26 | ret = (err == nil) |
| 27 | default: |
| 28 | ret = false |
| 29 | } |
| 30 | if ret { |
| 31 | initTerminal(w) |
| 32 | } |
| 33 | return ret |
| 34 | } |