terminal

package
v0.0.2 Latest Latest
Warning

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

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

Documentation

Overview

Terminal sanitization matches terminal-bash: strip ANSI CSI/OSC escape sequences from raw PTY output and normalize line endings into displayable plain text.

Package terminal provides persistent terminal sessions (S10), matching shell/terminal. This cross-platform backend uses subprocesses and pipes for spawn/send/read/signal/close, bounded scrollback, wait reasons, and one active session per agent. A native PTY backend can replace it without changing the interface.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NormalizeText

func NormalizeText(s string) string

NormalizeText converts CRLF and lone CR to LF and removes BEL.

func Sanitize

func Sanitize(s string) string

Sanitize strips escapes and normalizes text in one call.

func ShellCommand

func ShellCommand(script string) (name string, args []string)

ShellCommand returns the platform shell and one-command execution arguments, allowing tests such as echo to run under Windows cmd or Unix sh.

func StripANSI

func StripANSI(s string) string

StripANSI removes CSI, OSC, and two-byte ANSI escape sequences.

Types

type Session

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

Session is a persistent subprocess-and-pipe terminal with bounded scrollback.

func (*Session) Close

func (s *Session) Close() error

Close closes stdin, terminates the process, and records reason=closed.

func (*Session) Contains

func (s *Session) Contains(sub string) bool

Contains checks scrollback for a substring, useful for polling assertions.

func (*Session) Read

func (s *Session) Read() []string

Read returns a snapshot of all current scrollback lines.

func (*Session) ReadString

func (s *Session) ReadString() string

ReadString joins current scrollback into text.

func (*Session) Send

func (s *Session) Send(data string) error

Send writes stdin bytes, simulating keyboard input.

func (*Session) SendLine

func (s *Session) SendLine(line string) error

SendLine writes text with an appended newline.

func (*Session) Signal

func (s *Session) Signal() error

Signal sends a termination signal; Windows uses Kill.

func (*Session) Wait

func (s *Session) Wait() WaitReason

Wait blocks until termination and returns its reason.

type TerminalBackend

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

TerminalBackend owns terminal sessions and permits one active session per agent.

func NewBackend

func NewBackend(cfg TerminalConfig) *TerminalBackend

NewBackend creates a terminal backend.

func (*TerminalBackend) Active

func (b *TerminalBackend) Active() *Session

Active returns the active session, or nil.

func (*TerminalBackend) Spawn

func (b *TerminalBackend) Spawn(ctx context.Context) (*Session, error)

Spawn creates a session, rejecting it if another session is active.

type TerminalConfig

type TerminalConfig struct {
	Command     string   // Executable name, such as bash, cmd, or sh.
	Args        []string // Arguments.
	MaxLines    int      // Scrollback line limit; nonpositive defaults to 1000.
	ReadLineMax int      // Maximum bytes per line.
}

TerminalConfig configures a terminal session.

type WaitReason

type WaitReason string

WaitReason identifies why a session ended.

const (
	WaitExited    WaitReason = "exited"    // Normal process exit.
	WaitSignalled WaitReason = "signalled" // Signal termination.
	WaitClosed    WaitReason = "closed"    // Explicitly closed.
	WaitError     WaitReason = "error"     // Startup or execution error.
)

Session termination reasons.

Jump to

Keyboard shortcuts

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