45 lines
1.1 KiB
Markdown
45 lines
1.1 KiB
Markdown
|
# Color
|
||
|
|
||
|
Beaver comes with the `color` sub-package that can be used even without Beaver calls.
|
||
|
|
||
|
## Formatting a string with a single attribute
|
||
|
```go
|
||
|
text := color.FgRed.Format("red")
|
||
|
```
|
||
|
|
||
|
## Formatting a string with a full color
|
||
|
```go
|
||
|
text := color.New(color.BgGreen, color.FgRed, color.Bold).Format("green background, red text, and bold")
|
||
|
```
|
||
|
|
||
|
## Formatting strings with multiple colors
|
||
|
The following are different ways to print `This is a color test!`
|
||
|
where the word `color` is red while everything else is `green`.
|
||
|
|
||
|
### setup
|
||
|
Assuming each example is preceded by
|
||
|
```go
|
||
|
green := color.New(color.FgGreen)
|
||
|
red := color.New(color.FgRed)
|
||
|
```
|
||
|
|
||
|
#### pure `fmt.Println`
|
||
|
```go
|
||
|
fmt.Println(green.Format("This is a"), red.Format("color"), green.Format("test!"))
|
||
|
```
|
||
|
|
||
|
#### `color.Wrap`
|
||
|
```go
|
||
|
fmt.Println(green.Wrap("This is a #{color} test!", red))
|
||
|
```
|
||
|
|
||
|
#### string formatting with `fmt.Printf` and `color.Wrap`
|
||
|
```go
|
||
|
fmt.Printf(green.Wrap("This is a #{%s} test!\n", red), "color")
|
||
|
```
|
||
|
|
||
|
#### using a different directive prefix
|
||
|
```go
|
||
|
color.DirectivePrefix('$')
|
||
|
fmt.Printf(green.Wrap("This is a ${%s} test!\n", red), "color")
|
||
|
```
|