-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
221 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,41 @@ | ||
# go-color | ||
Go Color | ||
go-color is a simple color library for Go that allows you to print colored text for the Unix terminal. | ||
|
||
# Installation | ||
|
||
```bash | ||
go install github.com/ermanimer/go-color | ||
``` | ||
|
||
# Usage | ||
|
||
```go | ||
// use predefined colors | ||
color.Red.Println("red text") | ||
|
||
// create colors with color codes, see the color chart below. | ||
color.New(240).Println("gray text") | ||
|
||
// add background color | ||
color.New(220).With(color.Blue).Println("orange text on a blue background") | ||
|
||
// create colored strings | ||
fmt.Printf("%s %s\n", color.Green.Paint("green text"), color.Yellow.Paint("with yellow text")) | ||
``` | ||
|
||
**Output** | ||
|
||
![output](images/output.png) | ||
|
||
|
||
**Color Codes:** | ||
|
||
![color_codes](images/color_codes.png) | ||
|
||
# Contribution | ||
|
||
Your contribution and feedback are always welcome. | ||
|
||
# References | ||
|
||
[ANSI Escape Code](https://en.wikipedia.org/wiki/ANSI_escape_code) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package color | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
) | ||
|
||
type colorCode int | ||
|
||
const ( | ||
Black colorCode = 0 + iota | ||
Red | ||
Green | ||
Yellow | ||
Blue | ||
Magenta | ||
Cyan | ||
White | ||
BrightBlack | ||
BrightRed | ||
BrightGreen | ||
BrightYellow | ||
BrightBlue | ||
BrightMagenta | ||
BrightCyan | ||
BrightWhite | ||
) | ||
|
||
func New(code byte) colorCode { | ||
return colorCode(code) | ||
} | ||
|
||
func (c colorCode) Fprint(w io.Writer, a ...any) { | ||
makeEscCode(fg, c).Fprint(w, a...) | ||
} | ||
|
||
func (c colorCode) Fprintf(w io.Writer, format string, a ...any) { | ||
makeEscCode(fg, c).Fprintf(w, format, a...) | ||
} | ||
|
||
func (c colorCode) Fprintln(w io.Writer, a ...any) { | ||
makeEscCode(fg, c).Fprintln(w, a...) | ||
} | ||
|
||
func (c colorCode) Print(a ...any) { | ||
makeEscCode(fg, c).Print(a...) | ||
} | ||
|
||
func (c colorCode) Printf(format string, a ...any) { | ||
makeEscCode(fg, c).Printf(format, a...) | ||
} | ||
|
||
func (c colorCode) Println(a ...any) { | ||
makeEscCode(fg, c).Println(a...) | ||
} | ||
|
||
func (c colorCode) Paint(a any) string { | ||
return makeEscCode(fg, c).Paint(a) | ||
} | ||
|
||
func (foreground colorCode) With(background colorCode) escCode { | ||
return escCode(fmt.Sprintf("%s%s", makeEscCode(fg, foreground), makeEscCode(bg, background))) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package color | ||
|
||
import "testing" | ||
|
||
func TestWith(t *testing.T) { | ||
expected := escCode("\x1b[38;5;7;m\x1b[48;5;1;m") | ||
actual := White.With(Red) | ||
if actual != expected { | ||
t.Errorf("%s != %s", actual, expected) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package color | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
) | ||
|
||
type escCode string | ||
|
||
const reset escCode = "\x1b[0m" | ||
|
||
func (c escCode) Fprint(w io.Writer, a ...any) { | ||
c.fset(w) | ||
fmt.Fprint(w, a...) | ||
reset.fset(w) | ||
} | ||
|
||
func (c escCode) Fprintf(w io.Writer, format string, a ...any) { | ||
c.fset(w) | ||
fmt.Fprintf(w, format, a...) | ||
reset.fset(w) | ||
} | ||
|
||
func (c escCode) Fprintln(w io.Writer, a ...any) { | ||
c.fset(w) | ||
fmt.Fprint(w, a...) | ||
reset.fset(w) | ||
fmt.Print("\n") | ||
} | ||
|
||
func (c escCode) Print(a ...any) { | ||
c.set() | ||
fmt.Print(a...) | ||
reset.set() | ||
} | ||
|
||
func (c escCode) Printf(format string, a ...any) { | ||
c.set() | ||
fmt.Printf(format, a...) | ||
reset.set() | ||
} | ||
|
||
func (c escCode) Println(a ...any) { | ||
c.set() | ||
fmt.Print(a...) | ||
reset.set() | ||
fmt.Print("\n") | ||
} | ||
|
||
func (c escCode) Paint(a any) string { | ||
return fmt.Sprintf("%s%v%s", c, a, reset) | ||
} | ||
|
||
func (c escCode) set() { | ||
fmt.Print(c) | ||
} | ||
|
||
func (c escCode) fset(w io.Writer) { | ||
fmt.Fprint(w, c) | ||
} | ||
|
||
func makeEscCode(typeCode typeCode, cocolorCode colorCode) escCode { | ||
return escCode(fmt.Sprintf("\x1b[%d;5;%d;m", typeCode, cocolorCode)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package color | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestMakeEscCode(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
typeCode typeCode | ||
colorCode colorCode | ||
expected escCode | ||
}{ | ||
{ | ||
name: "fg", | ||
typeCode: fg, | ||
colorCode: White, | ||
expected: "\x1b[38;5;7;m", | ||
}, | ||
{ | ||
name: "bg", | ||
typeCode: bg, | ||
colorCode: Red, | ||
expected: "\x1b[48;5;1;m", | ||
}, | ||
} | ||
for _, test := range tests { | ||
t.Run(test.name, func(tt *testing.T) { | ||
actual := makeEscCode(test.typeCode, test.colorCode) | ||
if actual != test.expected { | ||
tt.Errorf("%s != %s", actual, test.expected) | ||
} | ||
}) | ||
} | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package color | ||
|
||
type typeCode int | ||
|
||
const ( | ||
fg typeCode = 38 | ||
bg typeCode = 48 | ||
) |