commit | b135084511330383e4d0adc4504cf407cab59bb0 | [log] [tgz] |
---|---|---|
author | abner.chen <chenguoqi@loongson.cn> | Sun Oct 17 01:41:19 2021 +0800 |
committer | GitHub <noreply@github.com> | Sat Oct 16 13:41:19 2021 -0400 |
tree | 77b3bcf071054f0b26e3f532bb798a0b5e0a4649 | |
parent | ac2ba380e8b8dd8ae6a81330678b7967dcfbb8ba [diff] |
Rename loongarchx to loong64. (#130) loong64 GOARCH value reserved for LoongArch architecture: https://golang.org/doc/go1.17. github issues: https://github.com/golang/go/issues/46229 LoongArch documents: https://loongson.github.io/LoongArch-Documentation/LoongArch-Vol1-EN.html Signed-off-by: guoqi.chen <chenguoqi@loongson.cn>
Pty is a Go package for using unix pseudo-terminals.
go get github.com/creack/pty
Note that those examples are for demonstration purpose only, to showcase how to use the library. They are not meant to be used in any kind of production environment.
package main import ( "io" "os" "os/exec" "github.com/creack/pty" ) func main() { c := exec.Command("grep", "--color=auto", "bar") f, err := pty.Start(c) if err != nil { panic(err) } go func() { f.Write([]byte("foo\n")) f.Write([]byte("bar\n")) f.Write([]byte("baz\n")) f.Write([]byte{4}) // EOT }() io.Copy(os.Stdout, f) }
package main import ( "io" "log" "os" "os/exec" "os/signal" "syscall" "github.com/creack/pty" "golang.org/x/term" ) func test() error { // Create arbitrary command. c := exec.Command("bash") // Start the command with a pty. ptmx, err := pty.Start(c) if err != nil { return err } // Make sure to close the pty at the end. defer func() { _ = ptmx.Close() }() // Best effort. // Handle pty size. ch := make(chan os.Signal, 1) signal.Notify(ch, syscall.SIGWINCH) go func() { for range ch { if err := pty.InheritSize(os.Stdin, ptmx); err != nil { log.Printf("error resizing pty: %s", err) } } }() ch <- syscall.SIGWINCH // Initial resize. defer func() { signal.Stop(ch); close(ch) }() // Cleanup signals when done. // Set stdin in raw mode. oldState, err := term.MakeRaw(int(os.Stdin.Fd())) if err != nil { panic(err) } defer func() { _ = term.Restore(int(os.Stdin.Fd()), oldState) }() // Best effort. // Copy stdin to the pty and the pty to stdout. // NOTE: The goroutine will keep reading until the next keystroke before returning. go func() { _, _ = io.Copy(ptmx, os.Stdin) }() _, _ = io.Copy(os.Stdout, ptmx) return nil } func main() { if err := test(); err != nil { log.Fatal(err) } }