# pty
Pty is a Go package for using unix pseudo-terminals.
## Install
```sh
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. If you want to **set deadlines to work** and `Close()` **interrupting** `Read()` on the returned `*os.File`, you will need to call `syscall.SetNonblock` manually.
### Command
```go
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
```go
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)
}
}
```
没有合适的资源?快使用搜索试试~ 我知道了~
PTY interface for Go.zip
共62个文件
go:50个
yml:3个
gitignore:1个
需积分: 5 0 下载量 46 浏览量
2023-12-28
20:53:24
上传
评论
收藏 34KB ZIP 举报
温馨提示
PTY interface for Go
资源推荐
资源详情
资源评论
收起资源包目录
PTY interface for Go.zip (62个子文件)
newname
ztypes_freebsd_arm64.go 235B
.editorconfig 980B
types_netbsd.go 237B
Dockerfile.golang 329B
ztypes_netbsd_32bit_int.go 263B
ztypes_arm64.go 152B
winsize_unix.go 873B
pty_unsupported.go 273B
start_windows.go 498B
ztypes_386.go 148B
go.mod 41B
ztypes_ppc64le.go 156B
pty_linux.go 1KB
asm_solaris_amd64.s 435B
pty_darwin.go 1KB
ztypes_dragonfly_amd64.go 262B
.github
workflows
crosscompile.yml 298B
test.yml 2KB
ztypes_openbsd_32bit_int.go 236B
LICENSE 1KB
ztypes_riscvx.go 182B
mktypes.bash 336B
pty_solaris.go 4KB
pty_dragonfly.go 2KB
ioctl_inner.go 361B
io_test.go 3KB
ztypes_freebsd_arm.go 220B
types_freebsd.go 228B
pty_freebsd.go 2KB
ztypes_arm.go 148B
start.go 670B
winsize.go 636B
ztypes_freebsd_amd64.go 255B
types.go 101B
ztypes_sparcx.go 182B
ioctl.go 590B
ioctl_legacy.go 216B
pty_openbsd.go 987B
doc.go 348B
ztypes_ppc.go 118B
test_crosscompile.sh 2KB
ioctl_solaris.go 1KB
types_openbsd.go 187B
ioctl_unsupported.go 157B
ioctl_bsd.go 1KB
winsize_unsupported.go 426B
doc_test.go 6KB
ztypes_freebsd_386.go 220B
helpers_test.go 1KB
.golangci.yml 18KB
.gitignore 27B
pty_netbsd.go 1KB
ztypes_freebsd_ppc64.go 202B
ztypes_freebsd_riscv64.go 182B
ztypes_ppc64.go 152B
ztypes_s390x.go 152B
ztypes_amd64.go 152B
README.md 2KB
ztypes_mipsx.go 232B
run.go 1KB
ztypes_loong64.go 156B
types_dragonfly.go 266B
共 62 条
- 1
资源评论
Lei宝啊
- 粉丝: 2067
- 资源: 1330
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功