nsf / termbox-go

Pure Go termbox implementation
http://godoc.org/github.com/nsf/termbox-go
MIT License
4.66k stars 372 forks source link

Chinese character width occupation problem #245

Open 404name opened 1 year ago

404name commented 1 year ago

https://github.com/nsf/termbox-go/blob/master/_demos/hello_world.go

When I change the hello world in the example program to Chinese, it is normal in the terminal that comes with the windows system, but in the cmd of vscode and goland, there is an extra space in the middle of the Chinese that occupies three widths (both used system terminal and utf-8 encoding). I'm very confused about this.

image

404name commented 1 year ago

gbk is correct, utf-8 will have one more space in some environments

I found the problem. When dealing with wide characters, space adaptation should be added. Under gbk, chcp 936 is normal here, and under utf-8 chcp 65001, there is one more space, and the logic of deleting and adding spaces is fine。So don't switch cmd to utf-8. (maybe it's just my local problem)

// https://github.com/nsf/termbox-go/blob/master/api_windows.go
if runewidth.RuneWidth(rune(char.char)) > 1 {
    char.attr |= common_lvb_leading_byte
    chars = append(chars, char)
    // here 
    chars = append(chars, char_info{
        char: char.char,
        attr: char.attr | common_lvb_trailing_byte,
    })
} 
404name commented 1 year ago

bug description:It seems that wide characters do not need to add spaces under UTF-8 encoding? (I verified it locally, not sure if it is the case on other platforms or other machines)

how to solve

expected:Is it possible to judge whether the current chcp is equal to 65001 (UTF-8), and consider adding spaces? [I know that there may be many encoding formats that are inconvenient to adapt, just ask questions when encountering this situation]

related environment platform:windows terminal: cmd | vscode| goland

ydzhou commented 5 months ago

I do not have this problem when using tbprint. https://github.com/ydzhou/pine/blob/f69443a87ad8c6754e305415dee930447fcdbb9f/internal/renderUtil.go#L55

My editor can print Chinese or other UTF-8 characters as expected (without any spacing in-between)