diff --git a/style.go b/style.go index fa008f0..395529a 100644 --- a/style.go +++ b/style.go @@ -75,20 +75,34 @@ const ( // A set of properties. type rules map[propKey]interface{} -// 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 NewStyle(strs ...string) Style { - return renderer.NewStyle(strs...) +// StyleOption is a function that applies a style option to a Style. +type StyleOption func(*Style) + +// WithString sets the underlying string value for this style. +func WithString(strs ...string) StyleOption { + return func(s *Style) { + s.value = joinString(strs...) + } } // 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(strs ...string) Style { - return Style{r: r}.SetString(strs...) +func NewStyle(opts ...StyleOption) Style { + 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. @@ -98,13 +112,19 @@ type Style struct { 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 // the underlying string is set, use the Style.String. This method is // 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 // strings directly with Style.Render. func (s Style) SetString(strs ...string) Style { - s.value = strings.Join(strs, " ") + s.value = joinString(strs...) return s } @@ -173,7 +193,7 @@ func (s Style) Render(strs ...string) string { } var ( - str = strings.Join(strs, " ") + str = joinString(strs...) te = s.r.ColorProfile().String() teSpace = s.r.ColorProfile().String()