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

cancelreader.NewReader not support chinese? #393

Closed
fzdwx opened this issue Aug 10, 2022 · 10 comments · Fixed by #343
Closed

cancelreader.NewReader not support chinese? #393

fzdwx opened this issue Aug 10, 2022 · 10 comments · Fixed by #343

Comments

@fzdwx
Copy link

fzdwx commented Aug 10, 2022

When I use textinput, an error (could not decode rune) occurs when entering Chinese,i found that it is probably the following place:
image

Then I wrote a test code:

func main() {
	test(os.Stdin)

	reader, _ := cancelreader.NewReader(os.Stdin)
	test(reader)
}

func test(reader io.Reader) {
	var buf [256]byte
	n, err := reader.Read(buf[:])
	if err != nil {
		fmt.Println(err)
	}

	r, _ := utf8.DecodeRune(buf[:n])
	fmt.Println("decode error ? ", r == utf8.RuneError)
	fmt.Printf("%s \n", string(r))
	fmt.Println(buf[:n])

	fmt.Println("=============")
}

When I type , os.stdin returns the result correctly, but cancelreader does not

output

$ go run .
好
decode error ?  false
好
[229 165 189 13 10]
=============
decode error ?  true
�
[186 195]
=============
@fzdwx
Copy link
Author

fzdwx commented Aug 10, 2022

go version go1.19 windows/amd64 faild
go version go1.18 windows/amd64 falid

but it was successful on go version go1.18.3 linux/amd64

@fzdwx
Copy link
Author

fzdwx commented Aug 12, 2022

Check out this issue golang/go#54389 (comment)

@knz
Copy link
Contributor

knz commented Aug 18, 2022

As explained in the upstream issue, it looks like this input is not utf-8.

@fzdwx
Copy link
Author

fzdwx commented Aug 18, 2022

yes, windows is GB18030

golang/go#54389 (comment)

@meowgorithm
Copy link
Member

Yeah, so it likely does not make sense to support non UTF-8 encodings, unfortunately.

@meowgorithm
Copy link
Member

Actually, I wonder if #343 would fix this. @fzdwx would you mind checking?

@fzdwx
Copy link
Author

fzdwx commented Aug 19, 2022

Yes, it fixed! @meowgorithm


But I have a second question:

On Windows run tea.program use windows.GetConsoleMode -> 992
but default mode is 503, so after running, the input direction key will be recognized as [[A

image

code:

package main

import (
	"fmt"
	"golang.org/x/sys/windows"
	"unicode/utf16"
)

var fileShareValidFlags uint32 = 0x00000007

func main() {
	var originalMode uint32

	handle, _ := windows.CreateFile(
		&(utf16.Encode([]rune("CONIN$\x00"))[0]), windows.GENERIC_READ|windows.GENERIC_WRITE,
		fileShareValidFlags, nil, windows.OPEN_EXISTING, windows.FILE_FLAG_OVERLAPPED, 0)

	_ = windows.GetConsoleMode(handle, &originalMode)

    // 503
	fmt.Println(originalMode)

	// problematic mode
	//windows.SetConsoleMode(handle, 992)
}

@meowgorithm
Copy link
Member

@fzdwx Would you mind opening a separate issue for this since it's a bit of a separate topic? That way it won't get lost.

@meowgorithm
Copy link
Member

@fzdwx This is now fixed in master and will be available in the next release.

@fzdwx
Copy link
Author

fzdwx commented Aug 20, 2022

Thanks, it works! 🎉

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

Successfully merging a pull request may close this issue.

3 participants