feat(opts): use style options

This commit is contained in:
Ayman Bagabas 2022-11-18 15:53:06 -05:00
parent abee24064a
commit 60d090659b

View File

@ -75,20 +75,34 @@ const (
// A set of properties. // A set of properties.
type rules map[propKey]interface{} type rules map[propKey]interface{}
// NewStyle returns a new, empty Style. While it's syntactic sugar for the // StyleOption is a function that applies a style option to a Style.
// Style{} primitive, it's recommended to use this function for creating styles type StyleOption func(*Style)
// in case the underlying implementation changes. It takes an optional string
// value to be set as the underlying string value for this style. // WithString sets the underlying string value for this style.
func NewStyle(strs ...string) Style { func WithString(strs ...string) StyleOption {
return renderer.NewStyle(strs...) return func(s *Style) {
s.value = joinString(strs...)
}
} }
// NewStyle returns a new, empty Style. While it's syntactic sugar for the // NewStyle returns a new, empty Style. While it's syntactic sugar for the
// Style{} primitive, it's recommended to use this function for creating styles // Style{} primitive, it's recommended to use this function for creating styles
// in case the underlying implementation changes. It takes an optional string // in case the underlying implementation changes. It takes an optional string
// value to be set as the underlying string value for this style. // value to be set as the underlying string value for this style.
func (r *Renderer) NewStyle(strs ...string) Style { func NewStyle(opts ...StyleOption) Style {
return Style{r: r}.SetString(strs...) return renderer.NewStyle(opts...)
}
// NewStyle returns a new, empty Style. While it's syntactic sugar for the
// Style{} primitive, it's recommended to use this function for creating styles
// in case the underlying implementation changes. It takes an optional string
// value to be set as the underlying string value for this style.
func (r *Renderer) NewStyle(opts ...StyleOption) Style {
s := Style{r: r}
for _, opt := range opts {
opt(&s)
}
return s
} }
// Style contains a set of rules that comprise a style as a whole. // Style contains a set of rules that comprise a style as a whole.
@ -98,13 +112,19 @@ type Style struct {
value string value string
} }
// joinString joins a list of strings into a single string separated with a
// space.
func joinString(strs ...string) string {
return strings.Join(strs, " ")
}
// SetString sets the underlying string value for this style. To render once // SetString sets the underlying string value for this style. To render once
// the underlying string is set, use the Style.String. This method is // the underlying string is set, use the Style.String. This method is
// a convenience for cases when having a stringer implementation is handy, such // a convenience for cases when having a stringer implementation is handy, such
// as when using fmt.Sprintf. You can also simply define a style and render out // as when using fmt.Sprintf. You can also simply define a style and render out
// strings directly with Style.Render. // strings directly with Style.Render.
func (s Style) SetString(strs ...string) Style { func (s Style) SetString(strs ...string) Style {
s.value = strings.Join(strs, " ") s.value = joinString(strs...)
return s return s
} }
@ -173,7 +193,7 @@ func (s Style) Render(strs ...string) string {
} }
var ( var (
str = strings.Join(strs, " ") str = joinString(strs...)
te = s.r.ColorProfile().String() te = s.r.ColorProfile().String()
teSpace = s.r.ColorProfile().String() teSpace = s.r.ColorProfile().String()