Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add Black and Underline #7

Merged
merged 1 commit into from
Oct 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@ import "github.com/TwiN/go-color"

func main() {
println(color.Ize(color.Red, "This is red"))
// Or if you prefer the longer version
// Or if you prefer the longer version:
println(color.Colorize(color.Red, "This is red"))
// Or if you prefer the non-parameterized version:
println(color.InRed("This is red"))
}
```

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

func main() {
println(color.InBold("This is bold"))
println(color.InUnderline("This is underlined"))
println(color.InBlack("This is black"))
println(color.InRed("This is red"))
println(color.InGreen("This is green"))
println(color.InYellow("This is yellow"))
Expand All @@ -76,6 +80,8 @@ import "github.com/TwiN/go-color"

func main() {
println(color.Bold + "This is bold" + color.Reset)
println(color.Underline + "This is underlined" + color.Reset)
println(color.Black + "This is black" + color.Reset)
println(color.Red + "This is red" + color.Reset)
println(color.Green + "This is green" + color.Reset)
println(color.Yellow + "This is yellow" + color.Reset)
Expand Down
73 changes: 52 additions & 21 deletions color.go
Original file line number Diff line number Diff line change
@@ -1,102 +1,133 @@
package color

var (
Reset = "\033[0m"
Bold = "\033[1m"
Red = "\033[31m"
Green = "\033[32m"
Yellow = "\033[33m"
Blue = "\033[34m"
Purple = "\033[35m"
Cyan = "\033[36m"
Gray = "\033[37m"
White = "\033[97m"
Reset = "\033[0m"
Bold = "\033[1m"
Underline = "\033[4m"
Black = "\033[30m"
Red = "\033[31m"
Green = "\033[32m"
Yellow = "\033[33m"
Blue = "\033[34m"
Purple = "\033[35m"
Cyan = "\033[36m"
Gray = "\033[37m"
White = "\033[97m"
)

// Ize is an alias for the Colorize function
//
// Example:
// println(color.Ize(color.Red, "This is red"))
//
// println(color.Ize(color.Red, "This is red"))
func Ize(color, s string) string {
return Colorize(color, s)
}

// Colorize wraps a given message in a given color.
//
// Example:
// println(color.Colorize(color.Red, "This is red"))
//
// println(color.Colorize(color.Red, "This is red"))
func Colorize(color, s string) string {
return color + s + Reset
}

// InBold wraps the given string s in Bold
//
// Example:
// println(color.InBold("This is bold"))
//
// println(color.InBold("This is bold"))
func InBold(s string) string {
return Colorize(Bold, s)
}

// InUnderline wraps the given string s in Underline
//
// Example:
//
// println(color.InUnderline("This is underlined"))
func InUnderline(s string) string {
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally, I would have preferred to call this function Underline, but that field is already taken by the variable so.. 😔

return Colorize(Underline, s)
}

// InBlack wraps the given string s in Black
//
// Example:
//
// println(color.InBlack("This is black"))
func InBlack(s string) string {
return Colorize(Black, s)
}

// InRed wraps the given string s in Red
//
// Example:
// println(color.InRed("This is red"))
//
// println(color.InRed("This is red"))
func InRed(s string) string {
return Colorize(Red, s)
}

// InGreen wraps the given string s in Green
//
// Example:
// println(color.InGreen("This is green"))
//
// println(color.InGreen("This is green"))
func InGreen(s string) string {
return Colorize(Green, s)
}

// InYellow wraps the given string s in Yellow
//
// Example:
// println(color.InYellow("This is yellow"))
//
// println(color.InYellow("This is yellow"))
func InYellow(s string) string {
return Colorize(Yellow, s)
}

// InBlue wraps the given string s in Blue
//
// Example:
// println(color.InBlue("This is blue"))
//
// println(color.InBlue("This is blue"))
func InBlue(s string) string {
return Colorize(Blue, s)
}

// InPurple wraps the given string s in Purple
//
// Example:
// println(color.InPurple("This is purple"))
//
// println(color.InPurple("This is purple"))
func InPurple(s string) string {
return Colorize(Purple, s)
}

// InCyan wraps the given string s in Cyan
//
// Example:
// println(color.InCyan("This is cyan"))
//
// println(color.InCyan("This is cyan"))
func InCyan(s string) string {
return Colorize(Cyan, s)
}

// InGray wraps the given string s in Gray
//
// Example:
// println(color.InGray("This is gray"))
//
// println(color.InGray("This is gray"))
func InGray(s string) string {
return Colorize(Gray, s)
}

// InWhite wraps the given string s in White
//
// Example:
// println(color.InWhite("This is white"))
//
// println(color.InWhite("This is white"))
func InWhite(s string) string {
return Colorize(White, s)
}
21 changes: 21 additions & 0 deletions color_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import "testing"

// Clearly not a real test - just a visual reference
func TestOutput(t *testing.T) {
println(Bold + "Hello" + Reset)
println(Underline + "Hello" + Reset)
println(Black + "Hello" + Reset)
println(Red + "Hello" + Reset)
println(Green + "Hello" + Reset)
println(Yellow + "Hello" + Reset)
Expand All @@ -25,6 +28,14 @@ func TestColorize(t *testing.T) {
Color: Bold,
ExpectedOutput: "\033[1mtest\033[0m",
},
{
Color: Underline,
ExpectedOutput: "\033[4mtest\033[0m",
},
{
Color: Black,
ExpectedOutput: "\033[30mtest\033[0m",
},
{
Color: Red,
ExpectedOutput: "\033[31mtest\033[0m",
Expand Down Expand Up @@ -79,6 +90,16 @@ func TestIn(t *testing.T) {
Color: Bold,
ExpectedOutput: "\033[1mtest\033[0m",
},
{
Func: InUnderline,
Color: Underline,
ExpectedOutput: "\033[4mtest\033[0m",
},
{
Func: InBlack,
Color: Black,
ExpectedOutput: "\033[30mtest\033[0m",
},
{
Func: InRed,
Color: Red,
Expand Down
2 changes: 2 additions & 0 deletions color_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ func init() {
if _, _, err := setConsoleModeProc.Call(uintptr(handle), 0x0001|0x0002|0x0004); err != nil && err.Error() != "The operation completed successfully." {
Reset = ""
Bold = ""
Underline = ""
Black = ""
Red = ""
Green = ""
Yellow = ""
Expand Down