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

golang/fmt: Println do not align line head when using raw mode #50761

Closed
zyxkad opened this issue Jan 22, 2022 · 4 comments
Closed

golang/fmt: Println do not align line head when using raw mode #50761

zyxkad opened this issue Jan 22, 2022 · 4 comments

Comments

@zyxkad
Copy link
Contributor

zyxkad commented Jan 22, 2022

What version of Go are you using (go version)?

$ go version
go version go1.17.2 darwin/amd64

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)? (That's not matter)

go env Output
$ go env

What did you do?

package main
import (
  "os"
  "fmt"
  terminal "golang.org/x/crypto/ssh/terminal"
)
func main(){
  fmt.Fprintln(os.Stdout, "hello")
  fmt.Fprintln(os.Stdout, "world")
  terminal.MakeRaw((int)(os.Stdin.Fd()))
  fmt.Fprintln(os.Stdout, "hello")
  fmt.Fprintln(os.Stdout, "world")
}

What did you expect to see?

$ go run main.go
hello
world
hello
world
$

What did you see instead?

$ go run main.go
hello
world
hello
     world
          $
@zyxkad
Copy link
Contributor Author

zyxkad commented Jan 22, 2022

I can use

fmt.Fprintf(os.Stdout, "%s\r\n", "hello")
fmt.Fprintf(os.Stdout, "%s\r\n", "world")

to replace it, but that work's hard, isn't?

@davecheney
Copy link
Contributor

When a terminal is placed in raw mode all line ending processing is disabled; fmt.Println appends a \n new line character to the end of its output. As you see, the output continues on a new line, the “paper” the tty is printing on was moved up one line. To move the “carriage”, the print head of to the start of the line, \r carriage return is needed. Normally these are handled by the tty driver in the default “cooked” mode.

This is not a bug in Go. Closed as working as intended.

@zyxkad
Copy link
Contributor Author

zyxkad commented Jan 23, 2022

When a terminal is placed in raw mode all line ending processing is disabled; fmt.Println appends a \n new line character to the end of its output. As you see, the output continues on a new line, the “paper” the tty is printing on was moved up one line. To move the “carriage”, the print head of to the start of the line, \r carriage return is needed. Normally these are handled by the tty driver in the default “cooked” mode.

This is not a bug in Go. Closed as working as intended.

Sorry, I can't make any sense about why you (or another coder) just put LF(\n), and don't put CR(\r)?

func Println(objs ...interface{}){
	str := fmt.Sprintln(objs...)
	if len(str) > 0 && str[len(str) - 1] == '\n' {
		if len(str) > 1 && str[len(str) - 2] == '\r' {
			io.WriteString(os.Stdout, str)
			return
		}
		io.WriteString(os.Stdout, str[:len(str) - 1])
	}
	os.Stdout.Write(crlf)
}

That's my solution now, It's not too long but I'd like to use standard library

@davecheney
Copy link
Contributor

Sorry, I can't make any sense about why you (or another coder) just put LF(\n), and don't put CR(\r)?

because the TTY driver knows to convert it to \r\n. When you shift to raw mode, that logic is disabled.

@golang golang locked and limited conversation to collaborators Jan 23, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants