Skip to content

Commit

Permalink
fix garbled output when exit on windows #67
Browse files Browse the repository at this point in the history
  • Loading branch information
lonnywong committed Dec 24, 2023
1 parent d78b35b commit ee691cb
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions tssh/trzsz.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,23 +54,27 @@ func writeAll(dst io.Writer, data []byte) error {

func wrapStdIO(serverIn io.WriteCloser, serverOut io.Reader, serverErr io.Reader, tty bool) {
win := runtime.GOOS == "windows"
forwardIO := func(reader io.Reader, writer io.WriteCloser, oldVal, newVal []byte) {
forwardIO := func(reader io.Reader, writer io.WriteCloser, input bool) {
defer writer.Close()
buffer := make([]byte, 32*1024)
for {
n, err := reader.Read(buffer)
if n > 0 {
buf := buffer[:n]
if win && !tty {
buf = bytes.ReplaceAll(buf, oldVal, newVal)
if input {
buf = bytes.ReplaceAll(buf, []byte("\r\n"), []byte("\n"))
} else {
buf = bytes.ReplaceAll(buf, []byte("\n"), []byte("\r\n"))
}
}
if err := writeAll(writer, buf); err != nil {
warning("wrap stdio write failed: %v", err)
return
}
}
if err == io.EOF {
if win && tty {
if win && tty && input {
_, _ = writer.Write([]byte{0x1A}) // ctrl + z
continue
}
Expand All @@ -83,13 +87,13 @@ func wrapStdIO(serverIn io.WriteCloser, serverOut io.Reader, serverErr io.Reader
}
}
if serverIn != nil {
go forwardIO(os.Stdin, serverIn, []byte("\r\n"), []byte("\n"))
go forwardIO(os.Stdin, serverIn, true)
}
if serverOut != nil {
go forwardIO(serverOut, os.Stdout, []byte("\n"), []byte("\r\n"))
go forwardIO(serverOut, os.Stdout, false)
}
if serverErr != nil {
go forwardIO(serverErr, os.Stderr, []byte("\n"), []byte("\r\n"))
go forwardIO(serverErr, os.Stderr, false)
}
}

Expand Down

0 comments on commit ee691cb

Please sign in to comment.