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

table format error on windows after a automerge column #337

Closed
sceneryback opened this issue Oct 16, 2024 · 18 comments
Closed

table format error on windows after a automerge column #337

sceneryback opened this issue Oct 16, 2024 · 18 comments
Labels
bug Something isn't working

Comments

@sceneryback
Copy link

Describe the bug
columns after a automerge column overflows when rendering table only on windows

To Reproduce
render table with auto merge columns

Expected behavior
should not overflow, like that on macos:
image

Screenshots
image

Software (please complete the following information):

  • OS: windows 10
  • GoLang Version go1.22.3 windows/amd64

Additional context
Add any other context about the problem here.

@sceneryback
Copy link
Author

@jedib0t

@jedib0t
Copy link
Owner

jedib0t commented Oct 16, 2024

Can you please leave sample code to reproduce this issue?

@sceneryback
Copy link
Author

hi @jedib0t try this sample code. Compile the code to windows/amd64, then run the binary inside a windows command:

package main

import (
	"fmt"

	"github.com/jedib0t/go-pretty/v6/table"
	"github.com/jedib0t/go-pretty/v6/text"
)

func main() {

	t := table.NewWriter()

	firstHeader := []interface{}{"c1", "c2", "c2", "c3"}
	secondHeader := []interface{}{"c1", "c21", "c22", "c3"}

	t.AppendHeader(table.Row(firstHeader), table.RowConfig{AutoMerge: true})
	t.AppendHeader(table.Row(secondHeader))

	var columns = []interface{}{"foo", "bar", "baz", "qux"}

	t.AppendRow(table.Row(columns))

	cfgs := []table.ColumnConfig{
		{Name: "c1", AutoMerge: true, AlignHeader: text.AlignCenter, Align: text.AlignCenter},
		{Name: "c3", AutoMerge: true, AlignHeader: text.AlignCenter, Align: text.AlignCenter, VAlignHeader: text.VAlignMiddle},
	}

	t.SetColumnConfigs(cfgs)

	t.SetStyle(table.StyleLight)
	t.Style().Options.SeparateRows = true
	t.Style().Options.SeparateColumns = true

	fmt.Println(t.Render())

}

@jedib0t
Copy link
Owner

jedib0t commented Oct 16, 2024

I don't see any issues. It could be the Asian characters you are using and the font you've chosen for your terminal.

Windows CMD.exe:
image

Windows PowerShell:
image

Linux:
image

@jedib0t
Copy link
Owner

jedib0t commented Oct 16, 2024

Try this workaround:

go-pretty/text/string.go

Lines 78 to 93 in 730bce7

// OverrideRuneWidthEastAsianWidth can *probably* help with alignment, and
// length calculation issues when dealing with Unicode character-set and a
// non-English language set in the LANG variable.
//
// Set this to 'false' to force the "runewidth" library to pretend to deal with
// English character-set. Be warned that if the text/content you are dealing
// with contains East Asian character-set, this may result in unexpected
// behavior.
//
// References:
// * https://github.com/mattn/go-runewidth/issues/64#issuecomment-1221642154
// * https://github.com/jedib0t/go-pretty/issues/220
// * https://github.com/jedib0t/go-pretty/issues/204
func OverrideRuneWidthEastAsianWidth(val bool) {
rwCondition.EastAsianWidth = val
}

You will have to invoke text.OverrideRuneWidthEastAsianWidth(true) once. After that the alignment issues may be fixed with subsequent Render() calls. There is no 100% working solve as of now.

@sceneryback
Copy link
Author

sceneryback commented Oct 17, 2024

hi @jedib0t On my windows, it still exists without any asian characters. And it is not fixed after calling text.OverrideRuneWidthEastAsianWidth(true):
image

Any other information you need to debug the issue?

@sceneryback
Copy link
Author

My windows terminal version:
image
My font config for windows terminal:
image

@jedib0t
Copy link
Owner

jedib0t commented Oct 17, 2024

Can you share the raw output of the following and a screenshot of the same:

package main

import (
	"fmt"
	"strings"

	"github.com/jedib0t/go-pretty/v6/table"
	"github.com/jedib0t/go-pretty/v6/text"
)

func main() {
	firstHeader := []interface{}{"c1", "c2", "c2", "c3"}
	secondHeader := []interface{}{"c1", "c21", "c22", "c3"}
	row1 := []interface{}{"foo", "bar", "baz", "qux"}
	colConfigs := []table.ColumnConfig{
		{Name: "c1", AutoMerge: true, AlignHeader: text.AlignCenter, Align: text.AlignCenter},
		{Name: "c3", AutoMerge: true, AlignHeader: text.AlignCenter, Align: text.AlignCenter, VAlignHeader: text.VAlignMiddle},
	}

	t := table.NewWriter()
	t.AppendHeader(firstHeader, table.RowConfig{AutoMerge: true})
	t.AppendHeader(secondHeader)
	t.AppendRow(row1)
	t.SetColumnConfigs(colConfigs)
	t.SetStyle(table.StyleLight)
	t.Style().Options.SeparateRows = true
	t.Style().Options.SeparateColumns = true
	out := t.Render()

	fmt.Println(out)
	for _, line := range strings.Split(out, "\n") {
		for _, char := range line {
			fmt.Printf(text.FgWhite.Sprintf("%4d ", char))
		}
		fmt.Println()
	}
	for _, line := range strings.Split(out, "\n") {
		for _, char := range line {
			fmt.Printf(text.FgHiBlue.Sprintf("%4c ", char))
		}
		fmt.Println()
	}
	for _, line := range strings.Split(out, "\n") {
		for _, char := range line {
			width := text.RuneWidth(char)
			color := text.FgGreen
			if width > 1 {
				color = text.FgRed
			}
			fmt.Printf(color.Sprintf("%4d ", width))
		}
		fmt.Println()
	}
}

@sceneryback
Copy link
Author

@jedib0t sure, this is my output:
image

@sceneryback
Copy link
Author

┌─────┬───────────┬─────┐
│  C1 │     C2     │  C3 │
│     ├─────┬─────┤     │
│     │ C21 │ C22 │     │
├─────┼─────┼─────┼─────┤
│ foo │ bar │ baz │ qux │
└─────┴─────┴─────┴─────┘
9484 9472 9472 9472 9472 9472 9516 9472 9472 9472 9472 9472 9472 9472 9472 9472 9472 9472 9516 9472 9472 9472 9472 9472 9488
9474   32   32   67   49   32 9474   32   32   32   32   32   67   50   32   32   32   32   32 9474   32   32   67   51   32 9474
9474   32   32   32   32   32 9500 9472 9472 9472 9472 9472 9516 9472 9472 9472 9472 9472 9508   32   32   32   32   32 9474
9474   32   32   32   32   32 9474   32   67   50   49   32 9474   32   67   50   50   32 9474   32   32   32   32   32 9474
9500 9472 9472 9472 9472 9472 9532 9472 9472 9472 9472 9472 9532 9472 9472 9472 9472 9472 9532 9472 9472 9472 9472 9472 9508
9474   32  102  111  111   32 9474   32   98   97  114   32 9474   32   98   97  122   32 9474   32  113  117  120   32 9474
9492 9472 9472 9472 9472 9472 9524 9472 9472 9472 9472 9472 9524 9472 9472 9472 9472 9472 9524 9472 9472 9472 9472 9472 9496
   ┌    ─    ─    ─    ─    ─    ┬    ─    ─    ─    ─    ─    ─    ─    ─    ─    ─    ─    ┬    ─    ─    ─    ─    ─    ┐
   │              C    1         │                             C    2                             │              C    3         │
   │                             ├    ─    ─    ─    ─    ─    ┬    ─    ─    ─    ─    ─    ┤                             │
   │                             │         C    2    1         │         C    2    2         │                             │
   ├    ─    ─    ─    ─    ─    ┼    ─    ─    ─    ─    ─    ┼    ─    ─    ─    ─    ─    ┼    ─    ─    ─    ─    ─    ┤
   │         f    o    o         │         b    a    r         │         b    a    z         │         q    u    x         │
   └    ─    ─    ─    ─    ─    ┴    ─    ─    ─    ─    ─    ┴    ─    ─    ─    ─    ─    ┴    ─    ─    ─    ─    ─    ┘
   2    2    2    2    2    2    2    2    2    2    2    2    2    2    2    2    2    2    2    2    2    2    2    2    2
   2    1    1    1    1    1    2    1    1    1    1    1    1    1    1    1    1    1    1    2    1    1    1    1    1    2
   2    1    1    1    1    1    2    2    2    2    2    2    2    2    2    2    2    2    2    1    1    1    1    1    2
   2    1    1    1    1    1    2    1    1    1    1    1    2    1    1    1    1    1    2    1    1    1    1    1    2
   2    2    2    2    2    2    2    2    2    2    2    2    2    2    2    2    2    2    2    2    2    2    2    2    2
   2    1    1    1    1    1    2    1    1    1    1    1    2    1    1    1    1    1    2    1    1    1    1    1    2
   2    2    2    2    2    2    2    2    2    2    2    2    2    2    2    2    2    2    2    2    2    2    2    2    2

@jedib0t
Copy link
Owner

jedib0t commented Oct 17, 2024

Was able to reproduce the issue with LANG=zh_CN.UTF-8 set in the environment. I'm guessing your environment has this set. This is the same issue as reported in #220 - I don't think this is an issue I can fix in my library, but I'll try.

@sceneryback
Copy link
Author

Thanks, look forward to your fix

@jedib0t
Copy link
Owner

jedib0t commented Oct 18, 2024

Hey @sceneryback I've pushed a fix to the branch fix-337. Can you try it and let me know? (use the code from that branch instead of the latest release)

@sceneryback
Copy link
Author

hi @jedib0t thank you very much! it seems fixed, look forward to seeing this in main branch as soon as possible
linux:
image
windows cmd.exe:
image
windows powershell:
image

I don't have a macbook by hand, if more tests passed, happy to see this fix merged :)

@sceneryback
Copy link
Author

but if I have Chinese characters in column, it renders empty line
image

@sceneryback
Copy link
Author

raw output is:

┌─────┬───────────┬──────┐
│  C1 │     C2    │ 中文 │
│     ├─────┬─────┼──────┤
│     │ C21 │ C22 │  C3  │
├─────┼─────┼─────┼──────┤
│ foo │ bar │ baz │  qux │
└─────┴─────┴─────┴──────┘
9484 9472 9472 9472 9472 9472 9516 9472 9472 9472 9472 9472 9472 9472 9472 9472 9472 9472 9516 9472 9472 9472 9472 9472 9472 9488
9474   32   32   67   49   32 9474   32   32   32   32   32   67   50   32   32   32   32 9474   32 20013 25991   32 9474
9474   32   32   32   32   32 9500 9472 9472 9472 9472 9472 9516 9472 9472 9472 9472 9472 9532 9472 9472 9472 9472 9472 9472 9508
9474   32   32   32   32   32 9474   32   67   50   49   32 9474   32   67   50   50   32 9474   32   32   67   51   32   32 9474
9500 9472 9472 9472 9472 9472 9532 9472 9472 9472 9472 9472 9532 9472 9472 9472 9472 9472 9532 9472 9472 9472 9472 9472 9472 9508
9474   32  102  111  111   32 9474   32   98   97  114   32 9474   32   98   97  122   32 9474   32   32  113  117  120   32 9474
9492 9472 9472 9472 9472 9472 9524 9472 9472 9472 9472 9472 9524 9472 9472 9472 9472 9472 9524 9472 9472 9472 9472 9472 9472 9496
   ┌    ─    ─    ─    ─    ─    ┬    ─    ─    ─    ─    ─    ─    ─    ─    ─    ─    ─    ┬    ─    ─    ─    ─    ─    ─    ┐
   │              C    1         │                             C    2                        │         中    文         │
   │                             ├    ─    ─    ─    ─    ─    ┬    ─    ─    ─    ─    ─    ┼    ─    ─    ─    ─    ─    ─    ┤
   │                             │         C    2    1         │         C    2    2         │              C    3              │
   ├    ─    ─    ─    ─    ─    ┼    ─    ─    ─    ─    ─    ┼    ─    ─    ─    ─    ─    ┼    ─    ─    ─    ─    ─    ─    ┤
   │         f    o    o         │         b    a    r         │         b    a    z         │              q    u    x         │
   └    ─    ─    ─    ─    ─    ┴    ─    ─    ─    ─    ─    ┴    ─    ─    ─    ─    ─    ┴    ─    ─    ─    ─    ─    ─    ┘
   2    2    2    2    2    2    2    2    2    2    2    2    2    2    2    2    2    2    2    2    2    2    2    2    2    2
   2    1    1    1    1    1    2    1    1    1    1    1    1    1    1    1    1    1    2    1    2    2    1    2
   2    1    1    1    1    1    2    2    2    2    2    2    2    2    2    2    2    2    2    2    2    2    2    2    2    2
   2    1    1    1    1    1    2    1    1    1    1    1    2    1    1    1    1    1    2    1    1    1    1    1    1    2
   2    2    2    2    2    2    2    2    2    2    2    2    2    2    2    2    2    2    2    2    2    2    2    2    2    2
   2    1    1    1    1    1    2    1    1    1    1    1    2    1    1    1    1    1    2    1    1    1    1    1    1    2
   2    2    2    2    2    2    2    2    2    2    2    2    2    2    2    2    2    2    2    2    2    2    2    2    2    2

@jedib0t
Copy link
Owner

jedib0t commented Oct 21, 2024

Hey. The vertical breaks have nothing to do with this library. It is most probably your terminal and font than anything else. The asian characters are most probably taller than all the other characters on that line, and is causing your terminal to display those gaps between the box drawing characters.

@jedib0t jedib0t added the bug Something isn't working label Oct 21, 2024
@jedib0t
Copy link
Owner

jedib0t commented Oct 21, 2024

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

No branches or pull requests

2 participants