mukan-ignite/ignite/pkg/cliui/prefixgen/prefixgen.go
Mukan Erkin Törük c32551b6f7
Some checks failed
Docs Deploy / build_and_deploy (push) Has been cancelled
Generate Docs / cli (push) Has been cancelled
Generate Config Doc / cli (push) Has been cancelled
Go formatting / go-formatting (push) Has been cancelled
Check links / markdown-link-check (push) Has been cancelled
Integration / pre-test (push) Has been cancelled
Integration / test on (push) Has been cancelled
Integration / status (push) Has been cancelled
Lint / Lint Go code (push) Has been cancelled
Test / test (ubuntu-latest) (push) Has been cancelled
refactor: replace all github.com upstream refs with git.cw.tr/mukan-network
2026-05-11 03:36:24 +03:00

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"
"git.cw.tr/mukan-network/mukan-ignite/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
}