| 1 |
package renderer |
| 2 |
|
| 3 |
import "github.com/fatih/color" |
| 4 |
|
| 5 |
// Colors is a slice of color attributes for use with fatih/color, such as color.FgWhite or color.Bold. |
| 6 |
type Colors []color.Attribute |
| 7 |
|
| 8 |
// Tint defines foreground and background color settings for table elements, with optional per-column overrides. |
| 9 |
type Tint struct { |
| 10 |
FG Colors // Foreground color attributes |
| 11 |
BG Colors // Background color attributes |
| 12 |
Columns []Tint // Per-column color settings |
| 13 |
} |
| 14 |
|
| 15 |
// Apply applies the Tint's foreground and background colors to the given text, returning the text unchanged if no colors are set. |
| 16 |
func (t Tint) Apply(text string) string { |
| 17 |
if len(t.FG) == 0 && len(t.BG) == 0 { |
| 18 |
return text |
| 19 |
} |
| 20 |
// Combine foreground and background colors |
| 21 |
combinedColors := append(t.FG, t.BG...) |
| 22 |
// Create a color function and apply it to the text |
| 23 |
c := color.New(combinedColors...).SprintFunc() |
| 24 |
return c(text) |
| 25 |
} |