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

cannot read escape code response with ReadRune() on Windows with Mintty #22

Open
srlehn opened this issue Apr 2, 2019 · 4 comments
Open

Comments

@srlehn
Copy link
Contributor

srlehn commented Apr 2, 2019

Update: this problem exists for the Cygwin terminal mintty

See the latest comment


I am trying to read the terminal response on escape code queries on Windows.
The code always queries successfully on Linux but sometimes messes the terminal up (I have to type stty echo then). On Windows however the terminal answer is not caught by the program and gets written as text.

How do I read such a terminal response? What am I doing wrong here?

demo code

ReadRune()

@hymkor
Copy link
Contributor

hymkor commented Apr 7, 2019

You might want to ignore '\000' .

// foo.go

package main

import (
	"fmt"
	"github.com/mattn/go-tty"
)

func main(){
	fmt.Println("----")
	fmt.Println("\033[0c")
	fmt.Println("----")
	fmt.Println("Hit 'q' to quit")

	tty1,err := tty.Open()
	if err != nil {
		panic(err.Error())
	}
	defer tty1.Close()

	for{
		r,err := tty1.ReadRune()
		if err != nil { panic(err.Error()) }
		if r == 'q' { break }
		if r == '\000' { continue }
		if r < ' ' {
			fmt.Printf("<%X>",r)
		}else{
			fmt.Printf("%c",r)
		}
	}
	fmt.Println()
}

On Windows 10 machine, I executed go run foo.go

$ go run foo.go
----

----
Hit 'q' to quit
<1B>[?1;0c

It looks that there are some responses.

@hymkor
Copy link
Contributor

hymkor commented Apr 7, 2019

The response ESC[?1;0c for the request ESC[0c means the terminal is VT101 compatible.

@srlehn
Copy link
Contributor Author

srlehn commented Apr 7, 2019

Hi zetamatta, thanks for the answer. I never noticed the \000 chars but they are not the problem.

I forgot to mention the important part (wasn't really aware of it): conhost.exe from the regular windows console and ConEmu DO catch the response but other terminals like mintty (Cygwin terminal) does not.

This is visible in the posted image: ConEmu is in the upper left corner, mintty in the upper right and conhost at the bottom.
ConEmu and conhost have the terminal response printed during the program run but with mintty the response is only printed after the program has run on the next shell prompt.

@srlehn srlehn changed the title cannot read escape code response with ReadRune() on Windows cannot read escape code response with ReadRune() on Windows with **MinTty** Apr 7, 2019
@srlehn srlehn changed the title cannot read escape code response with ReadRune() on Windows with **MinTty** cannot read escape code response with ReadRune() on Windows with MinTty Apr 7, 2019
@srlehn
Copy link
Contributor Author

srlehn commented Apr 7, 2019

perhaps somewhat related: mattn/go-isatty#8 mattn/go-isatty#13

go-tty should support Mintty too.

@srlehn srlehn changed the title cannot read escape code response with ReadRune() on Windows with MinTty cannot read escape code response with ReadRune() on Windows with Mintty Apr 19, 2019
Onewaysidewalks added a commit to Onewaysidewalks/tilt that referenced this issue May 4, 2023
Currently, when using `ReadRune` on windows OSes, the input comes prefixed with 'empty' characters (as described here: mattn/go-tty#22)
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