Qoutroy
Qoutroy

Reputation: 113

Does Go fmt indent automatically?

When I do fmt.Printf("...\n") it doesn't move the cursor to the 0th column and the next line is therefore indented:

13
  13
    13
      13
        13
          13
            113 ('q')

Here is my code:

package main

import (
    "bufio"
    "fmt"
    "os"
    "unicode"

    "golang.org/x/crypto/ssh/terminal"
)

func main() {
    oldState, err := terminal.MakeRaw(0)
    if err != nil {
        panic(err)
    }
    defer terminal.Restore(0, oldState)

    reader := bufio.NewReader(os.Stdin)

    var c rune
    for err == nil {
        if c == 'q' {
            break
        }

        c, _, err = reader.ReadRune()

        if unicode.IsControl(c) {
            fmt.Printf("%d\n", c)
        } else {
            fmt.Printf("%d ('%c')\n", c, c)
        }
    }

    if err != nil {
        panic(err)
    }
}

Upvotes: 3

Views: 950

Answers (1)

peterSO
peterSO

Reputation: 166714

Comment: You're putting the terminal in raw mode, doesn't that require a carriage return to put the cursor at the start of the line? – JimB


For example,

terminal.go:

package main

import (
    "bufio"
    "fmt"
    "os"
    "unicode"

    "golang.org/x/crypto/ssh/terminal"
)

func main() {
    oldState, err := terminal.MakeRaw(0)
    if err != nil {
        panic(err)
    }
    defer terminal.Restore(0, oldState)

    reader := bufio.NewReader(os.Stdin)

    var c rune
    for err == nil {
        if c == 'q' {
            break
        }

        c, _, err = reader.ReadRune()

        if unicode.IsControl(c) {
            fmt.Printf("%d\r\n", c)
        } else {
            fmt.Printf("%d ('%c')\r\n", c, c)
        }
    }

    if err != nil {
        panic(err)
    }
}

Output:

$ go run terminal.go
13
13
13
13
13
113 ('q')
$ 

Upvotes: 6

Related Questions