mirror of
https://github.com/fatih/color.git
synced 2025-02-16 18:34:39 +02:00
Initial commit, WIP
This commit is contained in:
commit
636dfd47b1
134
color.go
Normal file
134
color.go
Normal file
@ -0,0 +1,134 @@
|
||||
package color
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type Color struct {
|
||||
params []Parameter
|
||||
}
|
||||
|
||||
// Parameter defines a single SGR Code
|
||||
type Parameter int
|
||||
|
||||
const (
|
||||
FgBlack Parameter = iota + 30
|
||||
FgRed
|
||||
FgGreen
|
||||
FgYellow
|
||||
FgBlue
|
||||
FgMagent
|
||||
FgCyan
|
||||
FgWhite
|
||||
)
|
||||
|
||||
const (
|
||||
BgBlack Parameter = iota + 40
|
||||
BgRed
|
||||
BgGreen
|
||||
BgYellow
|
||||
BgBlue
|
||||
BgMagent
|
||||
BgCyan
|
||||
BgWhite
|
||||
)
|
||||
|
||||
const (
|
||||
Reset Parameter = iota
|
||||
Bold
|
||||
Faint
|
||||
Italic
|
||||
Underline
|
||||
BlinkSlow
|
||||
BlinkRapid
|
||||
ReverseVideo
|
||||
Concealed
|
||||
CrossedOut
|
||||
)
|
||||
|
||||
var (
|
||||
Black = &Color{params: []Parameter{FgBlack}}
|
||||
Red = &Color{params: []Parameter{FgRed}}
|
||||
Green = &Color{params: []Parameter{FgGreen}}
|
||||
Yellow = &Color{params: []Parameter{FgYellow}}
|
||||
Blue = &Color{params: []Parameter{FgBlue}}
|
||||
Magent = &Color{params: []Parameter{FgMagent}}
|
||||
Cyan = &Color{params: []Parameter{FgCyan}}
|
||||
White = &Color{params: []Parameter{FgWhite}}
|
||||
)
|
||||
|
||||
func New(value ...Parameter) *Color {
|
||||
c := &Color{params: make([]Parameter, 0)}
|
||||
c.Add(value...)
|
||||
return c
|
||||
}
|
||||
|
||||
func (c *Color) Bold() *Color {
|
||||
c.Add(Bold)
|
||||
return c
|
||||
}
|
||||
|
||||
func (c *Color) Add(value ...Parameter) *Color {
|
||||
c.params = append(c.params, value...)
|
||||
return c
|
||||
}
|
||||
|
||||
func (c *Color) prepend(value Parameter) {
|
||||
c.params = append(c.params, 0)
|
||||
copy(c.params[1:], c.params[0:])
|
||||
c.params[0] = value
|
||||
}
|
||||
|
||||
// Printf formats according to a format specifier and writes to standard output.
|
||||
// It returns the number of bytes written and any write error encountered.
|
||||
func (c *Color) Printf(format string, a ...interface{}) (n int, err error) {
|
||||
c.Set()
|
||||
defer Unset()
|
||||
|
||||
return fmt.Printf(format, a...)
|
||||
}
|
||||
|
||||
// Print formats using the default formats for its operands and writes to
|
||||
// standard output. Spaces are added between operands when neither is a
|
||||
// string. It returns the number of bytes written and any write error
|
||||
// encountered.
|
||||
func (c *Color) Print(a ...interface{}) (n int, err error) {
|
||||
c.Set()
|
||||
defer Unset()
|
||||
|
||||
return fmt.Print(a...)
|
||||
}
|
||||
|
||||
// Println formats using the default formats for its operands and writes to
|
||||
// standard output. Spaces are always added between operands and a newline is
|
||||
// appended. It returns the number of bytes written and any write error
|
||||
// encountered.
|
||||
func (c *Color) Println(a ...interface{}) (n int, err error) {
|
||||
c.Set()
|
||||
defer Unset()
|
||||
|
||||
return fmt.Println(a...)
|
||||
}
|
||||
|
||||
// sequence returns a formated SGR sequence to be plugged into a "\033[...m"
|
||||
// an example output might be: "1;36" -> bold cyan
|
||||
func (c *Color) sequence() string {
|
||||
format := make([]string, len(c.params))
|
||||
for i, v := range c.params {
|
||||
format[i] = strconv.Itoa(int(v))
|
||||
}
|
||||
|
||||
return strings.Join(format, ";")
|
||||
}
|
||||
|
||||
// Set sets the SGR sequence.
|
||||
func (c *Color) Set() {
|
||||
fmt.Printf("\033[%sm", c.sequence())
|
||||
}
|
||||
|
||||
func Unset() {
|
||||
fmt.Print("\033[0m")
|
||||
}
|
31
color_test.go
Normal file
31
color_test.go
Normal file
@ -0,0 +1,31 @@
|
||||
package color
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestColor(t *testing.T) {
|
||||
go func() {
|
||||
scanner := bufio.NewScanner(os.Stdout)
|
||||
for scanner.Scan() {
|
||||
fmt.Println(scanner.Text()) // Println will add back the final '\n'
|
||||
}
|
||||
}()
|
||||
|
||||
time.Sleep(time.Millisecond * 300)
|
||||
|
||||
c := Cyan.Bold()
|
||||
c.Println("ankara")
|
||||
|
||||
// New(FgGreen, Bold).Begin()
|
||||
// fmt.Println("san francisco")
|
||||
// End()
|
||||
|
||||
// Output:
|
||||
// ankara
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user