mattn / go-tty

MIT License
211 stars 18 forks source link

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

Open srlehn opened 5 years ago

srlehn commented 5 years ago

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 commented 5 years ago

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 commented 5 years ago

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

srlehn commented 5 years ago

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 commented 5 years ago

perhaps somewhat related: https://github.com/mattn/go-isatty/issues/8 https://github.com/mattn/go-isatty/pull/13

go-tty should support Mintty too.