pty

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Aug 7, 2020 License: ISC Imports: 4 Imported by: 0

README

######################
Pty - Pseudo terminal
######################

Pty implementation of UNIX 98 pty system.
pty wraps the os/exec library to handle the command execution wheres pty
will enable the os/exec module to run through a pseudo terminal.

Currently supports: Linux and Darwin(OSX).


How To:
------------

Pseudo terminal program:

.. code-block:: go

    package main

   import (
       "github.com/Mattemagikern/pty"

       "log"
       "os"
       "os/exec"
   )

   func pseudo_terminal() error {
       c := exec.Command("bash")
       /* Both Stdout and Stderr */
       c.Stdout = os.Stdout
       c.Stdin = os.Stdin

       pty, err := pty.New(c)
       if err != nil {
           return err
       }

       if err := <-pty.Wait(); err != nil {
           return err
       }
       return nil
   }

   func main() {
       if err := pseudo_terminal(); err != nil {
           log.Fatal(err)
       }
   }

Run interactive commands:

.. code-block:: go

   func run_cmd() error {
       c := exec.Command("cat")
       /* Both Stdout and Stderr */
       c.Stdout = os.Stdout
       c.Stdin = os.Stdin
       pty, err := pty.New(c)
       if err != nil {
           return err
       }

       if err := <-pty.Wait(); err != nil {
           return err
       }
       return nil
   }

   func main() {
       if err := run_cmd(); err != nil {
           log.Fatal(err)
       }
   }


Quirks
---------

An interesting thing I've found is that on Darwin you need to read the stdout
otherwise the execution stops. To see this behaviour; add the test below to the
pty_test.go file and execute on osx. Interesting enough this isn't a problem on
Linux.

.. code-block:: go

   func TestCommandDarwinQuirk(t *testing.T) {
      c := exec.Command("./echo.sh", "TestCommand")
      pty, err := pty.New(c)
      if err != nil {
      	t.Fatal(err)
      }
      select {
      case err := <-pty.Wait():
      	if err != nil {
      		t.Fatal(err)
      	}
      case <-time.NewTimer(2 * time.Second).C:
      	t.Fatal("Timeout")
      	pty.Close()
      }
   }

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Cfmakeraw

func Cfmakeraw(f *os.File) (*syscall.Termios, error)

func Restore

func Restore(f *os.File, term *syscall.Termios) error

Types

type Pty

type Pty interface {
	SetSize(cols, rows uint16) error
	GetSize() (uint16, uint16, error)
	Wait() <-chan error
	Close() error

	Read([]byte) (int, error)
	Write([]byte) (int, error)
}

func New

func New(cmd *exec.Cmd) (Pty, error)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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