Skip to content

Commit

Permalink
feat(opts): use style options
Browse files Browse the repository at this point in the history
  • Loading branch information
aymanbagabas committed Feb 9, 2023
1 parent abee240 commit 60d0906
Showing 1 changed file with 26 additions and 6 deletions.
32 changes: 26 additions & 6 deletions style.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,20 +75,34 @@ const (
// A set of properties.
type rules map[propKey]interface{}

// 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 NewStyle(strs ...string) Style {
return renderer.NewStyle(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(strs ...string) Style {
return Style{r: r}.SetString(strs...)
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.
Expand All @@ -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
}

Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 60d0906

Please sign in to comment.