Skip to content

Commit

Permalink
feat: added NewLettersFromStringWithRGB
Browse files Browse the repository at this point in the history
  • Loading branch information
floaust committed Mar 9, 2022
1 parent 8a3c436 commit 442a52f
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
5 changes: 5 additions & 0 deletions _examples/bigtext/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,9 @@ func main() {
pterm.NewLettersFromStringWithStyle("P", pterm.NewStyle(pterm.FgCyan)),
pterm.NewLettersFromStringWithStyle("Term", pterm.NewStyle(pterm.FgLightMagenta))).
Render()

// NewLettersFromStringWithRGB can be used to create a large text with a specific RGB color.
pterm.DefaultBigText.WithLetters(
pterm.NewLettersFromStringWithRGB("PTerm", pterm.NewRGB(255, 215, 0))).
Render()
}
31 changes: 30 additions & 1 deletion bigtext_printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,27 @@ func NewLettersFromStringWithStyle(text string, style *Style) Letters {
return l
}

// NewLettersFromStringWithRGB creates a Letters object from a string and applies an RGB color to it.
func NewLettersFromStringWithRGB(text string, rgb RGB) Letters {
s := strings.Split(text, "")
l := Letters{}

for _, s2 := range s {
l = append(l, Letter{
String: s2,
Style: &Style{},
RGB: rgb,
})
}

return l
}

// Letter is an object, which holds a string and a specific Style for it.
type Letter struct {
String string
Style *Style
RGB RGB
}

// WithStyle returns a new Letter with a specific Style.
Expand All @@ -44,6 +61,12 @@ func (l Letter) WithStyle(style *Style) *Letter {
return &l
}

// WithRGB returns a new Letter with a specific RGB color.
func (l Letter) WithRGB(rgb RGB) *Letter {
l.RGB = rgb
return &l
}

// WithString returns a new Letter with a specific String.
func (l Letter) WithString(s string) *Letter {
l.String = s
Expand Down Expand Up @@ -91,6 +114,7 @@ func (p BigTextPrinter) Srender() (string, error) {
bigLetters = append(bigLetters, Letter{
String: val,
Style: l.Style,
RGB: l.RGB,
})
}
}
Expand All @@ -116,7 +140,12 @@ func (p BigTextPrinter) Srender() (string, error) {
if letterLineLength < maxLetterWidth {
letterLine += strings.Repeat(" ", maxLetterWidth-letterLineLength)
}
ret += letter.Style.Sprint(letterLine)

if letter.RGB != (RGB{}) {
ret += letter.RGB.Sprint(letterLine)
} else {
ret += letter.Style.Sprint(letterLine)
}
}
ret += "\n"
}
Expand Down

0 comments on commit 442a52f

Please sign in to comment.