/
color_windows.go
94 lines (74 loc) · 2.21 KB
/
color_windows.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// Copyright 2012 The Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package term
import (
"github.com/anschelsc/doscolor"
"fmt"
"os"
)
func black(f *os.File, s string) error {
return text(f, s, []doscolor.Color{doscolor.Black})
}
func white(f *os.File, s string) error {
return text(f, s, []doscolor.Color{doscolor.White, doscolor.Bright})
}
func red(f *os.File, s string) error {
return text(f, s, []doscolor.Color{doscolor.Red})
}
func lightRed(f *os.File, s string) error {
return text(f, s, []doscolor.Color{doscolor.Red, doscolor.Bright})
}
func green(f *os.File, s string) error {
return text(f, s, []doscolor.Color{doscolor.Green})
}
func lightGreen(f *os.File, s string) error {
return text(f, s, []doscolor.Color{doscolor.Green, doscolor.Bright})
}
func darkYellow(f *os.File, s string) error {
return text(f, s, []doscolor.Color{doscolor.Yellow})
}
func lightYellow(f *os.File, s string) error {
return text(f, s, []doscolor.Color{doscolor.Yellow, doscolor.Bright})
}
func blue(f *os.File, s string) error {
return text(f, s, []doscolor.Color{doscolor.Blue})
}
func lightBlue(f *os.File, s string) error {
return text(f, s, []doscolor.Color{doscolor.Blue, doscolor.Bright})
}
func magenta(f *os.File, s string) error {
return text(f, s, []doscolor.Color{doscolor.Magenta})
}
func lightMagenta(f *os.File, s string) error {
return text(f, s, []doscolor.Color{doscolor.Magenta, doscolor.Bright})
}
func cyan(f *os.File, s string) error {
return text(f, s, []doscolor.Color{doscolor.Cyan})
}
func lightCyan(f *os.File, s string) error {
return text(f, s, []doscolor.Color{doscolor.Cyan, doscolor.Bright})
}
// Return an error if the text wasn't printed;
// otherwise return nil
func text(f *os.File, s string, c []doscolor.Color) error {
output := doscolor.NewWrapper(f)
if err := output.Save(); err != nil {
return err
}
if len(c) < 1 {
fmt.Fprintln(output, s)
}
var cbits doscolor.Color = 0
for _, color := range c {
cbits |= color
}
if err := output.Set(cbits); err != nil {
return err
}
fmt.Fprint(output, s)
// Don't check for an error here,
// because text has been printed
output.Restore()
return nil
}