@@ -19,15 +19,15 @@ var (
1919 // set (regardless of its value). This is a global option and affects all
2020 // colors. For more control over each color block use the methods
2121 // DisableColor() individually.
22- NoColor = noColorIsSet () || os .Getenv ("TERM" ) == "dumb" ||
23- (! isatty .IsTerminal (os .Stdout .Fd ()) && ! isatty .IsCygwinTerminal (os .Stdout .Fd ()))
22+ NoColor = noColorIsSet () || os .Getenv ("TERM" ) == "dumb" || ! stdoutIsTerminal ()
2423
2524 // Output defines the standard output of the print functions. By default,
26- // os.Stdout is used.
27- Output = colorable . NewColorableStdout ()
25+ // stdOut() is used.
26+ Output = stdOut ()
2827
29- // Error defines a color supporting writer for os.Stderr.
30- Error = colorable .NewColorableStderr ()
28+ // Error defines the standard error of the print functions. By default,
29+ // stdErr() is used.
30+ Error = stdErr ()
3131
3232 // colorsCache is used to reduce the count of created Color objects and
3333 // allows to reuse already created objects with required Attribute.
@@ -40,6 +40,33 @@ func noColorIsSet() bool {
4040 return os .Getenv ("NO_COLOR" ) != ""
4141}
4242
43+ // stdoutIsTerminal returns true if os.Stdout is a terminal.
44+ // Returns false if os.Stdout is nil (e.g., when running as a Windows service).
45+ func stdoutIsTerminal () bool {
46+ if os .Stdout == nil {
47+ return false
48+ }
49+ return isatty .IsTerminal (os .Stdout .Fd ()) || isatty .IsCygwinTerminal (os .Stdout .Fd ())
50+ }
51+
52+ // stdOut returns a writer for color output.
53+ // Returns io.Discard if os.Stdout is nil (e.g., when running as a Windows service).
54+ func stdOut () io.Writer {
55+ if os .Stdout == nil {
56+ return io .Discard
57+ }
58+ return colorable .NewColorableStdout ()
59+ }
60+
61+ // stdErr returns a writer for color error output.
62+ // Returns io.Discard if os.Stderr is nil (e.g., when running as a Windows service).
63+ func stdErr () io.Writer {
64+ if os .Stderr == nil {
65+ return io .Discard
66+ }
67+ return colorable .NewColorableStderr ()
68+ }
69+
4370// Color defines a custom color object which is defined by SGR parameters.
4471type Color struct {
4572 params []Attribute
0 commit comments