David K. Bainbridge | 528b318 | 2017-01-23 08:51:59 -0800 | [diff] [blame] | 1 | // Copyright 2016 Canonical Ltd. |
| 2 | // Licensed under the LGPLv3, see LICENCE file for details. |
| 3 | |
| 4 | package ansiterm |
| 5 | |
| 6 | import ( |
| 7 | "io" |
| 8 | "os" |
| 9 | |
| 10 | "github.com/mattn/go-colorable" |
| 11 | "github.com/mattn/go-isatty" |
| 12 | ) |
| 13 | |
| 14 | // colorEnabledWriter returns a writer that can handle the ansi color codes |
| 15 | // and true if the writer passed in is a terminal capable of color. If the |
| 16 | // TERM environment variable is set to "dumb", the terminal is not considered |
| 17 | // color capable. |
| 18 | func colorEnabledWriter(w io.Writer) (io.Writer, bool) { |
| 19 | f, ok := w.(*os.File) |
| 20 | if !ok { |
| 21 | return w, false |
| 22 | } |
| 23 | // Check the TERM environment variable specifically |
| 24 | // to check for "dumb" terminals. |
| 25 | if os.Getenv("TERM") == "dumb" { |
| 26 | return w, false |
| 27 | } |
| 28 | if !isatty.IsTerminal(f.Fd()) { |
| 29 | return w, false |
| 30 | } |
| 31 | return colorable.NewColorable(f), true |
| 32 | } |