termbox-go icon indicating copy to clipboard operation
termbox-go copied to clipboard

Chinese character width occupation problem

Open 404name opened this issue 2 years ago • 3 comments

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 avatar Jan 03 '23 06:01 404name

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 avatar Jan 03 '23 07:01 404name

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

  • GBK:There is no problem
  • UTF-8:just Delete lines 130 to 134 `https://github.com/nsf/termbox-go/blob/2ff630277754813b198ae96036e28e254d2c72bf/api_windows.go``
  • ABOUT:Doing so may cause problems on mac or other platforms? No test, only for win platform

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

404name avatar Jan 03 '23 10:01 404name

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)

ydzhou avatar Jan 09 '24 07:01 ydzhou