cli

package
v0.0.0-...-f9543a5 Latest Latest
Warning

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

Go to latest
Published: Oct 25, 2023 License: GPL-3.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var AnsiColorCodeRx = regexp.MustCompile("\x1B" + `\[[0-9;]*m`)

AnsiColorCodeRx is a regexp that matches ANSI escape sequences of the type SGR.

Functions

func DefaultCommandRunner

func DefaultCommandRunner(c Command, stdin io.Reader, stdout, stderr io.Writer) error

DefaultCommandRunner is a CommandRunner that actually executes the command.

func SetupInterface

func SetupInterface(stdin io.Reader, stdout, stderr io.Writer, commandRunner CommandRunner)

SetupInterface prepares the Interface instance with nonstandard file streams or a nonstandard CommandRunner. This is only required for unit tests.

Types

type Choice

type Choice struct {
	//If given, the choice can be selected by pressing the key that
	//produces this character.
	Shortcut byte
	//The display string that describes this choice.
	Text string
	//The string to return from Implementation.Query().
	Return string
}

Choice is a thing that the user can choose during Query().

type Command

type Command struct {
	Program []string
	WorkDir string
}

Command describes a command that can be run using the methods in the Implementation interface.

type CommandRunner

type CommandRunner func(c Command, stdin io.Reader, stdout, stderr io.Writer) error

CommandRunner is a function that can execute commands given to it. This interface is only useful for unit tests; the default CommandRunner suffices for all regular operation.

type Implementation

type Implementation struct {

	//If this flag is set, only ShowResult() will write into stdout; everything
	//else that usually goes to stdout goes to stderr instead.
	//
	//This is useful when gofu is expected to output a certain value to stdout
	//which is used by the next program in the pipe, and additional output from
	//subprocesses could confuse the stdout handler.
	StdoutProtected bool
	// contains filtered or unexported fields
}

Implementation wraps access to the CLI, including input, output and subprocesses.

var Interface *Implementation

Interface wraps access to the CLI, including input, output and subprocesses.

func (*Implementation) CaptureStdout

func (i *Implementation) CaptureStdout(c Command) (string, error)

CaptureStdout executes the given command on the same stderr and captures its stdout.

func (*Implementation) Confirm

func (i *Implementation) Confirm(question string) (bool, error)

Confirm displays a yes/no question and returns whether the user answered "yes".

func (*Implementation) Query

func (i *Implementation) Query(prompt string, choices ...Choice) (string, error)

Query displays a question and a set of answers and allows the user to select one of the answers. Returns the Return attribute of the selected Choice.

func (*Implementation) ReadLine

func (i *Implementation) ReadLine(prompt string) (string, error)

ReadLine reads a line from stdin (if tty: uses canonical mode).

func (*Implementation) Run

func (i *Implementation) Run(c Command) error

Run executes the given command on the same stdout and stderr.

func (*Implementation) ShowError

func (i *Implementation) ShowError(str string)

ShowError displays an error message on stderr.

func (*Implementation) ShowProgress

func (i *Implementation) ShowProgress(str string)

ShowProgress displays a progress message on stderr.

func (*Implementation) ShowResult

func (i *Implementation) ShowResult(str string)

ShowResult displays the result of a computation on stdout.

func (*Implementation) ShowResultsSorted

func (i *Implementation) ShowResultsSorted(strs []string)

ShowResultsSorted calls ShowResult() on each of the results after sorting them.

func (*Implementation) ShowUsage

func (i *Implementation) ShowUsage(str string)

ShowUsage displays a usage synopsis on stderr.

func (*Implementation) ShowWarning

func (i *Implementation) ShowWarning(str string)

ShowWarning displays a warning message on stderr.

type TUI

type TUI interface {
	//ReadLine reads a line from stdin (if tty: uses canonical mode).
	ReadLine(prompt string) (string, error)
	//Confirm displays a yes/no question and returns whether the user answered "yes".
	Confirm(question string) (bool, error)
	//Query displays a question and a set of answers and allows the user to select
	//one of the answers. Returns the Return attribute of the selected Choice.
	Query(prompt string, choices ...Choice) (string, error)
	//Print writes the given string (potentially including ANSI escape codes) to
	//the given writer. At this point, it can be decided whether to strip out the
	//ANSI escape codes.
	Print(w io.Writer, msg string)
}

TUI provides the interactive parts of the cli.Implementation, so that these can be easily swapped out for mock implementations in unit tests.

Jump to

Keyboard shortcuts

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