text

package
v1.7.1 Latest Latest
Warning

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

Go to latest
Published: Mar 14, 2022 License: Apache-2.0 Imports: 22 Imported by: 0

Documentation

Index

Constants

View Source
const DefaultTextWidth = 90

DefaultTextWidth is the width that should be passed to Wrap for most general-purpose blocks of text intended for the user.

Variables

View Source
var Bold = color.New(color.Bold).SprintFunc()

Bold is a Sprint-class function that makes the arguments bold.

View Source
var BoldGreen = color.New(color.Bold, color.FgGreen).SprintFunc()

BoldGreen is a Sprint-class function that makes the arguments bold and green.

View Source
var BoldRed = color.New(color.Bold, color.FgRed).SprintFunc()

BoldRed is a Sprint-class function that makes the arguments bold and red.

View Source
var BoldYellow = color.New(color.Bold, color.FgYellow).SprintFunc()

BoldYellow is a Sprint-class function that makes the arguments bold and yellow.

View Source
var Reset = color.New(color.Reset).SprintFunc()

Reset is a Sprint-class function that resets the color for the arguments.

Functions

func AskYesNo added in v1.4.0

func AskYesNo(w io.Writer, prompt string, r io.Reader) (bool, error)

AskYesNo is similar to Input, but the line read is coerced to one of true (yes and its variants) or false (no, its variants and anything else) on success.

func Break

func Break(w io.Writer)

Break simply writes a newline to the writer. It's intended to be used between blocks of text that would otherwise be adjacent, a sort of semantic markup.

func Description added in v0.3.0

func Description(w io.Writer, term, description string)

Description formats the output of a description item. A description item consists of a `term` and a `description`. Emphasis is placed on the `description` using Bold(). For example:

To compile the package, run:
    fastly compute build

func Error

func Error(w io.Writer, format string, args ...interface{})

Error is a wrapper for fmt.Fprintf with a bold red "ERROR: " prefix.

func Indent added in v0.39.2

func Indent(w io.Writer, indent uint, format string, args ...interface{})

Indent writes the help text to the writer using WrapIndent with DefaultTextWidth, suffixed by a newlines. It's intended to be used to provide detailed information, context, or help to the user.

func Info added in v0.13.0

func Info(w io.Writer, format string, args ...interface{})

Info is a wrapper for fmt.Fprintf with a bold "INFO: " prefix.

func Input

func Input(w io.Writer, prefix string, r io.Reader, validators ...func(string) error) (string, error)

Input prints the prefix to the writer, and then reads a single line from the reader, trimming writespace. The received line is passed to the validators, and if any of them return a non-nil error, the error is printed to the writer, and the input process happens again. Otherwise, the line is returned to the caller.

Input is intended to be used to take interactive input from the user.

func InputSecure

func InputSecure(w io.Writer, prefix string, r io.Reader, validators ...func(string) error) (string, error)

InputSecure is like Input but doesn't echo input back to the terminal, if and only if r is os.Stdin.

func LastFullLine

func LastFullLine(s string) string

LastFullLine returns the last full \n delimited line in s. That is, s must contain at least one \n for LastFullLine to return anything.

func Output

func Output(w io.Writer, format string, args ...interface{})

Output writes the help text to the writer using Wrap with DefaultTextWidth, suffixed by a newlines. It's intended to be used to provide detailed information, context, or help to the user.

func PrintBackend

func PrintBackend(out io.Writer, prefix string, b *fastly.Backend)

PrintBackend pretty prints a fastly.Backend structure in verbose format to a given io.Writer. Consumers can provide a prefix string which will be used as a prefix to each line, useful for indentation.

func PrintDictionary added in v0.21.0

func PrintDictionary(out io.Writer, prefix string, d *fastly.Dictionary)

PrintDictionary pretty prints a fastly.Dictionary structure in verbose format to a given io.Writer. Consumers can provide a prefix string which will be used as a prefix to each line, useful for indentation.

func PrintDictionaryItem added in v0.21.0

func PrintDictionaryItem(out io.Writer, prefix string, d *fastly.DictionaryItem)

PrintDictionaryItem pretty prints a fastly.DictionaryInfo structure in verbose format to a given io.Writer. Consumers can provide a prefix string which will be used as a prefix to each line, useful for indentation.

func PrintDictionaryItemKV added in v0.21.0

func PrintDictionaryItemKV(out io.Writer, prefix string, d *fastly.DictionaryItem)

PrintDictionaryItemKV pretty prints only the key/value pairs from a dictionary item.

func PrintHealthCheck

func PrintHealthCheck(out io.Writer, prefix string, h *fastly.HealthCheck)

PrintHealthCheck pretty prints a fastly.HealthCheck structure in verbose format to a given io.Writer. Consumers can provide a prefix string which will be used as a prefix to each line, useful for indentation.

func PrintService

func PrintService(out io.Writer, prefix string, s *fastly.Service)

PrintService pretty prints a fastly.Service structure in verbose format to a given io.Writer. Consumers can provide a prefix string which will be used as a prefix to each line, useful for indentation.

func PrintVersion

func PrintVersion(out io.Writer, indent string, v *fastly.Version)

PrintVersion pretty prints a fastly.Version structure in verbose format to a given io.Writer. Consumers can provide a prefix string which will be used as a prefix to each line, useful for indentation.

func Spin

func Spin(ctx context.Context, frames []rune, interval time.Duration, target Ticker) error

Spin calls Tick on the target with the relevant frame every interval. It returns when context is canceled, so should be called in its own goroutine.

func Success

func Success(w io.Writer, format string, args ...interface{})

Success is a wrapper for fmt.Fprintf with a bold green "SUCCESS: " prefix.

func Warning

func Warning(w io.Writer, format string, args ...interface{})

Warning is a wrapper for fmt.Fprintf with a bold yellow "WARNING: " prefix.

func Wrap

func Wrap(text string, width uint) string

Wrap a string at word boundaries with a maximum line length of width. Each newline-delimited line in the text is trimmed of whitespace before being added to the block for wrapping, which means strings can be declared in the source code with whatever leading indentation looks best in context. For example,

Wrap(`
    Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do
    eiusmod tempor incididunt ut labore et dolore magna aliqua. Dolor
    sed viverra ipsum nunc aliquet bibendum enim. In massa tempor nec
    feugiat.
`, 40)

Produces the output string

Lorem ipsum dolor sit amet, consectetur
adipiscing elit, sed do eiusmod tempor
incididunt ut labore et dolore magna
aliqua. Dolor sed viverra ipsum nunc
aliquet bibendum enim. In massa tempor
nec feugiat.

func WrapIndent added in v0.39.2

func WrapIndent(s string, lim uint, indent uint) string

WrapIndent a string at word boundaries with a maximum line length of width and indenting the lines by a specified number of spaces.

Types

type InteractiveProgress added in v0.38.0

type InteractiveProgress struct {
	// contains filtered or unexported fields
}

InteractiveProgress is an implementation of Progress that includes a spinner at the beginning of each Step, and where newline-delimited lines written via Write overwrite the current step line in the output.

func NewInteractiveProgress added in v0.38.0

func NewInteractiveProgress(output io.Writer, options ...Option) *InteractiveProgress

NewInteractiveProgress returns a InteractiveProgress outputting to the writer.

func (*InteractiveProgress) Done added in v0.38.0

func (p *InteractiveProgress) Done()

Done implements the Progress interface.

func (*InteractiveProgress) Fail added in v0.38.0

func (p *InteractiveProgress) Fail()

Fail implements the Progress interface.

func (*InteractiveProgress) Step added in v0.38.0

func (p *InteractiveProgress) Step(msg string)

Step implements the Progress interface.

func (*InteractiveProgress) Tick added in v0.38.0

func (p *InteractiveProgress) Tick(r rune)

Tick implements the Progress interface.

func (*InteractiveProgress) Write added in v0.38.0

func (p *InteractiveProgress) Write(buf []byte) (int, error)

Write implements the Progress interface, emitting each incoming byte slice to the internal buffer to be written to the terminal on the next tick.

type NullProgress added in v0.3.0

type NullProgress struct {
	// contains filtered or unexported fields
}

NullProgress is an implementation of Progress which discards everything written into it and produces no output.

func NewNullProgress added in v0.3.0

func NewNullProgress() *NullProgress

NewNullProgress returns a NullProgress.

func (*NullProgress) Done added in v0.3.0

func (p *NullProgress) Done()

Done implements the Progress interface. It's a no-op.

func (*NullProgress) Fail added in v0.3.0

func (p *NullProgress) Fail()

Fail implements the Progress interface. It's a no-op.

func (*NullProgress) Step added in v0.3.0

func (p *NullProgress) Step(msg string)

Step implements the Progress interface.

func (*NullProgress) Tick added in v0.3.0

func (p *NullProgress) Tick(r rune)

Tick implements the Progress interface. It's a no-opt

func (*NullProgress) Write added in v0.3.0

func (p *NullProgress) Write(buf []byte) (int, error)

Tick implements the Progress interface.

type Option added in v1.4.0

type Option func(*ProgressOptions)

Option represents optional configuration for a Progress type.

func WithReset added in v1.4.0

func WithReset() Option

type Progress

type Progress interface {
	io.Writer
	Tick(rune)
	Step(string)
	Done()
	Fail()
}

Progress is a producer contract, abstracting over the quiet and verbose Progress types. Consumers may use a Progress value in their code, and assign it based on the presence of a -v, --verbose flag. Callers are expected to call Step for each new major step of their procedural code, and Write with the verbose or detailed output of those steps. Callers must eventually call either Done or Fail, to signal success or failure respectively.

func NewProgress added in v0.38.0

func NewProgress(output io.Writer, verbose bool, options ...Option) Progress

NewProgress returns a Progress based on the given verbosity level or whether the current process is running in a terminal environment.

func ResetProgress added in v1.4.0

func ResetProgress(output io.Writer, verbose bool) Progress

ResetProgress wraps the NewProgress and passes through a restart option.

NOTE: A Progress sometimes needs to be marked as Done() so that other output can be printed to the terminal (otherwise the Progress will always overwrite the output). If we just call NewProgress again then we'll see an 'Initializing...' message which looks odd. Instead we can now reset the progress instead which will simply tell the Progress type not to set that step header.

type ProgressOptions added in v1.4.0

type ProgressOptions struct {
	// contains filtered or unexported fields
}

type QuietProgress

type QuietProgress struct {
	// contains filtered or unexported fields
}

QuietProgress is an implementation of Progress that attempts to be quiet in it's output. I.e. it only prints each Step as it progresses and discards any intermediary writes between steps. No spinners are used, therefore it's useful for non-TTY environiments, such as CI.

func NewQuietProgress

func NewQuietProgress(output io.Writer) *QuietProgress

NewQuietProgress returns a QuietProgress outputting to the writer.

func (*QuietProgress) Done

func (p *QuietProgress) Done()

Done implements the Progress interface. It's a no-op.

func (*QuietProgress) Fail

func (p *QuietProgress) Fail()

Fail implements the Progress interface. It's a no-op.

func (*QuietProgress) Step

func (p *QuietProgress) Step(msg string)

Step implements the Progress interface.

func (*QuietProgress) Tick

func (p *QuietProgress) Tick(r rune)

Tick implements the Progress interface. It's a no-op.

func (*QuietProgress) Write

func (p *QuietProgress) Write(buf []byte) (int, error)

Tick implements the Progress interface.

type Table

type Table struct {
	// contains filtered or unexported fields
}

Table wraps an instance of a tabwriter and provides helper methods to easily create a table, add a header, add rows and print to the writer.

func NewTable

func NewTable(w io.Writer) *Table

NewTable contructs a new Table.

func (*Table) AddHeader

func (t *Table) AddHeader(args ...interface{})

AddHeader writes a table header line.

func (*Table) AddLine

func (t *Table) AddLine(args ...interface{})

AddLine writes a new row to the table.

func (*Table) Print

func (t *Table) Print()

Print writes the table to the writer.

type Ticker

type Ticker interface {
	Tick(r rune)
}

Ticker is a small consumer contract for the Spin function, capturing part of the Progress interface.

type VerboseProgress

type VerboseProgress struct {
	// contains filtered or unexported fields
}

VerboseProgress is an implementation of Progress that treats Step and Write more or less the same: it simply pipes all output to the provided Writer. No spinners are used.

func NewVerboseProgress

func NewVerboseProgress(output io.Writer) *VerboseProgress

NewVerboseProgress returns a VerboseProgress outputting to the writer.

func (*VerboseProgress) Done

func (p *VerboseProgress) Done()

Done implements the Progress interface. It's a no-op.

func (*VerboseProgress) Fail

func (p *VerboseProgress) Fail()

Fail implements the Progress interface. It's a no-op.

func (*VerboseProgress) Step

func (p *VerboseProgress) Step(msg string)

Step implements the Progress interface.

func (*VerboseProgress) Tick

func (p *VerboseProgress) Tick(r rune)

Tick implements the Progress interface. It's a no-op.

func (*VerboseProgress) Write

func (p *VerboseProgress) Write(buf []byte) (int, error)

Tick implements the Progress interface.

Jump to

Keyboard shortcuts

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