Some checks are pending
Docs Deploy / build_and_deploy (push) Waiting to run
Generate Docs / cli (push) Waiting to run
Generate Config Doc / cli (push) Waiting to run
Go formatting / go-formatting (push) Waiting to run
Check links / markdown-link-check (push) Waiting to run
Integration / pre-test (push) Waiting to run
Integration / test on (push) Blocked by required conditions
Integration / status (push) Blocked by required conditions
Lint / Lint Go code (push) Waiting to run
Test / test (ubuntu-latest) (push) Waiting to run
88 lines
1.8 KiB
Go
88 lines
1.8 KiB
Go
// Package prefixgen is a prefix generation helper for log messages
|
|
// and any other kind.
|
|
package prefixgen
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
|
|
"github.com/ignite/cli/v29/ignite/pkg/cliui/colors"
|
|
)
|
|
|
|
// Prefixer generates prefixes.
|
|
type Prefixer struct {
|
|
format string
|
|
color string
|
|
left, right string
|
|
convertUppercase bool
|
|
}
|
|
|
|
// Option configures Prefixer.
|
|
type Option func(p *Prefixer)
|
|
|
|
// Color sets color to the prefix.
|
|
func Color(color string) Option {
|
|
return func(p *Prefixer) {
|
|
p.color = color
|
|
}
|
|
}
|
|
|
|
// SquareBrackets adds square brackets to the prefix.
|
|
func SquareBrackets() Option {
|
|
return func(p *Prefixer) {
|
|
p.left = "["
|
|
p.right = "]"
|
|
}
|
|
}
|
|
|
|
// SpaceRight adds rights space to the prefix.
|
|
func SpaceRight() Option {
|
|
return func(p *Prefixer) {
|
|
p.right += " "
|
|
}
|
|
}
|
|
|
|
// Uppercase formats the prefix to uppercase.
|
|
func Uppercase() Option {
|
|
return func(p *Prefixer) {
|
|
p.convertUppercase = true
|
|
}
|
|
}
|
|
|
|
// Common holds some common prefix options and extends those
|
|
// options by given options.
|
|
func Common(options ...Option) []Option {
|
|
return append([]Option{
|
|
SquareBrackets(),
|
|
SpaceRight(),
|
|
Uppercase(),
|
|
}, options...)
|
|
}
|
|
|
|
// New creates a new Prefixer with format and options.
|
|
// Format is an fmt.Sprintf() like format to dynamically create prefix texts
|
|
// as needed.
|
|
func New(format string, options ...Option) *Prefixer {
|
|
p := &Prefixer{
|
|
format: format,
|
|
}
|
|
for _, o := range options {
|
|
o(p)
|
|
}
|
|
return p
|
|
}
|
|
|
|
// Gen generates a new prefix by applying s to format given during New().
|
|
func (p *Prefixer) Gen(s ...interface{}) string {
|
|
format := p.format
|
|
format = p.left + format
|
|
format += p.right
|
|
prefix := fmt.Sprintf(format, s...)
|
|
if p.convertUppercase {
|
|
prefix = strings.ToUpper(prefix)
|
|
}
|
|
if p.color != "" {
|
|
return colors.SprintFunc(p.color)(prefix)
|
|
}
|
|
return prefix
|
|
}
|