Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added NewLettersFromStringWithRGB #322

Merged
merged 3 commits into from Mar 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions _examples/bigtext/main.go
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
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 (overwrites style).
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 (overwrites style).
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
43 changes: 43 additions & 0 deletions bigtext_printer_test.go
Expand Up @@ -32,6 +32,22 @@ func TestBigTextPrinter_Render(t *testing.T) {
pterm.EnableStyling()
}

func TestBigTextPrinter_RenderRGB(t *testing.T) {
printer := pterm.DefaultBigText.WithLetters(pterm.NewLettersFromStringWithRGB("Hello", pterm.NewRGB(255, 0, 0)))
content := captureStdout(func(w io.Writer) {
printer.Render()
})
testza.AssertNotZero(t, content)
testza.SnapshotCreateOrValidate(t, t.Name(), content)
// DisableStyling
pterm.DisableStyling()
content = captureStdout(func(w io.Writer) {
printer.Render()
})
testza.SnapshotCreateOrValidate(t, t.Name()+"NoStyling", content)
pterm.EnableStyling()
}

func TestBigTextPrinter_RenderRawOutput(t *testing.T) {
printer := pterm.DefaultBigText.WithLetters(pterm.NewLettersFromString("Hello"))
content := captureStdout(func(w io.Writer) {
Expand Down Expand Up @@ -93,6 +109,15 @@ func TestLetter_WithStyle(t *testing.T) {
testza.AssertZero(t, p.Style)
}

func TestLetter_WithRGB(t *testing.T) {
p := pterm.Letter{}
rgb := pterm.NewRGB(0, 0, 0)
p2 := p.WithRGB(rgb)

testza.AssertEqual(t, rgb, p2.RGB)
testza.AssertZero(t, p.RGB)
}

func TestNewLettersFromText(t *testing.T) {
e := pterm.Letters{
pterm.Letter{
Expand Down Expand Up @@ -125,6 +150,24 @@ func TestNewLettersFromTextWithStyle(t *testing.T) {
testza.AssertEqual(t, e, p)
}

func TestNewLettersFromTextWithRGB(t *testing.T) {
e := pterm.Letters{
pterm.Letter{
String: "a",
Style: pterm.NewStyle(),
RGB: pterm.NewRGB(0, 0, 0),
},
pterm.Letter{
String: "b",
Style: pterm.NewStyle(),
RGB: pterm.NewRGB(0, 0, 0),
},
}
p := pterm.NewLettersFromStringWithRGB("ab", pterm.NewRGB(0, 0, 0))

testza.AssertEqual(t, e, p)
}

func TestDefaultLettersMaxHeight(t *testing.T) {
maxHeight := 5
chars := pterm.DefaultBigText.BigCharacters
Expand Down

This file was deleted.

1 change: 0 additions & 1 deletion testdata/snapshots/TestBarChartPrinter_Render.testza

This file was deleted.

This file was deleted.