util

package
v0.13.0 Latest Latest
Warning

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

Go to latest
Published: May 14, 2026 License: Apache-2.0 Imports: 14 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

Index

Constants

This section is empty.

Variables

View Source
var IsTerminal = func(w any) bool {
	if tw, ok := w.(*TermWriter); ok {
		return term.IsTerminal(int(tw.Fd()))
	}
	if f, ok := w.(*os.File); ok {
		return term.IsTerminal(int(f.Fd()))
	}
	return false
}

IsTerminal is a helper for detecting whether an io.Writer or io.Reader is an interactive terminal / TTY. It is a variable so that tests can override it (similar to common.OpenBrowser).

Functions

func AnySlice

func AnySlice[T any](in []T) []any

func ConvertSliceToAny

func ConvertSliceToAny[T any](ts []T) []any

ConvertSliceToAny converts a slice of some type to slice of any. Returns nil if the input slice is nil.

func ConvertStringSlicePtr

func ConvertStringSlicePtr[T ~string](ss []string) *[]T

ConvertStringSlicePtr converts a slice of strings to a pointer to another string-like type. Returns nil if the input slice is nil.

func Deref

func Deref[T any](val *T) T

func DerefStr

func DerefStr[T ~string](val *T) string

func ExpandPath

func ExpandPath(path string) string

ExpandPath expands environment variables and tilde in file paths. It handles: - Empty paths (returns empty string) - Environment variable expansion (e.g., $HOME/config) - Home directory expansion (e.g., ~/config or ~) - Path normalization for cross-platform compatibility

func Must

func Must[T any](v T, err error) T

func PtrIfNonZero

func PtrIfNonZero[T comparable](val T) *T

func ReadAll

func ReadAll(ctx context.Context, stdin io.Reader) (string, error)

func ReadLine

func ReadLine(ctx context.Context, stdin io.Reader) (string, error)

func ReadPassword

func ReadPassword(ctx context.Context, stdin io.Reader) (string, error)

func SerializeToJSON

func SerializeToJSON(w io.Writer, v any) error

func SerializeToYAML

func SerializeToYAML(w io.Writer, v any) error

SerializeToYAML serializes data to YAML format.

This function first marshals to JSON, then unmarshals and encodes to YAML. This approach ensures that:

  1. Structs from third-party libraries or generated code that only include json: struct tags (without yaml: tags) are correctly marshaled
  2. JSON and YAML marshaling produce consistent output, both respecting the same json: tags and omitempty directives

func TryUnwrapFile

func TryUnwrapFile(w io.Writer) io.Writer

TryUnwrapFile attempts to extract the underlying *os.File from a writer. If the writer is a *TermWriter, the embedded *os.File is returned; if it is already an *os.File, it is returned as-is. Otherwise the original writer is returned unchanged. This is needed when passing writers to exec.Cmd, since os/exec only passes file descriptors directly to child processes when the writer is an *os.File. Any other io.Writer causes os/exec to create a pipe, which breaks TTY detection in the child process.

Types

type Duration

type Duration struct {
	time.Duration
}

Duration is a wrapper around time.Duration that allows it to be marshalled to/from JSON via the standard duration string format.

func (Duration) MarshalText

func (d Duration) MarshalText() ([]byte, error)

Implements the encoding.TextMarshaler interface.

func (*Duration) UnmarshalText

func (d *Duration) UnmarshalText(b []byte) error

Implements the encoding.TextUnmarshaler interface.

type TermWriter

type TermWriter struct {
	*os.File
	ColorProfile *colorprofile.Writer
}

TermWriter embeds *os.File so that Fd(), Read(), and Close() are promoted automatically (satisfying bubbletea's term.File interface for terminal detection), while overriding Write() to route through a colorprofile.Writer for automatic color downsampling.

func NewTermWriter

func NewTermWriter(f *os.File) *TermWriter

NewTermWriter creates a TermWriter that writes through a colorprofile.Writer while exposing the underlying file's terminal capabilities.

func (*TermWriter) Write

func (tw *TermWriter) Write(p []byte) (int, error)

Jump to

Keyboard shortcuts

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