console

package
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Nov 6, 2026 License: MIT Imports: 8 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 puts the local terminal into raw mode and forwards keystrokes and window resizes to a remote terminal.

Windows needs the console API rather than termios, and the Python implementation needed three rounds of fixes for VT sequences, the GBK code page and held-key repeat counts (DESIGN.md「交互式终端」), so this is written against x/term plus x/sys/windows rather than hand-rolled escape handling.

The three Windows lessons are encoded here rather than re-learned:

  • raw mode is a console-mode change, and the console host translates navigation keys into VT characters itself, so no escape decoding is needed and none is attempted;
  • output goes out as a byte stream, which keeps a GBK code page from turning UTF-8 into mojibake;
  • only stdin decides whether the console is interactive, so redirecting stdout does not disable raw mode.

Index

Constants

This section is empty.

Variables

View Source
var DefaultSize = Size{Cols: 200, Rows: 50}

DefaultSize is used when the real window size cannot be read.

Functions

func IsTerminal

func IsTerminal(fd int) bool

IsTerminal reports whether fd is a terminal.

It is stdin-only on Windows, matching the fix the Python implementation needed for redirected output (DESIGN.md「交互式终端」): checking stdout reports false for `jms login > file`, which would silently disable raw mode for a perfectly interactive session.

Types

type Options

type Options struct {
	// In is the local input; nil means os.Stdin.
	In io.Reader
	// Out is the local output; nil means os.Stdout.
	Out io.Writer
	// OnResize is consulted when the window size changes.
	OnResize ResizeFunc
	// Initial is the starting size; the zero value means DefaultSize.
	Initial Size
	// contains filtered or unexported fields
}

Options tunes an interactive session.

type ResizeFunc

type ResizeFunc func(Size)

ResizeFunc is called when the local window changes size.

type Session

type Session struct {
	// Read returns the next bytes typed locally.
	Read func(p []byte) (int, error)
	// Write sends remote output to the local screen.
	Write func(p []byte) (int, error)
	// Size reports the current local window size.
	Size func() Size
}

Session is a raw-mode local console attached to a remote terminal.

func Open

func Open(opts Options) (*Session, func(), error)

Open puts the local terminal into raw mode.

Restore must be called (typically deferred) to put it back. It is idempotent and is also run on every error path here, so a failed Open never leaves the terminal unusable.

type Size

type Size struct {
	Cols int
	Rows int
}

Size is a terminal window size in character cells.

Jump to

Keyboard shortcuts

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