term

package
v0.0.27 Latest Latest
Warning

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

Go to latest
Published: Sep 27, 2023 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AllowsColorOutput

func AllowsColorOutput(w io.Writer) bool

AllowsColorOutput returns true if the specified writer is a terminal and the process environment indicates color output is supported and desired.

func IsTerminal

func IsTerminal(i interface{}) bool

IsTerminal returns whether the passed object is a terminal or not

Types

type Handler

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

Handler guarantees execution of notifications after a critical section (the function passed to a Run method), even in the presence of process termination. It guarantees exactly once invocation of the provided notify functions.

func Chain

func Chain(handler *Handler, notify ...func()) *Handler

Chain creates a new handler that invokes all notify functions when the critical section exits and then invokes the optional handler's notifications. This allows critical sections to be nested without losing exactly once invocations. Notify functions can invoke any cleanup needed but should not exit (which is the responsibility of the parent handler).

func New

func New(final func(os.Signal), notify ...func()) *Handler

New creates a new handler that guarantees all notify functions are run after the critical section exits (or is interrupted by the OS), then invokes the final handler. If no final handler is specified, the default final is `os.Exit(1)`. A handler can only be used for one critical section.

func (*Handler) Close

func (h *Handler) Close()

Close executes all the notification handlers if they have not yet been executed.

func (*Handler) Run

func (h *Handler) Run(fn func() error) error

Run ensures that any notifications are invoked after the provided fn exits (even if the process is interrupted by an OS termination signal). Notifications are only invoked once per Handler instance, so calling Run more than once will not behave as the user expects.

func (*Handler) Signal

func (h *Handler) Signal(s os.Signal)

Signal is called when an os.Signal is received, and guarantees that all notifications are executed, then the final handler is executed. This function should only be called once per Handler instance.

type SafeFunc

type SafeFunc func() error

SafeFunc is a function to be invoked by TTY.

type TTY

type TTY struct {
	// In is a reader representing stdin. It is a required field.
	In io.Reader
	// Out is a writer representing stdout. It must be set to support terminal resizing. It is an
	// optional field.
	Out io.Writer
	// Raw is true if the terminal should be set raw.
	Raw bool
	// TryDev indicates the TTY should try to open /dev/tty if the provided input
	// is not a file descriptor.
	TryDev bool
	// Parent is an optional interrupt handler provided to this function - if provided
	// it will be invoked after the terminal state is restored. If it is not provided,
	// a signal received during the TTY will result in os.Exit(0) being invoked.
	Parent *Handler
}

TTY helps invoke a function and preserve the state of the terminal, even if the process is terminated during execution. It also provides support for terminal resizing for remote command execution/attachment.

func NewTTY

func NewTTY() *TTY

func (TTY) IsTerminalIn

func (t TTY) IsTerminalIn() bool

IsTerminalIn returns true if t.In is a terminal. Does not check /dev/tty even if TryDev is set.

func (TTY) IsTerminalOut

func (t TTY) IsTerminalOut() bool

IsTerminalOut returns true if t.Out is a terminal. Does not check /dev/tty even if TryDev is set.

func (TTY) Safe

func (t TTY) Safe(fn SafeFunc) error

Safe invokes the provided function and will attempt to ensure that when the function returns (or a termination signal is sent) that the terminal state is reset to the condition it was in prior to the function being invoked. If t.Raw is true the terminal will be put into raw mode prior to calling the function. If the input file descriptor is not a TTY and TryDev is true, the /dev/tty file will be opened (if available).

Jump to

Keyboard shortcuts

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