https://github.com/aymanbagabas/go-pty
Cross platform Go Pty interface
https://github.com/aymanbagabas/go-pty
cross-platform go golang pseudoterminal pty ssh terminal tty
Last synced: over 1 year ago
JSON representation
Cross platform Go Pty interface
- Host: GitHub
- URL: https://github.com/aymanbagabas/go-pty
- Owner: aymanbagabas
- License: mit
- Created: 2023-07-23T21:23:57.000Z (almost 3 years ago)
- Default Branch: master
- Last Pushed: 2024-12-13T14:34:35.000Z (over 1 year ago)
- Last Synced: 2025-03-16T00:11:22.518Z (over 1 year ago)
- Topics: cross-platform, go, golang, pseudoterminal, pty, ssh, terminal, tty
- Language: Go
- Homepage:
- Size: 143 KB
- Stars: 39
- Watchers: 3
- Forks: 2
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Go Pty
Go-Pty is a package for using pseudo-terminal interfaces in Go. It supports Unix PTYs and Windows through [ConPty](https://learn.microsoft.com/en-us/windows/console/creating-a-pseudoconsole-session).
## Why can't we just use os/exec?
Windows requires updating the process running in the PTY with a [special attribute](https://learn.microsoft.com/en-us/windows/console/creating-a-pseudoconsole-session) to enable ConPty support. This is not possible with os/exec see [go#62708](https://github.com/golang/go/issues/62708) and [go#6271](https://github.com/golang/go/pull/62710). On Unix, `pty.Cmd` is just a wrapper around `os/exec.Cmd` that sets up the PTY.
## Usage
```sh
go get github.com/aymanbagabas/go-pty
```
Example running `grep`
```go
package main
import (
"io"
"log"
"os"
"github.com/aymanbagabas/go-pty"
)
func main() {
pty, err := pty.New()
if err != nil {
log.Fatalf("failed to open pty: %s", err)
}
defer pty.Close()
c := pty.Command("grep", "--color=auto", "bar")
if err := c.Start(); err != nil {
log.Fatalf("failed to start: %s", err)
}
go func() {
pty.Write([]byte("foo\n"))
pty.Write([]byte("bar\n"))
pty.Write([]byte("baz\n"))
pty.Write([]byte{4}) // EOT
}()
go io.Copy(os.Stdout, pty)
if err := c.Wait(); err != nil {
panic(err)
}
}
```
Refer to [./examples](./examples) for more examples.
## Credits
- [creack/pty](https://github.com/creack/pty/): support for Unix PTYs
- [microsoft/hcsshim](https://github.com/microsoft/hcsshim): Windows ConPty implementation
## License
This project is licensed under the MIT License - see the [LICENSE](LICENSE) for details.