pterm / pterm

✨ #PTerm is a modern Go module to easily beautify console output. Featuring charts, progressbars, tables, trees, text input, select menus and much more 🚀 It's completely configurable and 100% cross-platform compatible.

Home Page:https://pterm.sh

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Style.Sprintf and RGBStyle.Sprintf behave differently

harenber opened this issue · comments

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.

Thanks for reporting the issue (and thanks for the compliment)! I'll fix it. You're right, they should both behave the same.

Fixed in #622. Will be merged and released as soon as the CI is done 🚀