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

The top border line was misaligned when title was added #220

Closed
HappyStu opened this issue Aug 19, 2022 · 7 comments
Closed

The top border line was misaligned when title was added #220

HappyStu opened this issue Aug 19, 2022 · 7 comments
Assignees
Labels
bug Something isn't working

Comments

@HappyStu
Copy link

Hello! I think this project is very nice. But there is still one small question.
When set the title of table, I find that the top border line is misaligned. For example:

┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃ Divide! ┃
┣━┳━━━━━━━━━┳━━━━━━━━━┳━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫
┃#┃FIRST NAME┃LAST NAME┃SALARY┃ ┃
┣━┳━━━━━━━━━┳━━━━━━━━━┳━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫
The style of table is table.StyleBold.

However, when the style is table.StyleDefault, the out of table is normal.

+-----------------------------------------------------------------------------+
| The style of the table is default |
+---+-----+------------+-----------+--------+-----------------------------+
| | # | FIRST NAME | LAST NAME | SALARY | |
+---+-----+------------+-----------+--------+-----------------------------+

What, may I ask, is the reason for this discrepancy?

environment: Windows, VSCode, Go 1.15

@jedib0t
Copy link
Owner

jedib0t commented Aug 21, 2022

This sounds like a bug. Lemme take a look.

@jedib0t jedib0t self-assigned this Aug 21, 2022
@jedib0t jedib0t added the bug Something isn't working label Aug 21, 2022
@jedib0t
Copy link
Owner

jedib0t commented Aug 21, 2022

I'm not able to reproduce this bug @HappyStu.

This is the code I used:

package main

import (
	"fmt"
	"os"

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

func main() {
	tw := table.NewWriter()
	tw.AppendHeader(table.Row{"#", "First Name", "Last Name", "Salary"})
	tw.AppendRows([]table.Row{
		{1, "Arya", "Stark", 3000},
		{20, "Jon", "Snow", 2000, "You know nothing, Jon Snow!"},
		{300, "Tyrion", "Lannister", 5000},
	})
	tw.SetOutputMirror(os.Stdout)

	tw.SetCaption("The style of this table is %s", table.StyleBold.Name)
	tw.SetStyle(table.StyleBold)
	tw.SetTitle("Divide!")
	tw.Render()

	fmt.Println()

	tw.SetCaption("The style of this table is %s", table.StyleDefault.Name)
	tw.SetStyle(table.StyleDefault)
	tw.SetTitle("The style of this table is %s", table.StyleDefault.Name)
	tw.Render()
}

This is the output I got:

┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃ Divide!                                                             ┃
┣━━━━━┳━━━━━━━━━━━━┳━━━━━━━━━━━┳━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫
┃   # ┃ FIRST NAME ┃ LAST NAME ┃ SALARY ┃                             ┃
┣━━━━━╋━━━━━━━━━━━━╋━━━━━━━━━━━╋━━━━━━━━╋━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫
┃   1 ┃ Arya       ┃ Stark     ┃   3000 ┃                             ┃
┃  20 ┃ Jon        ┃ Snow      ┃   2000 ┃ You know nothing, Jon Snow! ┃
┃ 300 ┃ Tyrion     ┃ Lannister ┃   5000 ┃                             ┃
┗━━━━━┻━━━━━━━━━━━━┻━━━━━━━━━━━┻━━━━━━━━┻━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
The style of this table is StyleBold

+---------------------------------------------------------------------+
| The style of this table is StyleDefault                             |
+-----+------------+-----------+--------+-----------------------------+
|   # | FIRST NAME | LAST NAME | SALARY |                             |
+-----+------------+-----------+--------+-----------------------------+
|   1 | Arya       | Stark     |   3000 |                             |
|  20 | Jon        | Snow      |   2000 | You know nothing, Jon Snow! |
| 300 | Tyrion     | Lannister |   5000 |                             |
+-----+------------+-----------+--------+-----------------------------+
The style of this table is StyleDefault

You can try it here: https://go.dev/play/p/I6uxssyXxhN?v=goprev

Can you share the code you used to test this?

@jedib0t
Copy link
Owner

jedib0t commented Aug 21, 2022

This sounds similar to the bug reported in #204 --- do you have a different LANG value?

┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃ Divide!                                                                 ┃
┣━━━━━┳━━━━━━━━━━━━┳━━━━━━━━━━━┳━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫
┃   # ┃ FIRST NAME ┃ LAST NAME ┃ SALARY ┃                             ┃
┣━━━━━╋━━━━━━━━━━━━╋━━━━━━━━━━━╋━━━━━━━━╋━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫
┃   1 ┃ Arya       ┃ Stark     ┃   3000 ┃                             ┃
┃  20 ┃ Jon        ┃ Snow      ┃   2000 ┃ You know nothing, Jon Snow! ┃
┃ 300 ┃ Tyrion     ┃ Lannister ┃   5000 ┃                             ┃
┗━━━━━┻━━━━━━━━━━━━┻━━━━━━━━━━━┻━━━━━━━━┻━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
The style of this table is StyleBold

+---------------------------------------------------------------------+
| The style of this table is StyleDefault                             |
+-----+------------+-----------+--------+-----------------------------+
|   # | FIRST NAME | LAST NAME | SALARY |                             |
+-----+------------+-----------+--------+-----------------------------+
|   1 | Arya       | Stark     |   3000 |                             |
|  20 | Jon        | Snow      |   2000 | You know nothing, Jon Snow! |
| 300 | Tyrion     | Lannister |   5000 |                             |
+-----+------------+-----------+--------+-----------------------------+
The style of this table is StyleDefault

@jedib0t
Copy link
Owner

jedib0t commented Aug 21, 2022

Found the issue. An underlying runewidth library is computing the length of the middle separator in StyleBold as 2 with LANG=zh_CN.UTF-8. When rendering the title row, this makes the title text to be padded more than necessary. At the moment, I don't have a fix for it. I'll try to follow up with the runewidth library maintainers for a solve.

@jedib0t
Copy link
Owner

jedib0t commented Aug 21, 2022

Reported this in the dependency repo: mattn/go-runewidth#64

@HappyStu
Copy link
Author

Thanks for your reply. So I think I can use a fixed symbol width, such as middle separator of 1, to circumvent the problem caused by LANG=zh_CN.UTF-8. Or use English. Thank you again!

@jedib0t
Copy link
Owner

jedib0t commented Aug 22, 2022

@HappyStu if you are not gonna have East Asian characters in your table contents, you can now use a function I've provided as a workaround for this issue: text.OverrideRuneWidthEastAsianWidth(false). Run this function once and all your go-pretty code will pretend to deal with non-asian character-set and should work without breaking alignment like you reported.

Tag: https://github.com/jedib0t/go-pretty/releases/tag/v6.3.7

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