cliutils

package
v0.2.9 Latest Latest
Warning

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

Go to latest
Published: Dec 5, 2025 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package cliutils holds helper functions for interacting with a user at a command-line interface.

Index

Constants

View Source
const (
	NoDefault = "[[twos.dev/winter/cliutils.NoDefault]]"
)

Variables

This section is empty.

Functions

func Ask

func Ask(question, dfault string, in io.Reader, out io.Writer) (answer string, err error)

Ask writes question to out with a visual treatment indicating a prompt, then reads from in until it reads a line break. It returns all text read until the line break (excluding it).

If dfault is NoDefault, when the returned text would be an empty string, the function repeats from the start until the returned text would be nonempty.

If dfault is any other string, when the returned text would be an empty string, dfault is returned instead.

question is implicitly passed through Sprintf for formatting.

If using Ask with os.Stdin and os.Stdout and no error is expected, use MustAsk instead.

func AskBool

func AskBool(question string, dfault bool, in io.Reader, out io.Writer) (answer bool, err error)

AskBool is like Ask but asks the user a yes/no question instead of a generic string question.

AskBool writes question to out with a visual treatment indicating a prompt, then reads from in until it reads a line break.

If the read text is "y", "Y", "yes", or similar; true is returned. If the read text is "n", "N", "no", or similar; false is returned. If the read text is the empty string, dfault is returned. Otherwise, the function repeats from the start until a sufficient answer is read.

question is implicitly passed through Sprintf for formatting.

func MustAsk

func MustAsk(question, dfault string) (answer string)

MustAsk is like Ask but panics on error and always interacts with stdin/stdout.

MustAsk writes question to stdout with a visual treatment indicating a prompt, then reads from stdin until it reads a line break. It returns all text read until the line break (excluding it).

If dfault is NoDefault, when the returned text would be an empty string, the function repeats from the start until the returned text would be nonempty.

If dfault is any other string, when the returned text would be an empty string, dfault is returned instead.

question is implicitly passed through Sprintf for formatting.

If any issue occurs, MustAsk panics. To handle errors or use other readers or writers, use Ask instead.

func Printf

func Printf(format string, args ...any) (int, error)

Printf is like fmt.Printf, but applies several visual treatments to make format pleasant to read at a terminal, especially when it spans multiple lines or paragraphs.

Specifically, single newlines and common indentation are ignored and the text is wrapped to 80 characters (allowing strings to be well-formatted in both source code and output), and limited Markdown support is applied.

func Sprintf

func Sprintf(format string, args ...any) string

Sprintf is like fmt.Sprintf, but applies visual treatments for pleasant reading at a terminal, especially when it spans multiple lines or paragraphs.

Specifically, single newlines and common indentation are ignored and the text is wrapped to 80 characters (allowing strings to be well-formatted in both source code and output), and limited Markdown support is applied.

Types

This section is empty.

Jump to

Keyboard shortcuts

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