term

package
v0.13.0 Latest Latest
Warning

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

Go to latest
Published: Jan 18, 2025 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package term provides structures and helper functions to work with terminal (state, sizes).

Index

Constants

This section is empty.

Variables

View Source
var IsInputTTY = sync.OnceValue(func() bool {
	return isatty.IsTerminal(os.Stdin.Fd())
})
View Source
var IsOutputTTY = sync.OnceValue(func() bool {
	return isatty.IsTerminal(os.Stdout.Fd())
})

Functions

func NewMaxWidthWriter

func NewMaxWidthWriter(w io.Writer, maxWidth uint) io.Writer

NewMaxWidthWriter is a Writer that supports a limit of characters on every line, but doesn't do any word wrapping automatically.

func NewPunchCardWriter

func NewPunchCardWriter(w io.Writer) io.Writer

NewPunchCardWriter is a NewWordWrapWriter that limits the line width to 80 columns.

func NewResponsiveWriter

func NewResponsiveWriter(w io.Writer) io.Writer

NewResponsiveWriter creates a Writer that detects the column width of the terminal we are in, and adjusts every line width to fit and use recommended terminal sizes for better readability. Does proper word wrapping automatically.

if terminal width >= 120 columns		use 120 columns
if terminal width >= 100 columns		use 100 columns
if terminal width >=  80 columns		use  80 columns

In case we're not in a terminal or if it's smaller than 80 columns width, doesn't do any wrapping.

func NewWordWrapWriter

func NewWordWrapWriter(w io.Writer, limit uint) io.Writer

NewWordWrapWriter is a Writer that supports a limit of characters on every line and does auto word wrapping that respects that limit.

func ReadPipeInput added in v0.1.10

func ReadPipeInput() string

ReadPipeInput reads input from a pipe and returns it as a string. It checks if the input is coming from a pipe and reads the content until EOF. Returns:

  • A string containing the pipe input if successful.
  • An empty string if there is an error or no input from the pipe.

func SanitizeFilename added in v0.12.1

func SanitizeFilename(filename string) string

SanitizeFilename cleans up a filename to make it safe for use in file systems by removing or replacing invalid characters.

Types

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
}

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 (TTY) GetSize

func (t TTY) GetSize() *TerminalSize

GetSize returns the current size of the user's terminal. If it isn't a terminal, nil is returned.

type TerminalSize

type TerminalSize struct {
	Width  uint16
	Height uint16
}

TerminalSize represents the width and height of a terminal.

func GetSize

func GetSize(fd uintptr) *TerminalSize

GetSize returns the current size of the terminal associated with fd.

type TerminalSizeQueue

type TerminalSizeQueue interface {
	// Next returns the new terminal size after the terminal has been resized. It returns nil when
	// monitoring has been stopped.
	Next() *TerminalSize
}

TerminalSizeQueue is capable of returning terminal resize events as they occur.

Jump to

Keyboard shortcuts

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