added DeadlineHolder
2 files changed
tree: 3b61c7dcc35943cd371fbc61f412f74aae49b461
  1. .github/
  2. .gitignore
  3. asm_solaris_amd64.s
  4. cmd_windows.go
  5. doc.go
  6. doc_test.go
  7. Dockerfile.golang
  8. go.mod
  9. go.sum
  10. io_test.go
  11. ioctl.go
  12. ioctl_bsd.go
  13. ioctl_inner.go
  14. ioctl_legacy.go
  15. ioctl_solaris.go
  16. ioctl_unsupported.go
  17. LICENSE
  18. mktypes.bash
  19. pty_darwin.go
  20. pty_dragonfly.go
  21. pty_freebsd.go
  22. pty_linux.go
  23. pty_netbsd.go
  24. pty_openbsd.go
  25. pty_solaris.go
  26. pty_unsupported.go
  27. pty_windows.go
  28. README.md
  29. run.go
  30. run_unix.go
  31. run_windows.go
  32. test_crosscompile.sh
  33. types.go
  34. types_dragonfly.go
  35. types_freebsd.go
  36. types_netbsd.go
  37. types_openbsd.go
  38. winsize.go
  39. winsize_unix.go
  40. winsize_windows.go
  41. ztypes_386.go
  42. ztypes_amd64.go
  43. ztypes_arm.go
  44. ztypes_arm64.go
  45. ztypes_dragonfly_amd64.go
  46. ztypes_freebsd_386.go
  47. ztypes_freebsd_amd64.go
  48. ztypes_freebsd_arm.go
  49. ztypes_freebsd_arm64.go
  50. ztypes_freebsd_ppc64.go
  51. ztypes_freebsd_riscv64.go
  52. ztypes_loong64.go
  53. ztypes_mipsx.go
  54. ztypes_netbsd_32bit_int.go
  55. ztypes_openbsd_32bit_int.go
  56. ztypes_ppc.go
  57. ztypes_ppc64.go
  58. ztypes_ppc64le.go
  59. ztypes_riscvx.go
  60. ztypes_s390x.go
  61. ztypes_sparcx.go
README.md

pty

Pty is a Go package for using unix pseudo-terminals and windows ConPty.

Install

go get github.com/creack/pty

Examples

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.

NOTE: This package requires ConPty support on windows platform, please make sure your windows system meet these requirements

Command

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)
}

Shell

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)
        }
}