Convert Figma logo to code with AI

creack logopty

PTY interface for Go

1,725
241
1,725
13

Top Related Projects

4,711

LXC - Linux Containers

24,648

Podman: A tool for managing OCI containers and pods.

7,568

A tool that facilitates building OCI images.

24,181

Podman: A tool for managing OCI containers and pods.

3,109

A fast and lightweight fully featured OCI runtime and C library for running containers

Quick Overview

The creack/pty project is a Go package that provides a simple interface for interacting with pseudo-terminals (PTYs) on Unix-like systems. It allows you to create and manage PTYs, which are commonly used in terminal emulators, SSH clients, and other applications that need to interact with a terminal-like environment.

Pros

  • Cross-platform compatibility: The package supports a wide range of Unix-like operating systems, including Linux, macOS, and BSD.
  • Ease of use: The API is straightforward and easy to integrate into your Go projects.
  • Actively maintained: The project has regular updates and bug fixes, ensuring its reliability and compatibility with the latest Go versions.
  • Extensive documentation: The project's documentation provides clear examples and detailed explanations of the available functionality.

Cons

  • Limited to Unix-like systems: The package is not compatible with Windows, as it relies on Unix-specific system calls and features.
  • Potential performance overhead: Interacting with PTYs may introduce some performance overhead, especially for high-throughput applications.
  • Dependency on external libraries: The package depends on the golang.org/x/sys library, which may require additional setup or configuration in some environments.
  • Lack of advanced features: The package provides a basic set of PTY management functions, but may not offer more advanced features found in some other PTY libraries.

Code Examples

Here are a few examples of how to use the creack/pty package in your Go code:

Creating a PTY and running a command

package main

import (
    "fmt"
    "io"
    "os"
    "os/exec"

    "github.com/creack/pty"
)

func main() {
    // Create a new PTY
    ptyMaster, ptySlaveFile, err := pty.Open()
    if err != nil {
        panic(err)
    }
    defer ptyMaster.Close()

    // Start a new process with the PTY as its controlling terminal
    cmd := exec.Command("bash")
    cmd.Stdin = ptySlaveFile
    cmd.Stdout = ptySlaveFile
    cmd.Stderr = ptySlaveFile
    cmd.Start()

    // Copy the PTY output to the standard output
    io.Copy(os.Stdout, ptyMaster)
}

This example creates a new PTY, starts a new bash process with the PTY as its controlling terminal, and then copies the output from the PTY to the standard output.

Resizing the PTY

package main

import (
    "fmt"
    "os"

    "github.com/creack/pty"
)

func main() {
    // Create a new PTY
    ptyMaster, _, err := pty.Open()
    if err != nil {
        panic(err)
    }
    defer ptyMaster.Close()

    // Resize the PTY to 80x24 columns/rows
    if err := pty.Setsize(ptyMaster, &pty.Winsize{
        Rows: 24,
        Cols: 80,
    }); err != nil {
        panic(err)
    }

    fmt.Println("PTY resized to 80x24")
}

This example creates a new PTY and then resizes it to 80 columns and 24 rows using the pty.Setsize() function.

Attaching to an existing PTY

package main

import (
    "fmt"
    "os"

    "github.com/creack/pty"
)

func main() {
    // Attach to an existing PTY file descriptor
    ptyMaster, err := pty.AttachTo(os.Stdin.Fd())
    if err != nil {
        panic(err)
    }
    defer ptyMaster.Close()

    fmt.Println("Attached to existing PTY")
}

This example attaches to an existing PTY using the pty.AttachTo() function, which takes a file descriptor as input.

Getting Started

To use the `creack/

Competitor Comparisons

4,711

LXC - Linux Containers

Pros of lxc/lxc

  • lxc/lxc provides a comprehensive set of tools and libraries for managing Linux containers, including support for various container types and features.
  • The project has a large and active community, with regular updates and improvements.
  • lxc/lxc is widely used in production environments and has a proven track record of stability and reliability.

Cons of lxc/lxc

  • The learning curve for lxc/lxc can be steeper compared to creack/pty, as it involves understanding the complexities of container management.
  • The project's scope is broader than creack/pty, which may make it less suitable for specific use cases that require a more focused approach.

Code Comparison

lxc/lxc (creating a container):

import lxc

container = lxc.Container("mycontainer")
container.create("ubuntu")
container.start()

creack/pty (creating a pseudo-terminal):

import "github.com/creack/pty"

f, err := pty.Start(cmd)
if err != nil {
    // handle error
}
24,648

Podman: A tool for managing OCI containers and pods.

Pros of Podman

  • Podman is a full-featured container runtime that supports a wide range of container-related features, including image management, networking, and security.
  • Podman provides a Docker-compatible API, making it easy to migrate existing Docker-based workflows to Podman.
  • Podman is designed to be rootless by default, improving security and reducing the risk of privilege escalation.

Cons of Podman

  • Podman has a larger codebase and more complex architecture compared to creack/pty, which may make it more challenging to understand and contribute to.
  • Podman's focus on enterprise-level features may make it overkill for some use cases where a simpler solution like creack/pty would suffice.

Code Comparison

Podman:

package main

import (
    "fmt"
    "os"

    "github.com/containers/podman/v4/pkg/bindings"
    "github.com/containers/podman/v4/pkg/bindings/containers"
)

func main() {
    conn, err := bindings.NewConnection(context.Background(), "unix:///run/podman/podman.sock")
    if err != nil {
        fmt.Println(err)
        os.Exit(1)
    }

    _, err = containers.Run(context.Background(), conn, &containers.RunOptions{
        Name:  "mycontainer",
        Image: "docker.io/library/alpine:latest",
        Command: []string{
            "/bin/sh",
            "-c",
            "while true; do echo 'Hello, world!'; sleep 1; done",
        },
    })
    if err != nil {
        fmt.Println(err)
        os.Exit(1)
    }
}

creack/pty:

package main

import (
    "fmt"
    "io"
    "os"
    "os/exec"

    "github.com/creack/pty"
)

func main() {
    cmd := exec.Command("/bin/bash")
    ptmx, err := pty.Start(cmd)
    if err != nil {
        fmt.Println(err)
        os.Exit(1)
    }
    defer func() { _ = ptmx.Close() }()
    io.Copy(os.Stdout, ptmx)
}
7,568

A tool that facilitates building OCI images.

Pros of Buildah

  • Buildah is a tool for building, modifying, and managing Docker and OCI (Open Container Initiative) images. It provides a more low-level and flexible approach to container image management compared to creack/pty.
  • Buildah supports a wide range of container runtimes, including Docker, Podman, and CRI-O, making it more versatile and compatible with different container environments.
  • Buildah offers advanced features like multi-stage builds, layer caching, and the ability to build images without a Docker daemon, which can be beneficial in certain use cases.

Cons of Buildah

  • Buildah has a steeper learning curve compared to creack/pty, as it provides a more complex and feature-rich set of tools for container image management.
  • The Buildah codebase is larger and more complex than creack/pty, which may make it more challenging to contribute to or maintain for some users.
  • Buildah may have a higher resource footprint compared to creack/pty, as it includes more functionality and dependencies.

Code Comparison

Here's a brief code comparison between Buildah and creack/pty:

Buildah (creating a container image):

b, err := buildah.NewBuilder(ctx, store, buildah.DefaultContainerConfig)
if err != nil {
    // Handle error
}
defer b.Delete()

if err := b.Add(ctx, ".", "/"); err != nil {
    // Handle error
}

if _, err := b.CommitImage(ctx, "my-image", buildah.CommitOptions{}); err != nil {
    // Handle error
}

creack/pty (creating a pseudo-terminal):

pty, tty, err := pty.Open()
if err != nil {
    // Handle error
}
defer pty.Close()

cmd := exec.Command("bash")
cmd.Stdin = tty
cmd.Stdout = tty
cmd.Stderr = tty

if err := cmd.Start(); err != nil {
    // Handle error
}
24,181

Podman: A tool for managing OCI containers and pods.

Pros of Podman

  • Podman is a full-featured container runtime that supports a wide range of container-related features, including image management, networking, and security.
  • Podman provides a Docker-compatible API, making it easy to migrate existing Docker-based workflows to Podman.
  • Podman is designed to be rootless by default, improving security and reducing the risk of privilege escalation.

Cons of Podman

  • Podman has a larger codebase and more complex architecture compared to creack/pty, which may make it more challenging to understand and contribute to.
  • Podman's focus on enterprise-level features may make it overkill for some use cases where a simpler solution like creack/pty would suffice.

Code Comparison

Podman:

package main

import (
    "fmt"
    "os"

    "github.com/containers/podman/v4/pkg/bindings"
    "github.com/containers/podman/v4/pkg/bindings/containers"
)

func main() {
    conn, err := bindings.NewConnection(context.Background(), "unix:///run/podman/podman.sock")
    if err != nil {
        fmt.Println(err)
        os.Exit(1)
    }

    _, err = containers.Run(context.Background(), conn, &containers.RunOptions{
        Name:  "mycontainer",
        Image: "docker.io/library/alpine:latest",
        Command: []string{
            "/bin/sh",
            "-c",
            "while true; do echo 'Hello, world!'; sleep 1; done",
        },
    })
    if err != nil {
        fmt.Println(err)
        os.Exit(1)
    }
}

creack/pty:

package main

import (
    "fmt"
    "io"
    "os"
    "os/exec"

    "github.com/creack/pty"
)

func main() {
    cmd := exec.Command("/bin/bash")
    ptmx, err := pty.Start(cmd)
    if err != nil {
        fmt.Println(err)
        os.Exit(1)
    }
    defer func() { _ = ptmx.Close() }()
    io.Copy(os.Stdout, ptmx)
}
3,109

A fast and lightweight fully featured OCI runtime and C library for running containers

Pros of containers/crun

  • Designed specifically for container runtime, providing a more specialized and optimized solution compared to creack/pty.
  • Supports a wider range of container technologies, including OCI and CRI-O, making it more versatile.
  • Offers improved performance and scalability for container-based workloads.

Cons of containers/crun

  • May have a steeper learning curve due to its specialized nature, compared to the more general-purpose creack/pty.
  • Potentially less flexible in terms of use cases outside of container-related tasks.
  • May have a larger codebase and dependencies, which could impact deployment complexity.

Code Comparison

containers/crun:

int main(int argc, char *argv[]) {
    if (argc < 2) {
        fprintf(stderr, "Usage: %s <command> [args...]\n", argv[0]);
        return 1;
    }

    return crun_run_linux_container(argc - 1, argv + 1, NULL);
}

creack/pty:

func Open() (pty, tty *os.File, err error) {
    p, err := os.OpenFile("/dev/ptmx", os.O_RDWR|os.O_NOCTTY, 0)
    if err != nil {
        return nil, nil, err
    }

    slaveName, err := ptsname(p)
    if err != nil {
        p.Close()
        return nil, nil, err
    }
    return p, os.OpenFile(slaveName, os.O_RDWR|os.O_NOCTTY, 0)
}

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

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. 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.

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

NPM DownloadsLast 30 Days