blob: 96fd11c51005efd2cf77259e92c7d64815160847 [file] [log] [blame]
David K. Bainbridge528b3182017-01-23 08:51:59 -08001// Copyright 2016 Canonical Ltd.
2// Licensed under the LGPLv3, see LICENCE file for details.
3
4package ansiterm
5
6import (
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.
18func 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}