Initial version
This commit is contained in:
parent
df33375505
commit
cf14f3468e
55
color.go
Normal file
55
color.go
Normal file
|
@ -0,0 +1,55 @@
|
|||
package color
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
// Whether the terminal supports true color
|
||||
var TrueColor = os.Getenv("COLORTERM") == "truecolor"
|
||||
|
||||
// Color code
|
||||
type Code int
|
||||
|
||||
// Color codes
|
||||
const (
|
||||
Black Code = iota + 30
|
||||
Red
|
||||
Green
|
||||
Yellow
|
||||
Blue
|
||||
Magenta
|
||||
Cyan
|
||||
White
|
||||
)
|
||||
|
||||
func (code Code) String(args ...any) string {
|
||||
return fmt.Sprintf("\x1b[%dm%s\x1b[0m", code, fmt.Sprint(args...))
|
||||
}
|
||||
|
||||
func (code Code) Print(args ...any) {
|
||||
if !TrueColor {
|
||||
fmt.Print(args...)
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Printf("\x1b[%dm%s\x1b[0m", code, fmt.Sprint(args...))
|
||||
}
|
||||
|
||||
func (code Code) Printf(format string, args ...any) {
|
||||
if !TrueColor {
|
||||
fmt.Printf(format, args...)
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Printf("\x1b[%dm%s\x1b[0m", code, fmt.Sprintf(format, args...))
|
||||
}
|
||||
|
||||
func (code Code) Println(args ...any) {
|
||||
if !TrueColor {
|
||||
fmt.Println(args...)
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Printf("\x1b[%dm%s\x1b[0m\n", code, fmt.Sprint(args...))
|
||||
}
|
40
tests/color_test.go
Normal file
40
tests/color_test.go
Normal file
|
@ -0,0 +1,40 @@
|
|||
package color_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
color "git.sharkk.net/Go/Color"
|
||||
)
|
||||
|
||||
func TestPrint(t *testing.T) {
|
||||
color.Black.Print("Black\n")
|
||||
color.White.Print("White\n")
|
||||
color.Red.Print("Red\n")
|
||||
color.Green.Print("Green\n")
|
||||
color.Blue.Print("Blue\n")
|
||||
color.Yellow.Print("Yellow\n")
|
||||
color.Magenta.Print("Magenta\n")
|
||||
color.Cyan.Print("Cyan\n")
|
||||
}
|
||||
|
||||
func TestPrintf(t *testing.T) {
|
||||
color.Black.Printf("%s\n", "Black")
|
||||
color.White.Printf("%s\n", "White")
|
||||
color.Red.Printf("%s\n", "Red")
|
||||
color.Green.Printf("%s\n", "Green")
|
||||
color.Blue.Printf("%s\n", "Blue")
|
||||
color.Yellow.Printf("%s\n", "Yellow")
|
||||
color.Magenta.Printf("%s\n", "Magenta")
|
||||
color.Cyan.Printf("%s\n", "Cyan")
|
||||
}
|
||||
|
||||
func TestPrintln(t *testing.T) {
|
||||
color.Black.Println("Black")
|
||||
color.White.Println("White")
|
||||
color.Red.Println("Red")
|
||||
color.Green.Println("Green")
|
||||
color.Blue.Println("Blue")
|
||||
color.Yellow.Println("Yellow")
|
||||
color.Magenta.Println("Magenta")
|
||||
color.Cyan.Println("Cyan")
|
||||
}
|
Loading…
Reference in New Issue
Block a user