pty

package module
v0.0.0-...-06aa35b Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 23, 2017 License: MIT Imports: 7 Imported by: 0

README

pty

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

Install

go get github.com/kr/pty

Examples

package main

import (
	"github.com/kr/pty"
	"io"
	"os"
	"os/exec"
)

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

For interactive mode (such as bash) use the StartRaw function:

package main

import (
	"github.com/kr/pty"
	"io"
	"os"
	"os/exec"
)

func main() {
	c := exec.Command("bash")
	f, restore, err := pty.StartRaw(c)
	if err != nil {
		panic(err)
	}

	defer restore()

	go func() {
		io.Copy(f, os.Stdin)
	}()
	io.Copy(os.Stdout, f)
}

Documentation

Overview

Package pty provides functions for working with Unix terminals.

Index

Constants

This section is empty.

Variables

View Source
var ErrNotTerminal = errors.New("fd is not a terminal")

ErrNotTerminal is returned if the file descriptor picked up is not a tty

View Source
var ErrUnsupported = errors.New("unsupported")

ErrUnsupported is returned if a function is not available on the current platform.

Functions

func Getsize

func Getsize(t *os.File) (rows, cols int, err error)

Getsize returns the number of rows (lines) and cols (positions in each line) in terminal t.

func Open

func Open() (pty, tty *os.File, err error)

Opens a pty and its corresponding tty.

func Start

func Start(c *exec.Cmd) (pty *os.File, err error)

Start assigns a pseudo-terminal tty os.File to c.Stdin, c.Stdout, and c.Stderr, calls c.Start, and returns the File of the tty's corresponding pty.

func StartRaw

func StartRaw(c *exec.Cmd) (pty *os.File, restore func(), err error)

Start raw acts as Start but put the terminal into raw mode. Returns an additional function that should be used to restore the terminal state.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL