ui

package
v6.47.2+incompatible Latest Latest
Warning

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

Go to latest
Published: Nov 5, 2019 License: Apache-2.0 Imports: 27 Imported by: 0

Documentation

Overview

Package ui will provide hooks into STDOUT, STDERR and STDIN. It will also handle translation as necessary.

This package is explicitly designed for the CF CLI and is *not* to be used by any package outside of the commands package.

Index

Constants

View Source
const DefaultTableSpacePadding = 3

DefaultTableSpacePadding is the default space padding in tables.

View Source
const LogTimestampFormat = "2006-01-02T15:04:05.00-0700"

LogTimestampFormat is the timestamp formatting for log lines.

View Source
const RedactedValue = "[PRIVATE DATA HIDDEN]"

RedactedValue is the text that is displayed for redacted content. (eg authorization tokens, passwords, etc.)

Variables

View Source
var ErrInvalidIndex = errors.New("invalid list index")
View Source
var ErrValueMissmatch = errors.New("values provided were of different types")

Functions

func ParseLocale

func ParseLocale(locale string) (string, error)

ParseLocale will return a locale formatted as "<language code>-<region code>" for all non-Chinese lanagues. For Chinese, it will return "zh-<script>", defaulting to "hant" if script is unspecified.

func RedactHeaders

func RedactHeaders(header http.Header) http.Header

func SanitizeJSON

func SanitizeJSON(raw []byte) ([]byte, error)

Types

type Change

type Change struct {
	Header       string
	CurrentValue interface{}
	NewValue     interface{}
	HiddenValue  bool
}

type Config

type Config interface {
	// ColorEnabled enables or disabled color
	ColorEnabled() configv3.ColorSetting
	// Locale is the language to translate the output to
	Locale() string
	// IsTTY returns true when the ui has a TTY
	IsTTY() bool
	// TerminalWidth returns the width of the terminal
	TerminalWidth() int
}

Config is the UI configuration.

type Exiter

type Exiter interface {
	Exit(code int)
}

Exiter hides os.Exit for testing purposes

type Interactor

type Interactor interface {
	NewInteraction(prompt string, choices ...interact.Choice) Resolver
}

Interactor hides interact.NewInteraction for testing purposes

type InvalidChoiceError

type InvalidChoiceError struct {
	Choice string
}

func (InvalidChoiceError) Error

func (InvalidChoiceError) Error() string

type LocaleReader

type LocaleReader interface {
	Locale() string
}

type LogMessage

type LogMessage interface {
	Message() string
	Type() string
	Timestamp() time.Time
	SourceType() string
	SourceInstance() string
}

LogMessage is a log response representing one to many joined lines of a log message.

type RequestLoggerFileWriter

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

func (*RequestLoggerFileWriter) DisplayBody

func (display *RequestLoggerFileWriter) DisplayBody([]byte) error

func (*RequestLoggerFileWriter) DisplayDump

func (display *RequestLoggerFileWriter) DisplayDump(dump string) error

func (*RequestLoggerFileWriter) DisplayHeader

func (display *RequestLoggerFileWriter) DisplayHeader(name string, value string) error

func (*RequestLoggerFileWriter) DisplayHost

func (display *RequestLoggerFileWriter) DisplayHost(name string) error

func (*RequestLoggerFileWriter) DisplayJSONBody

func (display *RequestLoggerFileWriter) DisplayJSONBody(body []byte) error

func (*RequestLoggerFileWriter) DisplayMessage

func (display *RequestLoggerFileWriter) DisplayMessage(msg string) error

func (*RequestLoggerFileWriter) DisplayRequestHeader

func (display *RequestLoggerFileWriter) DisplayRequestHeader(method string, uri string, httpProtocol string) error

func (*RequestLoggerFileWriter) DisplayResponseHeader

func (display *RequestLoggerFileWriter) DisplayResponseHeader(httpProtocol string, status string) error

func (*RequestLoggerFileWriter) DisplayType

func (display *RequestLoggerFileWriter) DisplayType(name string, requestDate time.Time) error

func (*RequestLoggerFileWriter) HandleInternalError

func (display *RequestLoggerFileWriter) HandleInternalError(err error)

func (*RequestLoggerFileWriter) Start

func (display *RequestLoggerFileWriter) Start() error

func (*RequestLoggerFileWriter) Stop

func (display *RequestLoggerFileWriter) Stop() error

type RequestLoggerTerminalDisplay

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

func (*RequestLoggerTerminalDisplay) DisplayBody

func (display *RequestLoggerTerminalDisplay) DisplayBody([]byte) error

func (*RequestLoggerTerminalDisplay) DisplayDump

func (display *RequestLoggerTerminalDisplay) DisplayDump(dump string) error

func (*RequestLoggerTerminalDisplay) DisplayHeader

func (display *RequestLoggerTerminalDisplay) DisplayHeader(name string, value string) error

func (*RequestLoggerTerminalDisplay) DisplayHost

func (display *RequestLoggerTerminalDisplay) DisplayHost(name string) error

func (*RequestLoggerTerminalDisplay) DisplayJSONBody

func (display *RequestLoggerTerminalDisplay) DisplayJSONBody(body []byte) error

func (*RequestLoggerTerminalDisplay) DisplayMessage

func (display *RequestLoggerTerminalDisplay) DisplayMessage(msg string) error

func (*RequestLoggerTerminalDisplay) DisplayRequestHeader

func (display *RequestLoggerTerminalDisplay) DisplayRequestHeader(method string, uri string, httpProtocol string) error

func (*RequestLoggerTerminalDisplay) DisplayResponseHeader

func (display *RequestLoggerTerminalDisplay) DisplayResponseHeader(httpProtocol string, status string) error

func (*RequestLoggerTerminalDisplay) DisplayType

func (display *RequestLoggerTerminalDisplay) DisplayType(name string, requestDate time.Time) error

func (*RequestLoggerTerminalDisplay) HandleInternalError

func (display *RequestLoggerTerminalDisplay) HandleInternalError(err error)

func (*RequestLoggerTerminalDisplay) Start

func (display *RequestLoggerTerminalDisplay) Start() error

func (*RequestLoggerTerminalDisplay) Stop

func (display *RequestLoggerTerminalDisplay) Stop() error

type Resolver

type Resolver interface {
	Resolve(dst interface{}) error
	SetIn(io.Reader)
	SetOut(io.Writer)
}

type TranslateFunc

type TranslateFunc func(translationID string, args ...interface{}) string

TranslateFunc returns the translation of the string identified by translationID.

If there is no translation for translationID, then the translationID is used as the translation.

func GetTranslationFunc

func GetTranslationFunc(reader LocaleReader) (TranslateFunc, error)

GetTranslationFunc will return back a function that can be used to translate strings into the currently set locale.

type TranslationEntry

type TranslationEntry struct {
	// ID is the original English string.
	ID string `json:"id"`
	// Translation is the translation of the ID.
	Translation string `json:"translation"`
}

TranslationEntry is the expected format of the translation file.

type UI

type UI struct {
	// In is the input buffer
	In io.Reader
	// Out is the output buffer
	Out io.Writer
	// OutForInteration is the output buffer when working with go-interact. When
	// working with Windows, color.Output does not work with TTY detection. So
	// real STDOUT is required or go-interact will not properly work.
	OutForInteration io.Writer
	// Err is the error buffer
	Err io.Writer

	Exiter Exiter

	Interactor Interactor

	IsTTY         bool
	TerminalWidth int

	TimezoneLocation *time.Location
	// contains filtered or unexported fields
}

UI is interface to interact with the user

func NewTestUI

func NewTestUI(in io.Reader, out io.Writer, err io.Writer) *UI

NewTestUI will return a UI object where Out, In, and Err are customizable, and colors are disabled

func NewUI

func NewUI(config Config) (*UI, error)

NewUI will return a UI object where Out is set to STDOUT, In is set to STDIN, and Err is set to STDERR

func (*UI) DeferText

func (ui *UI) DeferText(template string, templateValues ...map[string]interface{})

DeferText translates the template, substitutes in templateValues, and Enqueues the output to be presented later via FlushDeferred. Only the first map in templateValues is used.

func (*UI) DisplayBoolPrompt

func (ui *UI) DisplayBoolPrompt(defaultResponse bool, template string, templateValues ...map[string]interface{}) (bool, error)

DisplayBoolPrompt outputs the prompt and waits for user input. It only allows for a boolean response. A default boolean response can be set with defaultResponse.

func (*UI) DisplayChangeForPush

func (ui *UI) DisplayChangeForPush(header string, stringTypePadding int, hiddenValue bool, originalValue interface{}, newValue interface{}) error

DisplayChangeForPush will display the header and old/new value with the appropriately red/green minuses and pluses.

func (*UI) DisplayChangesForPush

func (ui *UI) DisplayChangesForPush(changeSet []Change) error

DisplayChangesForPush will display the set of changes via DisplayChangeForPush in the order given.

func (*UI) DisplayDeprecationWarning

func (ui *UI) DisplayDeprecationWarning()

func (*UI) DisplayError

func (ui *UI) DisplayError(err error)

DisplayError outputs the translated error message to ui.Err if the error satisfies TranslatableError, otherwise it outputs the original error message to ui.Err. It also outputs "FAILED" in bold red to ui.Out.

func (*UI) DisplayFileDeprecationWarning

func (ui *UI) DisplayFileDeprecationWarning()

func (*UI) DisplayHeader

func (ui *UI) DisplayHeader(text string)

DisplayHeader translates the header, bolds and adds the default color to the header, and outputs the result to ui.Out.

func (*UI) DisplayInstancesTableForApp

func (ui *UI) DisplayInstancesTableForApp(table [][]string)

func (*UI) DisplayKeyValueTable

func (ui *UI) DisplayKeyValueTable(prefix string, table [][]string, padding int)

DisplayKeyValueTable outputs a matrix of strings as a table to UI.Out. Prefix will be prepended to each row and padding adds the specified number of spaces between columns. The final columns may wrap to multiple lines but will still be confined to the last column. Wrapping will occur on word boundaries.

func (*UI) DisplayKeyValueTableForApp

func (ui *UI) DisplayKeyValueTableForApp(table [][]string)

func (*UI) DisplayLogMessage

func (ui *UI) DisplayLogMessage(message LogMessage, displayHeader bool)

DisplayLogMessage formats and outputs a given log message.

func (*UI) DisplayNewline

func (ui *UI) DisplayNewline()

DisplayNewline outputs a newline to UI.Out.

func (*UI) DisplayNonWrappingTable

func (ui *UI) DisplayNonWrappingTable(prefix string, table [][]string, padding int)

DisplayNonWrappingTable outputs a matrix of strings as a table to UI.Out. Prefix will be prepended to each row and padding adds the specified number of spaces between columns.

func (*UI) DisplayOK

func (ui *UI) DisplayOK()

DisplayOK outputs a bold green translated "OK" to UI.Out.

func (*UI) DisplayOptionalTextPrompt

func (ui *UI) DisplayOptionalTextPrompt(defaultValue string, template string, templateValues ...map[string]interface{}) (string, error)

DisplayOptionalTextPrompt outputs the prompt and waits for user input.

func (*UI) DisplayPasswordPrompt

func (ui *UI) DisplayPasswordPrompt(template string, templateValues ...map[string]interface{}) (string, error)

DisplayPasswordPrompt outputs the prompt and waits for user input. Hides user's response from the screen.

func (*UI) DisplayTableWithHeader

func (ui *UI) DisplayTableWithHeader(prefix string, table [][]string, padding int)

DisplayTableWithHeader outputs a simple non-wrapping table with bolded headers.

func (*UI) DisplayText

func (ui *UI) DisplayText(template string, templateValues ...map[string]interface{})

DisplayText translates the template, substitutes in templateValues, and outputs the result to ui.Out. Only the first map in templateValues is used.

func (*UI) DisplayTextMenu

func (ui *UI) DisplayTextMenu(choices []string, promptTemplate string, templateValues ...map[string]interface{}) (string, error)

DisplayTextMenu lets the user choose from a list of options, either by name or by number.

func (*UI) DisplayTextPrompt

func (ui *UI) DisplayTextPrompt(template string, templateValues ...map[string]interface{}) (string, error)

DisplayTextPrompt outputs the prompt and waits for user input.

func (*UI) DisplayTextWithBold

func (ui *UI) DisplayTextWithBold(template string, templateValues ...map[string]interface{})

DisplayTextWithBold translates the template, bolds the templateValues, substitutes templateValues into the template, and outputs the result to ui.Out. Only the first map in templateValues is used.

func (*UI) DisplayTextWithFlavor

func (ui *UI) DisplayTextWithFlavor(template string, templateValues ...map[string]interface{})

DisplayTextWithFlavor translates the template, bolds and adds cyan color to templateValues, substitutes templateValues into the template, and outputs the result to ui.Out. Only the first map in templateValues is used.

func (*UI) DisplayWarning

func (ui *UI) DisplayWarning(template string, templateValues ...map[string]interface{})

DisplayWarning translates the warning, substitutes in templateValues, and outputs to ui.Err. Only the first map in templateValues is used.

func (*UI) DisplayWarningV7

func (ui *UI) DisplayWarningV7(template string, templateValues ...map[string]interface{})

DisplayWarningV7 translates the warning, substitutes in templateValues, and outputs to ui.Err. Only the first map in templateValues is used. This command has one fewer newline than DisplayWarning. Use it before an OK message in V7.

func (*UI) DisplayWarnings

func (ui *UI) DisplayWarnings(warnings []string)

Translates warnings and outputs them to ui.Err. Prints each warning with a trailing newline. Prints the final warning with two trailing newlines.

func (*UI) DisplayWarningsV7

func (ui *UI) DisplayWarningsV7(warnings []string)

Translates warnings and outputs them to ui.Err. Prints each warning with a trailing newline.

func (*UI) FlushDeferred

func (ui *UI) FlushDeferred()

FlushDeferred displays text previously deferred (using DeferText) to the UI's `Out`.

func (*UI) GetErr

func (ui *UI) GetErr() io.Writer

GetErr returns the error writer.

func (*UI) GetIn

func (ui *UI) GetIn() io.Reader

GetIn returns the input reader.

func (*UI) GetOut

func (ui *UI) GetOut() io.Writer

GetOut returns the output writer. Same as `Writer`.

func (*UI) RequestLoggerFileWriter

func (ui *UI) RequestLoggerFileWriter(filePaths []string) *RequestLoggerFileWriter

RequestLoggerFileWriter returns a RequestLoggerFileWriter that cannot overwrite another RequestLoggerFileWriter.

func (*UI) RequestLoggerTerminalDisplay

func (ui *UI) RequestLoggerTerminalDisplay() *RequestLoggerTerminalDisplay

RequestLoggerTerminalDisplay returns a RequestLoggerTerminalDisplay that cannot overwrite another RequestLoggerTerminalDisplay or the current display.

func (*UI) TranslateText

func (ui *UI) TranslateText(template string, templateValues ...map[string]interface{}) string

TranslateText passes the template through an internationalization function to translate it to a pre-configured language, and returns the template with templateValues substituted in. Only the first map in templateValues is used.

func (*UI) UserFriendlyDate

func (ui *UI) UserFriendlyDate(input time.Time) string

UserFriendlyDate converts the time to UTC and then formats it to ISO8601.

func (*UI) Writer

func (ui *UI) Writer() io.Writer

Writer returns the output writer. Same as `GetOut`.

Directories

Path Synopsis
Code generated by counterfeiter.
Code generated by counterfeiter.

Jump to

Keyboard shortcuts

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