当我执行fmt.Printf("...\n")
时,它不会将光标移到第0列,因此缩进了下一行:
13
13
13
13
13
13
113 ('q')
这是我的代码:
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)
}
}
答案 0 :(得分:6)
注释:您将终端置于原始模式,不需要 回车将光标放在行的开头? – JimB
例如,
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)
}
}
输出:
$ go run terminal.go
13
13
13
13
13
113 ('q')
$