terminal

package
v0.1.0 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

sanitize.go 复刻官方 terminal/terminal-bash 的清洗逻辑:把 PTY 原始输出中的 ANSI CSI/OSC 转义序列剥离,并归一化回车换行,得到可安全展示的纯文本。

Package terminal 提供持久终端会话抽象(任务 S10:Terminal PTY)。

对齐上游:packages/shell/terminal

说明:在 Windows 上无原生 PTY,本包提供跨平台「终端会话」抽象:以子进程+管道承载, 提供 spawn / send / read / signal / close 生命周期、有界回滚(bounded scrollback)、 wait reason,以及单 agent 独占活动(同一时刻仅一个活跃 session)。在 *nix 上可替换为 真正的 PTY 后端(接口不变)。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NormalizeText

func NormalizeText(s string) string

NormalizeText 归一化终端文本:CRLF 与孤立 CR 转为 LF,并移除 BEL。

func Sanitize

func Sanitize(s string) string

Sanitize 一步完成转义剥离与文本归一化。

func ShellCommand

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

ShellCommand 返回当前平台默认 shell 与退出只需一条命令的包装。 用于测试:`echo ...` 在 Windows cmd / Unix sh 上均可运行。

func StripANSI

func StripANSI(s string) string

StripANSI 移除字符串中的 ANSI 转义序列(CSI、OSC、两字节转义族)。

Types

type Session

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

Session 是一次持久终端会话(子进程 + 管道 + 有界回滚)。

func (*Session) Close

func (s *Session) Close() error

Close 关闭会话(关闭 stdin 并终止进程,标记 reason=closed)。

func (*Session) Contains

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

Contains 判断回滚中是否出现某子串(轮询断言用)。

func (*Session) Read

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

Read 取出当前回滚的全部行(快照)。

func (*Session) ReadString

func (s *Session) ReadString() string

ReadString 返回当前回滚的拼接文本。

func (*Session) Send

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

Send 向 stdin 写入数据(模拟键盘输入)。

func (*Session) SendLine

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

SendLine 写一行(自动补 \n)。

func (*Session) Signal

func (s *Session) Signal() error

Signal 向进程发送终止信号(跨平台:Windows 走 Kill)。

func (*Session) Wait

func (s *Session) Wait() WaitReason

Wait 阻塞直到会话结束,返回结束原因。

type TerminalBackend

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

TerminalBackend 生成并持有终端会话;保证单 agent 独占活动(同一时刻一个活跃 session)。

func NewBackend

func NewBackend(cfg TerminalConfig) *TerminalBackend

NewBackend 创建终端后端。

func (*TerminalBackend) Active

func (b *TerminalBackend) Active() *Session

Active 返回当前活跃会话(无则 nil)。

func (*TerminalBackend) Spawn

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

Spawn 生成一个新会话;若已有活跃会话则返回错误(单 agent 独占)。

type TerminalConfig

type TerminalConfig struct {
	Command     string   // 可执行名(如 bash / cmd / sh)
	Args        []string // 参数
	MaxLines    int      // 有界回滚行数(<=0 用默认 1000)
	ReadLineMax int      // 单行最大字节(防超行)
}

TerminalConfig 是生成终端的配置。

type WaitReason

type WaitReason string

WaitReason 是会话结束原因。

const (
	WaitExited    WaitReason = "exited"    // 进程正常退出
	WaitSignalled WaitReason = "signalled" // 被信号终止
	WaitClosed    WaitReason = "closed"    // 被显式 close
	WaitError     WaitReason = "error"     // 启动/运行错误
)

结束原因枚举。

Jump to

Keyboard shortcuts

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