Documentation
¶
Index ¶
- Variables
- func AnySlice[T any](in []T) []any
- func ConvertSliceToAny[T any](ts []T) []any
- func ConvertStringSlicePtr[T ~string](ss []string) *[]T
- func Deref[T any](val *T) T
- func DerefStr[T ~string](val *T) string
- func ExpandHome(path string) string
- func ExpandPath(path string) string
- func FormatStorageMB(mb int) string
- func IsCI() bool
- func Must[T any](v T, err error) T
- func PtrIfNonNil[T ~[]E, E any](val T) *T
- func ReadAll(ctx context.Context, stdin io.Reader) (string, error)
- func ReadLine(ctx context.Context, stdin io.Reader) (string, error)
- func SerializeToJSON(w io.Writer, v any) error
- func SerializeToYAML(w io.Writer, v any) error
- func TryUnwrapFile(w io.Writer) io.Writer
- type Duration
- type TermWriter
Constants ¶
This section is empty.
Variables ¶
var IsTerminal = func(v any) bool { if f, ok := v.(fileDescriptor); 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.
var ReadPassword = func(ctx context.Context, stdin io.Reader) (string, error) { f, ok := stdin.(*os.File) if !ok { return "", errors.New("stdin is not a terminal") } state, err := term.GetState(int(f.Fd())) if err != nil { return "", fmt.Errorf("error getting current terminal state: %w", err) } defer term.Restore(int(f.Fd()), state) return readString(ctx, func() (string, error) { val, err := term.ReadPassword(int(f.Fd())) if err != nil { return "", err } return string(val), nil }) }
ReadPassword reads a password from stdin without echoing it. It is a variable so that tests can inject input (similar to IsTerminal), since the real implementation requires stdin to be an *os.File.
Functions ¶
func ConvertSliceToAny ¶
ConvertSliceToAny converts a slice of some type to slice of any. Returns nil if the input slice is nil.
func ConvertStringSlicePtr ¶
ConvertStringSlicePtr converts a slice of strings to a pointer to another string-like type. Returns nil if the input slice is nil.
func ExpandHome ¶ added in v0.29.0
ExpandHome expands a leading tilde (~ or ~/...) to the home directory and cleans the result, leaving environment variables untouched. An empty path stays empty, and the tilde is left in place if the home directory is unknown.
func ExpandPath ¶
ExpandPath expands environment variables and a leading tilde in a path, and cleans the result. An empty path stays empty.
func FormatStorageMB ¶ added in v0.29.0
FormatStorageMB formats a size in mebibytes, as the Tiger Cloud API reports it, in the largest unit that keeps the value at least 1, to one decimal place ("512 MB", "1.2 GB", "843.5 GB").
func IsCI ¶
func IsCI() bool
IsCI determines if the current execution context is within a known CI/CD system. This is based on https://github.com/watson/ci-info/blob/HEAD/index.js.
func PtrIfNonNil ¶
func PtrIfNonNil[T ~[]E, E any](val T) *T
func SerializeToYAML ¶
SerializeToYAML serializes data to YAML format.
This function first marshals to JSON, then unmarshals and encodes to YAML. This approach ensures that:
- Structs from third-party libraries or generated code that only include json: struct tags (without yaml: tags) are correctly marshaled
- JSON and YAML marshaling produce consistent output, both respecting the same json: tags and omitempty directives
func TryUnwrapFile ¶ added in v0.27.0
TryUnwrapFile attempts to extract the underlying *os.File from a writer. If the writer is a *TermWriter, the embedded *os.File is returned; 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 ¶
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 ¶
Implements the encoding.TextMarshaler interface.
func (*Duration) UnmarshalText ¶
Implements the encoding.TextUnmarshaler interface.
type TermWriter ¶ added in v0.27.0
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 ¶ added in v0.27.0
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) DisableColor ¶ added in v0.27.0
func (tw *TermWriter) DisableColor()
DisableColor makes the writer strip all ANSI color sequences.