terminal

package
v0.7.2 Latest Latest
Warning

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

Go to latest
Published: Nov 11, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package terminal attaches a local terminal to a hijacked container stream.

A Session puts stdin into raw mode when it is a terminal, copies bytes in both directions, and restores the terminal when the remote side finishes, the context is canceled, or Close is called. Window changes are reported through a callback so the caller can invoke the engine resize API.

Index

Constants

This section is empty.

Variables

View Source
var ErrClosed = errors.New("terminal: session closed")

ErrClosed is returned when Close is called, including when Close stops a running Start.

Functions

This section is empty.

Types

type Session

type Session struct {
	// contains filtered or unexported fields
}

Session is an interactive terminal bound to a hijacked connection.

Close is idempotent. It is safe to call if Start never runs, and it is safe to call again after Start returns. Close during Start stops the copies and makes Start return ErrClosed.

func NewSession

func NewSession(in *os.File, out io.Writer, conn io.WriteCloser, reader io.Reader) (*Session, error)

NewSession binds in and out to a hijacked stream.

conn is the write side of the stream. reader is the read side. For a Docker hijack those are the connection and its buffered reader, which must be drained separately. NewSession does not change terminal settings; Start does that when in is a terminal.

func (*Session) Close

func (s *Session) Close() error

Close restores the terminal state and closes the hijacked connection. A second call is a no-op. Close during Start makes Start return ErrClosed.

func (*Session) Size added in v0.5.0

func (s *Session) Size() (Size, error)

Size returns the current window size.

func (*Session) Start

func (s *Session) Start(ctx context.Context, resize func(Size) error) (err error)

Start copies in to conn and reader to out.

When in reaches EOF, Start half-closes conn so the remote sees EOF and keeps copying output. When reader returns, Start closes conn and returns. Canceling ctx closes conn and stops both copies.

resize is called with the current size before copying starts, then again when the window changes. A nil resize skips watching. resize runs synchronously for the initial size and again from a goroutine afterward; it should return when ctx is canceled.

Start is called once. It enters raw mode when in is a terminal and restores the previous state before returning.

type Size added in v0.5.0

type Size struct {
	Width  uint
	Height uint
}

Size is the terminal window, in characters.

Jump to

Keyboard shortcuts

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