Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
ermanimer committed Oct 6, 2023
1 parent 3d77e6c commit b535b33
Show file tree
Hide file tree
Showing 8 changed files with 221 additions and 1 deletion.
41 changes: 40 additions & 1 deletion README.md
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)
63 changes: 63 additions & 0 deletions color.go
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)))
}
11 changes: 11 additions & 0 deletions color_test.go
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)
}
}
64 changes: 64 additions & 0 deletions esc.go
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))
}
35 changes: 35 additions & 0 deletions esc_test.go
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)
}
})
}
}
Binary file added images/color_codes.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/output.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 8 additions & 0 deletions type.go
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
)

0 comments on commit b535b33

Please sign in to comment.