• Stars
    star
    1,679
  • Rank 27,815 (Top 0.6 %)
  • Language
    Go
  • License
    MIT License
  • Created over 13 years ago
  • Updated 4 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

PTY interface for Go

pty

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

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.

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

More Repositories

1

goproxy

GoProxy is a ReverseProxy / LoadBalancer helper for Golang
Go
176
star
2

docker-firefox

Firefox over Docker via VNC
72
star
3

termios

Multi Platform Golang terminal management: Raw mode, TTY size.
Go
38
star
4

httpreq

The package provides an easy way to "unmarshal" query string data into a struct. Without reflect.
Go
36
star
5

goselect

select(2) implementation in Go
Go
31
star
6

dotfiles

Dev environment
Emacs Lisp
23
star
7

ehttp

Golang http wrapper handling errors.
Go
20
star
8

gosplice

Splice implementation in Go
Go
10
star
9

multio

Multiple I/O operation for Golang
Go
8
star
10

assumerole

Assume AWS role and auto refresh tokens.
Go
7
star
11

go-redis-server

Redis server implementation in Go.
Go
6
star
12

logicsim

Simple web-based logic gate simulator
TypeScript
5
star
13

docker_devmapper

Docker statically linked with libdevmapper
Shell
5
star
14

plantuml-hot-loader

Plantuml server with hot reload
JavaScript
5
star
15

goray

Raytracer in pure Go.
Go
5
star
16

ibweb

An Interactive Broker web viewer in python
Python
5
star
17

utc

Tiny util to convert utc to local date.
Go
2
star
18

go-base-template

Base template for a Golang project.
2
star
19

aef

Configuration variables management
Go
2
star
20

sink

Small utils that consumes tcp/udp connection without replying.
Go
2
star
21

gofour

Terminal Connect Four
Go
2
star
22

yaml2json

small util that converts yaml from stdin to json on stdout
Makefile
2
star
23

goshell

Partial recover of first go shell
Go
2
star
24

docker-grpc

Dockerfile with protobuf, grpc and protoc-gen-go installed.
Dockerfile
2
star
25

butterfly

Dockerfile for butterfly in-browser terminal
Shell
2
star
26

go-sqlite-static

Sqlite binding for Golang statically linked (Docker)
Go
2
star
27

weechat-docker

Perl
2
star
28

ml

Go
2
star
29

go-auth-hello

Support code for the North Austin Golang Meetup of April 21th, 2016
Go
2
star
30

dev

Docker image for golang/docker dev environment emacs/zsh/tmux
2
star
31

golang-web

Basic load-balanced web app in Go with bootstrap and tutum and datadog.
Smarty
1
star
32

hcler

Encode arbitrary Go types to Hashicorp's HCL format.
Go
1
star
33

gomeetup

Go
1
star
34

filewatcher

File system watcher using polling to work within docker mount bind.
Go
1
star
35

spider

Go
1
star
36

rtv0

Playing with basic ray tracing
C
1
star
37

golisten

Privilege de-escalation listen in Go.
Go
1
star
38

adventofcode

Go
1
star
39

fdf

Wireframe (fil de fer) visualizer
Go
1
star
40

elixir

Elixir Dockerfile
1
star
41

lm

Logging message library for Golang
Go
1
star
42

bug

Braille Unicode Graphics
Go
1
star
43

waitgroup

Waitgroup re-implementattion using chans
Go
1
star
44

gogrid

Interactive grid in the terminal
Go
1
star
45

ledcube

LED cube controlled by Golang using gobot.io
C++
1
star
46

partypher

Parts/Inventory management in Go.
Go
1
star
47

docker-jenkins

Jenkins in Docker with Git plugin and Docker installed
1
star
48

tcplb

TCP Load balancer in Go
Go
1
star
49

gogrok

Ngrok in Go
Go
1
star
50

auth

1
star