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

Text wrapping in a table breaks color configuration #330

Closed
ayala-orca opened this issue Sep 1, 2024 · 5 comments · Fixed by #333
Closed

Text wrapping in a table breaks color configuration #330

ayala-orca opened this issue Sep 1, 2024 · 5 comments · Fixed by #333
Labels
bug Something isn't working

Comments

@ayala-orca
Copy link

Describe the bug
When using the table library to format text with by setting text.WrapText and WidthMax, if the text contains both color and bold formatting, the text wrapping breaks the color configuration. Only the first line is colored as expected.

To Reproduce
Run the following code

func main() {
	columnHeaders := []interface{}{"Color only", "Color and Bold", "No color"}
	rows := []table.Row{
		{
			color.New(color.FgHiRed).Sprint("This line should be colored in red"),
			color.New(color.FgHiRed).Sprintf("%s %s", color.New(color.Bold).Sprint("Bold Title"), "This line should be colored in red"),
			"This line should be colored in white",
		},
	}

	t := table.NewWriter()
	t.SetOutputMirror(os.Stdout)
	t.Style().Options = table.Options{
		SeparateColumns: true,
		DrawBorder:      true,
	}
	t.AppendHeader(columnHeaders)
	var columnConfig []table.ColumnConfig

	for i := 0; i < len(columnHeaders); i++ {
		columnConfig = append(columnConfig, table.ColumnConfig{Number: i + 1, WidthMaxEnforcer: text.WrapText, WidthMax: 25})
	}
	t.SetColumnConfigs(columnConfig)
	t.AppendRows(rows)
	t.Render()
}

Expected behavior
The text should retain the color formatting even after being wrapped to the next line.

Screenshots
As shown in the second column - the color configuration is broken when the text wrapping occurs.
image

Software (please complete the following information):

  • GoLang Version: 1.22
@ayala-orca ayala-orca changed the title Text wrapping in a table breaks Color configuration Text wrapping in a table breaks color configuration Sep 1, 2024
@jedib0t
Copy link
Owner

jedib0t commented Sep 1, 2024

What is the color library you are using?

@jedib0t jedib0t added the bug Something isn't working label Sep 1, 2024
@ayala-orca
Copy link
Author

What is the color library you are using?

https://github.com/fatih/color

@jedib0t
Copy link
Owner

jedib0t commented Sep 1, 2024

Found the issue, and a workaround.

Issue is because fatih/color library uses \x1b0;22m to turn off Bold, and that is not a widely supported instruction, and my text library fails to parse it correctly.

Workaround: Use this:

			fmt.Sprintf("%s %s",
				color.New(color.FgHiRed, color.Bold).Sprint("Bold Title"),
				color.New(color.FgHiRed).Sprint("This line should be colored in red"),
			),

instead of:

			color.New(color.FgHiRed).Sprintf("%s %s", color.New(color.Bold).Sprint("Bold Title"), "This line should be colored in red"),

I'll see what I can do to handle this case without the workaround.

@jedib0t
Copy link
Owner

jedib0t commented Oct 3, 2024

With the fix in the commit above, the output for your code looks like this:
image

@jedib0t
Copy link
Owner

jedib0t commented Oct 3, 2024

The fix is part of tag v6.6.0 @ https://github.com/jedib0t/go-pretty/releases/tag/v6.6.0 - please let me know if you face any issues with this release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants