44 "bytes"
55 "io"
66 "os"
7+
8+ "github.com/fatih/color"
79)
810
911type fileWriter interface {
@@ -20,6 +22,8 @@ type IOStreams struct {
2022 In fileReader
2123 Out fileWriter
2224 ErrOut fileWriter
25+
26+ colorEnabled bool
2327}
2428
2529func System () * IOStreams {
@@ -29,6 +33,8 @@ func System() *IOStreams {
2933 ErrOut : os .Stderr ,
3034 }
3135
36+ ios .colorEnabled = ! color .NoColor
37+
3238 return ios
3339}
3440
@@ -42,13 +48,57 @@ func Test() (*IOStreams, *bytes.Buffer, *bytes.Buffer, *bytes.Buffer) {
4248 fd : 0 ,
4349 ReadCloser : io .NopCloser (in ),
4450 },
45- Out : & fdWriter {fd : 1 , Writer : out },
46- ErrOut : & fdWriter {fd : 2 , Writer : errOut },
51+ Out : & fdWriter {fd : 1 , Writer : out },
52+ ErrOut : & fdWriter {fd : 2 , Writer : errOut },
53+ colorEnabled : false ,
4754 }
4855
4956 return io , in , out , errOut
5057}
5158
59+ func (ios * IOStreams ) ColorEnabled () bool {
60+ return ios .colorEnabled
61+ }
62+
63+ type ColorScheme struct {
64+ enabled bool
65+ }
66+
67+ func (ios * IOStreams ) ColorScheme () * ColorScheme {
68+ return & ColorScheme {enabled : ios .colorEnabled }
69+ }
70+
71+ func (cs * ColorScheme ) format (c * color.Color , s string ) string {
72+ if ! cs .enabled {
73+ return s
74+ }
75+ return c .Sprint (s )
76+ }
77+
78+ func (cs * ColorScheme ) Bold (s string ) string {
79+ return cs .format (color .New (color .Bold ), s )
80+ }
81+
82+ func (cs * ColorScheme ) Green (s string ) string {
83+ return cs .format (color .New (color .FgGreen ), s )
84+ }
85+
86+ func (cs * ColorScheme ) Cyan (s string ) string {
87+ return cs .format (color .New (color .FgCyan ), s )
88+ }
89+
90+ func (cs * ColorScheme ) Yellow (s string ) string {
91+ return cs .format (color .New (color .FgYellow ), s )
92+ }
93+
94+ func (cs * ColorScheme ) Red (s string ) string {
95+ return cs .format (color .New (color .FgRed ), s )
96+ }
97+
98+ func (cs * ColorScheme ) Muted (s string ) string {
99+ return cs .format (color .New (color .FgHiBlack ), s )
100+ }
101+
52102type fdWriter struct {
53103 io.Writer
54104 fd uintptr
0 commit comments