Skip to content

Commit 3a0d3b0

Browse files
committed
color: rename std stream helpers
Problem: The nil-safe writer helpers in this PR are named and , which reads like package init work instead of standard stream accessors. The related docs also still describe the old defaults. Solution: Rename the helpers to and , and update the nearby doc comments so and describe the actual default writers.
1 parent d71cc52 commit 3a0d3b0

File tree

2 files changed

+45
-8
lines changed

2 files changed

+45
-8
lines changed

color.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,12 @@ var (
2222
NoColor = noColorIsSet() || os.Getenv("TERM") == "dumb" || !stdoutIsTerminal()
2323

2424
// Output defines the standard output of the print functions. By default,
25-
// os.Stdout is used.
26-
Output = initOutput()
25+
// stdOut() is used.
26+
Output = stdOut()
2727

28-
// Error defines a color supporting writer for os.Stderr.
29-
Error = initError()
28+
// Error defines the standard error of the print functions. By default,
29+
// stdErr() is used.
30+
Error = stdErr()
3031

3132
// colorsCache is used to reduce the count of created Color objects and
3233
// allows to reuse already created objects with required Attribute.
@@ -48,18 +49,18 @@ func stdoutIsTerminal() bool {
4849
return isatty.IsTerminal(os.Stdout.Fd()) || isatty.IsCygwinTerminal(os.Stdout.Fd())
4950
}
5051

51-
// initOutput returns a writer for color output.
52+
// stdOut returns a writer for color output.
5253
// Returns io.Discard if os.Stdout is nil (e.g., when running as a Windows service).
53-
func initOutput() io.Writer {
54+
func stdOut() io.Writer {
5455
if os.Stdout == nil {
5556
return io.Discard
5657
}
5758
return colorable.NewColorableStdout()
5859
}
5960

60-
// initError returns a writer for color error output.
61+
// stdErr returns a writer for color error output.
6162
// Returns io.Discard if os.Stderr is nil (e.g., when running as a Windows service).
62-
func initError() io.Writer {
63+
func stdErr() io.Writer {
6364
if os.Stderr == nil {
6465
return io.Discard
6566
}

color_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,42 @@ func Test_noColorIsSet(t *testing.T) {
236236
}
237237
}
238238

239+
func TestStdoutIsTerminal_NilStdout(t *testing.T) {
240+
stdout := os.Stdout
241+
os.Stdout = nil
242+
t.Cleanup(func() {
243+
os.Stdout = stdout
244+
})
245+
246+
if stdoutIsTerminal() {
247+
t.Fatal("stdoutIsTerminal() = true, want false")
248+
}
249+
}
250+
251+
func TestStdOut_NilStdout(t *testing.T) {
252+
stdout := os.Stdout
253+
os.Stdout = nil
254+
t.Cleanup(func() {
255+
os.Stdout = stdout
256+
})
257+
258+
if got := stdOut(); got != io.Discard {
259+
t.Fatalf("stdOut() = %v, want %v", got, io.Discard)
260+
}
261+
}
262+
263+
func TestStdErr_NilStderr(t *testing.T) {
264+
stderr := os.Stderr
265+
os.Stderr = nil
266+
t.Cleanup(func() {
267+
os.Stderr = stderr
268+
})
269+
270+
if got := stdErr(); got != io.Discard {
271+
t.Fatalf("stdErr() = %v, want %v", got, io.Discard)
272+
}
273+
}
274+
239275
func TestColorVisual(t *testing.T) {
240276
// First Visual Test
241277
Output = colorable.NewColorableStdout()

0 commit comments

Comments
 (0)