package colors import ( "fmt" "os" "github.com/gleisonmv/colors" "golang.org/x/term" ) type Color int64 const ( FgBlue Color = iota FgBlueBold FgRed BgBlue BgGreen FgRedBold BgRed FgGreenBold BgYellow FgYellow FgYellowBold BgBrightBlack ) // Text generates a string with args using sprintf and gleisonmv/colors. func Text(text string, color Color, args ...any) string { if len(args) > 0 { text = formatText(text, args...) } switch color { case FgBlue: return colors.Text(text).FgBlue().String() case FgBlueBold: return colors.Text(text).FgBlue().Bold().String() case FgRed: return colors.Text(text).FgRed().String() case BgBlue: return colors.Text(text).BgBlue().String() case BgGreen: return colors.Text(text).BgGreen().String() case FgRedBold: return colors.Text(text).FgRed().Bold().String() case BgRed: return colors.Text(text).BgRed().String() case FgGreenBold: return colors.Text(text).FgGreen().Bold().String() case BgYellow: return colors.Text(text).FgBlack().BgYellow().String() case FgYellow: return colors.Text(text).FgYellow().String() case FgYellowBold: return colors.Text(text).FgYellow().Bold().String() case BgBrightBlack: return colors.Text(text).BgBrightBlack().String() } return text } // BufferedText returns a colored string with length of terminal width buffered with spaces. func BufferedText(text string, color Color, args ...any) string { if len(args) > 0 { text = formatText(text, args...) } termWidth, _, err := term.GetSize(int(os.Stdout.Fd())) if err != nil { termWidth = len(text) } // Right pad text to width of terminal using spaces text = fmt.Sprintf("%- *s", termWidth, text) return Text(text, color) } func formatText(text string, args ...any) string { return fmt.Sprintf(text, args...) }