Skip to content

Commit 0239897

Browse files
committed
feat: Add Black and Underline
1 parent d4362db commit 0239897

File tree

4 files changed

+82
-22
lines changed

4 files changed

+82
-22
lines changed

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,10 @@ import "github.com/TwiN/go-color"
2929

3030
func main() {
3131
println(color.Ize(color.Red, "This is red"))
32-
// Or if you prefer the longer version
32+
// Or if you prefer the longer version:
3333
println(color.Colorize(color.Red, "This is red"))
34+
// Or if you prefer the non-parameterized version:
35+
println(color.InRed("This is red"))
3436
}
3537
```
3638

@@ -50,6 +52,8 @@ import "github.com/TwiN/go-color"
5052

5153
func main() {
5254
println(color.InBold("This is bold"))
55+
println(color.InUnderline("This is underlined"))
56+
println(color.InBlack("This is black"))
5357
println(color.InRed("This is red"))
5458
println(color.InGreen("This is green"))
5559
println(color.InYellow("This is yellow"))
@@ -76,6 +80,8 @@ import "github.com/TwiN/go-color"
7680

7781
func main() {
7882
println(color.Bold + "This is bold" + color.Reset)
83+
println(color.Underline + "This is underlined" + color.Reset)
84+
println(color.Black + "This is black" + color.Reset)
7985
println(color.Red + "This is red" + color.Reset)
8086
println(color.Green + "This is green" + color.Reset)
8187
println(color.Yellow + "This is yellow" + color.Reset)

color.go

Lines changed: 52 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,102 +1,133 @@
11
package color
22

33
var (
4-
Reset = "\033[0m"
5-
Bold = "\033[1m"
6-
Red = "\033[31m"
7-
Green = "\033[32m"
8-
Yellow = "\033[33m"
9-
Blue = "\033[34m"
10-
Purple = "\033[35m"
11-
Cyan = "\033[36m"
12-
Gray = "\033[37m"
13-
White = "\033[97m"
4+
Reset = "\033[0m"
5+
Bold = "\033[1m"
6+
Underline = "\033[4m"
7+
Black = "\033[30m"
8+
Red = "\033[31m"
9+
Green = "\033[32m"
10+
Yellow = "\033[33m"
11+
Blue = "\033[34m"
12+
Purple = "\033[35m"
13+
Cyan = "\033[36m"
14+
Gray = "\033[37m"
15+
White = "\033[97m"
1416
)
1517

1618
// Ize is an alias for the Colorize function
1719
//
1820
// Example:
19-
// println(color.Ize(color.Red, "This is red"))
21+
//
22+
// println(color.Ize(color.Red, "This is red"))
2023
func Ize(color, s string) string {
2124
return Colorize(color, s)
2225
}
2326

2427
// Colorize wraps a given message in a given color.
2528
//
2629
// Example:
27-
// println(color.Colorize(color.Red, "This is red"))
30+
//
31+
// println(color.Colorize(color.Red, "This is red"))
2832
func Colorize(color, s string) string {
2933
return color + s + Reset
3034
}
3135

3236
// InBold wraps the given string s in Bold
3337
//
3438
// Example:
35-
// println(color.InBold("This is bold"))
39+
//
40+
// println(color.InBold("This is bold"))
3641
func InBold(s string) string {
3742
return Colorize(Bold, s)
3843
}
3944

45+
// InUnderline wraps the given string s in Underline
46+
//
47+
// Example:
48+
//
49+
// println(color.InUnderline("This is underlined"))
50+
func InUnderline(s string) string {
51+
return Colorize(Underline, s)
52+
}
53+
54+
// InBlack wraps the given string s in Black
55+
//
56+
// Example:
57+
//
58+
// println(color.InBlack("This is black"))
59+
func InBlack(s string) string {
60+
return Colorize(Black, s)
61+
}
62+
4063
// InRed wraps the given string s in Red
4164
//
4265
// Example:
43-
// println(color.InRed("This is red"))
66+
//
67+
// println(color.InRed("This is red"))
4468
func InRed(s string) string {
4569
return Colorize(Red, s)
4670
}
4771

4872
// InGreen wraps the given string s in Green
4973
//
5074
// Example:
51-
// println(color.InGreen("This is green"))
75+
//
76+
// println(color.InGreen("This is green"))
5277
func InGreen(s string) string {
5378
return Colorize(Green, s)
5479
}
5580

5681
// InYellow wraps the given string s in Yellow
5782
//
5883
// Example:
59-
// println(color.InYellow("This is yellow"))
84+
//
85+
// println(color.InYellow("This is yellow"))
6086
func InYellow(s string) string {
6187
return Colorize(Yellow, s)
6288
}
6389

6490
// InBlue wraps the given string s in Blue
6591
//
6692
// Example:
67-
// println(color.InBlue("This is blue"))
93+
//
94+
// println(color.InBlue("This is blue"))
6895
func InBlue(s string) string {
6996
return Colorize(Blue, s)
7097
}
7198

7299
// InPurple wraps the given string s in Purple
73100
//
74101
// Example:
75-
// println(color.InPurple("This is purple"))
102+
//
103+
// println(color.InPurple("This is purple"))
76104
func InPurple(s string) string {
77105
return Colorize(Purple, s)
78106
}
79107

80108
// InCyan wraps the given string s in Cyan
81109
//
82110
// Example:
83-
// println(color.InCyan("This is cyan"))
111+
//
112+
// println(color.InCyan("This is cyan"))
84113
func InCyan(s string) string {
85114
return Colorize(Cyan, s)
86115
}
87116

88117
// InGray wraps the given string s in Gray
89118
//
90119
// Example:
91-
// println(color.InGray("This is gray"))
120+
//
121+
// println(color.InGray("This is gray"))
92122
func InGray(s string) string {
93123
return Colorize(Gray, s)
94124
}
95125

96126
// InWhite wraps the given string s in White
97127
//
98128
// Example:
99-
// println(color.InWhite("This is white"))
129+
//
130+
// println(color.InWhite("This is white"))
100131
func InWhite(s string) string {
101132
return Colorize(White, s)
102133
}

color_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ import "testing"
44

55
// Clearly not a real test - just a visual reference
66
func TestOutput(t *testing.T) {
7+
println(Bold + "Hello" + Reset)
8+
println(Underline + "Hello" + Reset)
9+
println(Black + "Hello" + Reset)
710
println(Red + "Hello" + Reset)
811
println(Green + "Hello" + Reset)
912
println(Yellow + "Hello" + Reset)
@@ -25,6 +28,14 @@ func TestColorize(t *testing.T) {
2528
Color: Bold,
2629
ExpectedOutput: "\033[1mtest\033[0m",
2730
},
31+
{
32+
Color: Underline,
33+
ExpectedOutput: "\033[4mtest\033[0m",
34+
},
35+
{
36+
Color: Black,
37+
ExpectedOutput: "\033[30mtest\033[0m",
38+
},
2839
{
2940
Color: Red,
3041
ExpectedOutput: "\033[31mtest\033[0m",
@@ -79,6 +90,16 @@ func TestIn(t *testing.T) {
7990
Color: Bold,
8091
ExpectedOutput: "\033[1mtest\033[0m",
8192
},
93+
{
94+
Func: InUnderline,
95+
Color: Underline,
96+
ExpectedOutput: "\033[4mtest\033[0m",
97+
},
98+
{
99+
Func: InBlack,
100+
Color: Black,
101+
ExpectedOutput: "\033[30mtest\033[0m",
102+
},
82103
{
83104
Func: InRed,
84105
Color: Red,

color_windows.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ func init() {
1616
if _, _, err := setConsoleModeProc.Call(uintptr(handle), 0x0001|0x0002|0x0004); err != nil && err.Error() != "The operation completed successfully." {
1717
Reset = ""
1818
Bold = ""
19+
Underline = ""
20+
Black = ""
1921
Red = ""
2022
Green = ""
2123
Yellow = ""

0 commit comments

Comments
 (0)