Coder Social home page Coder Social logo

go-pty's Introduction

Go Pty

Latest Release GoDoc Build Status

Go-Pty is a package for using pseudo-terminal interfaces in Go. It supports Unix PTYs and Windows through ConPty.

Why can't we just use os/exec?

Windows requires updating the process running in the PTY with a special attribute to enable ConPty support. This is not possible with os/exec see go#62708 and go#6271. On Unix, pty.Cmd is just a wrapper around os/exec.Cmd that sets up the PTY.

Usage

go get github.com/aymanbagabas/go-pty

Example running grep

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 for more examples.

Credits

License

This project is licensed under the MIT License - see the LICENSE for details.

go-pty's People

Contributors

aymanbagabas avatar dependabot[bot] avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

go-pty's Issues

Issue with Shell Example

Hey, so I came across this and it looks like a really cool library (thanks for making it!), and I was hoping to use it in a project I'm working on.

Basically, what I'm trying to implement is very similar to your shell example, except that the example isn't behaving the way I expected it to. Essentially, I'm looking to use this package from a background service on my system, and have that background service spawn a pty that a user can connect to and use normally by running screen /dev/ttys002 or similar.

When I run the shell example though, it seems to effectively run it in my current TTY, which is obviously not too helpful in my case. It seems this might be intended given the code, particularly with regard to the io.Copy routines reading/writing os.Stdout/os.Stdin, as well as the fact ptmx.Name() isn't outputted at all.

// 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 io.Copy(ptmx, os.Stdin)
go io.Copy(os.Stdout, ptmx)

I tried to play with the arguments to the IO functions and replace os.Std* with a handle to ptmx.Name(), and that got the shell to seem to not use the current TTY, but I still can't seem to do anything at all inside the screen connected to ptmx.Name(). No data echos no matter what I type (stty shows echo enabled though), and typing exit doesn't seem to reach bash because it doesn't exit.

I feel like I can't be far off at all from getting this to work, but I can't figure out what I'm missing. I'm also confused as to how the shell example was supposed to work in the first place. If you could point me in the right direction I'd really appreciate it.

Thanks so much

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.