Skip to content

Style.Sprintf and RGBStyle.Sprintf behave differently #620

@harenber

Description

@harenber

I'm just learning to use your library, so please excuse me if it is written somewhere, but I couldn't find anything.

First: thanks for providing this fine library and all the useful documentation (and demos!)!

Starting to use it, I got confused. I used this piece of code:

func p(a *pterm.AreaPrinter) {

	foregroundRGB := pterm.RGB{R: 0, G: 0, B: 0}
	backgroundRGB := pterm.RGB{R: 255, G: 0, B: 0}
	aRGBStyle := pterm.NewRGBStyle(foregroundRGB, backgroundRGB)
	aStyle := pterm.NewStyle(pterm.FgBlack,pterm.BgLightRed)
	for i := 0; i < 5; i++ {
		if i%2 == 0 {
			a.Update(aStyle.Sprintf("Current count: %d\n",i))
		} else {
			a.Update(aRGBStyle.Sprintf("Current count: %d\n",i))			
		}
		time.Sleep(time.Second)
	}
}
  • the astyle.Sprintf behave like I would expect it to behave (and consistently with fmt.Sprintf): it outputs the string with the style applied.
  • the aRGBStyle.Sprintf however, complains about a format mismatch: Current count: %!d(string=1) and even if you change the %d into a %s (which makes no sense?), only the value of i is printed in the style. "Current count: " is still the default (white on black in my case). Only if you do a aRGBSyle.Sprintf("%s\n", "Current count: ", i) (with only the %s in the format, not %s%d as one might expect) I got what I expected.

As said: maybe it's written somewhere, but I couldn't find the difference being mentioned in the documentation and at least IMHO it would be easier if these two Sprintf calls would behave consistently.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions