term

package
v0.8.46 Latest Latest
Warning

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

Go to latest
Published: Jul 14, 2025 License: Apache-2.0 Imports: 18 Imported by: 8

README

term package

term.go and its sub-sequences (term_unix...) are copied from golang.org/x/term, and:

  • disable err returned by readPassword when unsupported (and plan9, maybe nacl)

Documentation

Overview

Package term provides support functions for dealing with terminals, as commonly found on UNIX systems.

Putting a terminal into raw mode is the most common requirement:

oldState, err := term.MakeRaw(int(os.Stdin.Fd()))
if err != nil {
        panic(err)
}
defer term.Restore(int(os.Stdin.Fd()), oldState)

Note that on non-Unix systems os.Stdin.Fd() may not be 0.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DisableColors

func DisableColors() bool

DisableColors return a state which identify wherher needs disable colorful console output always.

func GetFdSize added in v0.5.9

func GetFdSize(fd uintptr) (cols, rows int, err error)

func GetSize added in v0.5.9

func GetSize(fd int) (width, height int, err error)

GetSize returns the visible dimensions of the given terminal.

These dimensions don't include any scrollback buffer height.

func GetState added in v0.5.9

func GetState(fd int) (*term.State, error)

GetState returns the current state of a terminal which may be useful to restore the terminal after a signal.

func GetTtySize

func GetTtySize() (cols, rows int)

GetTtySize returns the window size in columns and rows in the active console window. The return value of this function is in the order of cols, rows.

func GetTtySizeByFd added in v0.5.9

func GetTtySizeByFd(fd uintptr) (cols, rows int, err error)

func GetTtySizeByFile added in v0.5.9

func GetTtySizeByFile(outf *os.File) (cols, rows int, err error)

func GetTtySizeByName added in v0.5.9

func GetTtySizeByName(fn string) (cols, rows int, err error)

func IsAnsiEscaped added in v0.5.9

func IsAnsiEscaped(s string) bool

IsAnsiEscaped detects a string if it contains ansi color escaped sequences

func IsCharDevice added in v0.5.9

func IsCharDevice(f *os.File) bool

IsCharDevice detect a file if it's a character device, for unix. Specially used for test if terminal under darwin, since new macOS has a different os.Stdout.

func IsColored

func IsColored(w io.Writer) bool

IsColored detects a writer if it is a colorful tty device.

A colorful tty device can receive ANSI escaped sequences and draw its.

func IsColorful added in v0.7.23

func IsColorful(w io.Writer) (colorful bool)

IsColorful detects a writer if it is a colorful tty device.

A colorful tty device can receive ANSI escaped sequences and draw its.

func IsCygwinTerminal added in v0.5.27

func IsCygwinTerminal(fd uintptr) bool

IsCygwinTerminal return true if the file descriptor is a cygwin or msys2 terminal. This is also always false on this environment.

func IsENOTTY added in v0.8.39

func IsENOTTY(err error) bool

func IsRoot added in v0.5.27

func IsRoot() bool

func IsStartupByDoubleClick added in v0.5.9

func IsStartupByDoubleClick() bool

IsStartupByDoubleClick detects if windows golang executable file is running via double click or from cmd/shell terminator

func IsTerminal added in v0.5.9

func IsTerminal(fd int) bool

IsTerminal returns whether the given file descriptor is a terminal.

func IsTty

func IsTty(w io.Writer) bool

IsTty detects a writer if it is abstracting from a tty (console, terminal) device.

func MakeNewTerm added in v0.8.26

func MakeNewTerm(ctx context.Context, config *PromptModeConfig) (deferFunc func(), err error)

func MakeRaw added in v0.5.9

func MakeRaw(fd int) (*term.State, error)

MakeRaw puts the terminal connected to the given file descriptor into raw mode and returns the previous state of the terminal so that it can be restored.

func MakeRawWrapped added in v0.8.26

func MakeRawWrapped() (deferFunc func(), err error)

func ReadPassword

func ReadPassword(fd int) ([]byte, error)

ReadPassword reads a line of input from a terminal without local echo. This is commonly used for inputting passwords and other sensitive data. The slice returned does not include the \n.

func ReadTill added in v0.8.35

func ReadTill(fd int, delim byte) (string, bool, error)

ReadTill reads a line or a string ending with delim of input from a terminal without local echo.

func Restore added in v0.5.9

func Restore(fd int, oldState *term.State) error

Restore restores the terminal connected to the given file descriptor to a previous state.

func SetDisableColors added in v0.8.35

func SetDisableColors(b bool)

func StatStdout added in v0.7.23

func StatStdout() (normalFile, redirected, piped, term bool)

func StatStdoutString added in v0.7.23

func StatStdoutString() (status string)

func StdoutIsPiped added in v0.7.23

func StdoutIsPiped() (b bool)

func StringToBool added in v0.5.9

func StringToBool(val string, defaultVal ...bool) (ret bool)

func StripEscapes

func StripEscapes(str string) (strCleaned string)

StripEscapes removes any ansi color escaped sequences from a string

func ToBool added in v0.5.9

func ToBool(val any, defaultVal ...bool) (ret bool)

ToBool translate a value (int, bool, string) to boolean

Types

type LooperFunc added in v0.8.26

type LooperFunc func(ctx context.Context, tty SmallTerm, replyPrefix string, exitChan <-chan struct{}, closer func()) (err error)

type PromptModeConfig added in v0.8.46

type PromptModeConfig struct {
	Name              string // used for binding history records
	WelcomeText       string
	PromptText        string
	ReplyText         string
	MainLooperHandler LooperFunc
	PostInitTerminal  func(t *term.Terminal)
	MaxHistoryEntries int
}

type SmallTerm added in v0.8.26

type SmallTerm interface {
	io.Writer
	ReadLine() (string, error)
}

Directories

Path Synopsis
Package chk provides some checked terminal utilities.
Package chk provides some checked terminal utilities.
Package color provides a wrapped standard output device like printf but with colored enhancements.
Package color provides a wrapped standard output device like printf but with colored enhancements.

Jump to

Keyboard shortcuts

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