{"id":13440961,"url":"https://github.com/creack/pty","last_synced_at":"2025-05-14T08:05:19.110Z","repository":{"id":37548451,"uuid":"1698361","full_name":"creack/pty","owner":"creack","description":"PTY interface for Go","archived":false,"fork":false,"pushed_at":"2024-10-31T19:00:02.000Z","size":133,"stargazers_count":1782,"open_issues_count":13,"forks_count":246,"subscribers_count":32,"default_branch":"master","last_synced_at":"2025-05-14T08:05:09.395Z","etag":null,"topics":["cross-platform","go","pty","tty"],"latest_commit_sha":null,"homepage":"https://pkg.go.dev/github.com/creack/pty?tab=doc","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/creack.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2011-05-03T21:25:23.000Z","updated_at":"2025-05-13T06:34:18.000Z","dependencies_parsed_at":"2023-11-16T16:37:34.863Z","dependency_job_id":"171ebce9-3ee5-46ff-aef9-29c28f92b431","html_url":"https://github.com/creack/pty","commit_stats":{"total_commits":103,"total_committers":44,"mean_commits":2.340909090909091,"dds":0.8252427184466019,"last_synced_commit":"2cde18bfb702199728dd43bf10a6c15c7336da0a"},"previous_names":[],"tags_count":25,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/creack%2Fpty","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/creack%2Fpty/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/creack%2Fpty/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/creack%2Fpty/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/creack","download_url":"https://codeload.github.com/creack/pty/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254101588,"owners_count":22014907,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cross-platform","go","pty","tty"],"created_at":"2024-07-31T03:01:28.321Z","updated_at":"2025-05-14T08:05:19.084Z","avatar_url":"https://github.com/creack.png","language":"Go","funding_links":[],"categories":["HarmonyOS","开源类库","Go","Open source library","Repositories"],"sub_categories":["Windows Manager","命令行","Command Line"],"readme":"# pty\n\nPty is a Go package for using unix pseudo-terminals.\n\n## Install\n\n```sh\ngo get github.com/creack/pty\n```\n\n## Examples\n\nNote 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.\n\n### Command\n\n```go\npackage main\n\nimport (\n\t\"io\"\n\t\"os\"\n\t\"os/exec\"\n\n\t\"github.com/creack/pty\"\n)\n\nfunc main() {\n\tc := exec.Command(\"grep\", \"--color=auto\", \"bar\")\n\tf, err := pty.Start(c)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tgo func() {\n\t\tf.Write([]byte(\"foo\\n\"))\n\t\tf.Write([]byte(\"bar\\n\"))\n\t\tf.Write([]byte(\"baz\\n\"))\n\t\tf.Write([]byte{4}) // EOT\n\t}()\n\tio.Copy(os.Stdout, f)\n}\n```\n\n### Shell\n\n```go\npackage main\n\nimport (\n        \"io\"\n        \"log\"\n        \"os\"\n        \"os/exec\"\n        \"os/signal\"\n        \"syscall\"\n\n        \"github.com/creack/pty\"\n        \"golang.org/x/term\"\n)\n\nfunc test() error {\n        // Create arbitrary command.\n        c := exec.Command(\"bash\")\n\n        // Start the command with a pty.\n        ptmx, err := pty.Start(c)\n        if err != nil {\n                return err\n        }\n        // Make sure to close the pty at the end.\n        defer func() { _ = ptmx.Close() }() // Best effort.\n\n        // Handle pty size.\n        ch := make(chan os.Signal, 1)\n        signal.Notify(ch, syscall.SIGWINCH)\n        go func() {\n                for range ch {\n                        if err := pty.InheritSize(os.Stdin, ptmx); err != nil {\n                                log.Printf(\"error resizing pty: %s\", err)\n                        }\n                }\n        }()\n        ch \u003c- syscall.SIGWINCH // Initial resize.\n        defer func() { signal.Stop(ch); close(ch) }() // Cleanup signals when done.\n\n        // Set stdin in raw mode.\n        oldState, err := term.MakeRaw(int(os.Stdin.Fd()))\n        if err != nil {\n                panic(err)\n        }\n        defer func() { _ = term.Restore(int(os.Stdin.Fd()), oldState) }() // Best effort.\n\n        // Copy stdin to the pty and the pty to stdout.\n        // NOTE: The goroutine will keep reading until the next keystroke before returning.\n        go func() { _, _ = io.Copy(ptmx, os.Stdin) }()\n        _, _ = io.Copy(os.Stdout, ptmx)\n\n        return nil\n}\n\nfunc main() {\n        if err := test(); err != nil {\n                log.Fatal(err)\n        }\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcreack%2Fpty","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcreack%2Fpty","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcreack%2Fpty/lists"}