console

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 11, 2026 License: AGPL-3.0 Imports: 9 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package console drives an interactive terminal for the AI: it types one line as the user would, waits until the device or shell is back at its prompt, pages through "--More--", and stops where the terminal asks for a password or an answer. A terminal has no exit codes, so neither does a Result.

Index

Constants

View Source
const MaxLine = 4096

MaxLine is the longest line Line types.

Variables

View Source
var (
	// ErrLine is a line that is empty, too long, or has a control
	// character in it.
	ErrLine = errors.New("console: a line is one line of printable text")
	// ErrKey is a key Key does not press.
	ErrKey = errors.New("console: unknown key")
)
View Source
var Keys = map[string][]byte{
	"enter":  {'\r'},
	"space":  {' '},
	"q":      {'q'},
	"ctrl_c": {0x03},
}

Keys are what Key presses.

Functions

func Flatten

func Flatten(s string) string

Flatten is terminal output as the lines it leaves: escape sequences dropped, carriage returns and backspaces writing over what was there, trailing blanks trimmed.

Types

type Options

type Options struct {
	// Timeout is the longest wait; 60 seconds.
	Timeout time.Duration
	// Settle is how long the output stays quiet before the cursor's line
	// is read as a prompt; 300 ms.
	Settle time.Duration
	// Idle is how long quiet output without a prompt ends the wait; 10 s.
	Idle time.Duration
	// MaxPages is how many "--More--" pages are turned; 200.
	MaxPages int
	// MaxBytes is how much output is kept; 1 MB less 4 KB.
	MaxBytes int
	// Decode turns the bytes printed into text; nil takes them as UTF-8.
	Decode func([]byte) string
}

Options bound a wait. Zero fields take the defaults.

type Result

type Result struct {
	Status Status
	Output string
	Pages  int
	Bytes  int64
}

Result is what a line or a key printed, as text without the echo of the line typed, and how the wait for it ended.

func Key

func Key(ctx context.Context, t Terminal, key string, o Options) (Result, error)

Key presses one of Keys and waits for what it prints.

func Line

func Line(ctx context.Context, t Terminal, line string, o Options) (Result, error)

Line types line and Enter, and waits for what it prints.

type Status

type Status string

Status is how a wait ended.

const (
	// AtPrompt is back at a prompt: the command has finished.
	AtPrompt Status = "prompt"
	// Password asks for a password, which only the user types.
	Password Status = "password"
	// Question asks for an answer, as [y/n] or [confirm].
	Question Status = "question"
	// FullScreen is a full-screen program, as an editor or less.
	FullScreen Status = "fullscreen"
	// Quiet printed nothing for a while without a prompt: it may still run.
	Quiet Status = "quiet"
	// Timeout ran out of time; the command may still run.
	Timeout Status = "timeout"
	// Limit printed more than is kept, or more pages than are turned.
	Limit Status = "limit"
)

type Terminal

type Terminal interface {
	// Input is where typed bytes go, bound to the connection of the call.
	Input() (io.Writer, error)
	// Watch gives fn the bytes printed from now on, until stop.
	Watch(fn func(p []byte)) (stop func())
	// CursorLine is the text of the line the cursor is on.
	CursorLine() string
	// FullScreen reports a full-screen program on the alternate screen.
	FullScreen() bool
}

Terminal is the terminal the console types into and reads from.

Jump to

Keyboard shortcuts

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