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

Width of Box Drawing characters and LANG=zh_CN.UTF-8 #64

Closed
jedib0t opened this issue Aug 21, 2022 · 2 comments
Closed

Width of Box Drawing characters and LANG=zh_CN.UTF-8 #64

jedib0t opened this issue Aug 21, 2022 · 2 comments

Comments

@jedib0t
Copy link

jedib0t commented Aug 21, 2022

Hello!

I maintain a golang library for drawing ASCII tables at https://github.com/jedib0t/go-pretty and this is one of the few dependencies I have, to calculate rune width for drawing the tables. Sample: https://go.dev/play/p/I6uxssyXxhN?v=goprev

Now, a couple of users reported some alignment issues, and after some investigation I figured that the Width returned for Box Drawing characters were not the expected values when LANG=zh_CN.UTF-8 or when EastAsianWidth=true is set in go-runewidth.

To replicate the bug, I create this program -- say foo.go:

package main

import (
	"fmt"
	"strings"

	"github.com/mattn/go-runewidth"
)

func main() {
	boxDrawingChars := []string{
		"+", "-", "=",
		"┏", "┳", "┓",
		"┣", "╋", "┫",
		"┗", "┻", "┛",
		"━", "┃",
	}

	cellWidth := 8
	for _, boxDrawingChar := range boxDrawingChars {
		padding := strings.Repeat(" ", cellWidth-runewidth.StringWidth(boxDrawingChar))
		fmt.Printf("| %s%s |\n", boxDrawingChar, padding)
	}
}

Output:

$ LANG=en_US.UTF-8 go run foo.go
| +        |
| -        |
| =        |
| ┏        |
| ┳        |
| ┓        |
| ┣        |
| ╋        |
| ┫        |
| ┗        |
| ┻        |
| ┛        |
| ━        |
| ┃        |

$ LANG=zh_CN.UTF-8 go run foo.go 
| +        |
| -        |
| =        |
| ┏       |
| ┳       |
| ┓       |
| ┣       |
| ╋       |
| ┫       |
| ┗       |
| ┻       |
| ┛       |
| ━       |
| ┃       |

Is this behavior right, or am I using runewidth.RuneWidth/StringWidth incorrectly?

@mattn
Copy link
Owner

mattn commented Aug 21, 2022

go-runewidth returns the value 2 for the East Asian Ambiguous Width as same as that wcwidth(3) depend on your locales. It is caused by the difference between the character width of the ruled line as represented in the terminal and the actual value returned by wcwidth(3) or runewidth. If you want it to be fixed behavior under certain conditions, change the value of the EastAsianWidth variable.

@jedib0t
Copy link
Author

jedib0t commented Aug 22, 2022

Thanks for the response. Will provide a workaround for my library's users.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants