Help us shape the future of Go! Take the 2026 Go Developer Survey

console

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Oct 30, 2026 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Overview

Package console asks a person to confirm on the console device, never on stdin or stdout, so piping `yes` or capturing output cannot answer it.

Index

Constants

View Source
const (
	DefaultTimeout  = 5 * time.Minute
	DefaultAttempts = 3
)
View Source
const Alphabet = "ABCDEFGHJKMNPQRSTUVWXYZ23456789"

Alphabet has no look-alike characters (no 0 O 1 I L).

View Source
const ChoicePrompt = "Press Enter for "

ChoicePrompt starts the question a Choice asks when more than one option can be picked.

Variables

View Source
var ErrNotTerminal = errors.New("the console device isn't a terminal, so hidden input can't be read")

ErrNotTerminal means the device given to ReadSecret isn't the real console, so input can't be hidden there.

Functions

func ChoiceText

func ChoiceText(ch approval.Choice) string

ChoiceText is how ch shows: its title, then one line per option, the default marked ❯ and an unavailable one with its reason.

func ColorCapable

func ColorCapable(f *os.File) bool

ColorCapable reports whether f is a terminal, which takes ANSI escape codes.

func Flush

func Flush(dev io.ReadWriteCloser)

Flush discards what was typed on the console device before a question, so an Enter pressed earlier can't answer it. Only a real console device is flushed; anything else is left alone.

func IsTerminal

func IsTerminal(f *os.File) bool

IsTerminal reports whether f is a terminal (a real console handle on Windows).

func NewCode

func NewCode(r io.Reader) (string, error)

NewCode returns a random code. Bytes of 248 and above are rejected so each of the 31 characters is equally likely.

func Open

func Open() (io.ReadWriteCloser, error)

Open opens the controlling terminal.

func ReadSecret added in v0.2.0

func ReadSecret(ctx context.Context, dev io.ReadWriteCloser) ([]byte, error)

ReadSecret reads one line from the console device dev with echo off (for agents token). The line is never shown, and the caller should clear the bytes after use. Ctrl-C (ctx cancelled) turns echo back on before it returns, so the terminal isn't left without it.

func Usable

func Usable(stdin, stderr *os.File) bool

Usable reports whether stdin and stderr are both terminals: the first condition for asking a person anything (spec §6.3).

func Width

func Width(f *os.File) int

Width is f's terminal width in columns, or 0 when f is not a terminal.

Types

type Challenger

type Challenger struct {
	Open     func() (io.ReadWriteCloser, error)
	Rand     io.Reader
	Timeout  time.Duration
	Attempts int
	After    func(time.Duration) <-chan time.Time
}

Challenger implements approval.Approver on the console device.

func (*Challenger) Confirm

func (c *Challenger) Confirm(ctx context.Context, summary string) error

Confirm shows summary and a one-time code on the console, and returns nil only when the person types the code back.

func (*Challenger) ConfirmChoosing

func (c *Challenger) ConfirmChoosing(ctx context.Context, summary string, ch approval.Choice) (int, error)

ConfirmChoosing is Confirm with ch asked between the summary and the code (R2): Enter picks ch.Default, an option's key, name or number picks it, and an unavailable option asks again. With one option left, ch is shown and nothing is asked. It returns the option chosen. A default that isn't an option that can be picked is refused before the console opens.

Jump to

Keyboard shortcuts

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