output

package
v0.35.0 Latest Latest
Warning

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

Go to latest
Published: May 11, 2023 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package output provides interfaces and implementations for outputting data and messages. All sq command output should be via one of the writer interfaces defined in this package. The RecordWriterAdapter type provides a bridge between the asynchronous libsq.RecordWriter interface and the simpler synchronous RecordWriter interface defined here.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ConfigWriter added in v0.34.0

type ConfigWriter interface {
	// Location prints the config location. The origin may be empty, or one
	// of "flag", "env", "default".
	Location(loc, origin string) error

	// Opt prints a single options.Opt.
	Opt(o options.Options, opt options.Opt) error

	// Options prints config options.
	Options(reg *options.Registry, o options.Options) error

	// SetOption is called when an option is set.
	SetOption(o options.Options, opt options.Opt) error

	// UnsetOption is called when an option is unset.
	UnsetOption(opt options.Opt) error
}

ConfigWriter prints config.

type ErrorWriter

type ErrorWriter interface {
	// Error outputs err.
	Error(err error)
}

ErrorWriter outputs errors.

type MetadataWriter

type MetadataWriter interface {
	// TableMetadata writes the table metadata.
	TableMetadata(tblMeta *source.TableMetadata) error

	// SourceMetadata writes the source metadata.
	SourceMetadata(srcMeta *source.Metadata) error

	// DriverMetadata writes the metadata for the drivers.
	DriverMetadata(drvrs []driver.Metadata) error
}

MetadataWriter can output metadata.

type PingWriter

type PingWriter interface {
	// Open opens the writer to write the supplied sources.
	Open(srcs []*source.Source) error

	// Result prints a ping result. The ping succeeded if
	// err is nil. If err is context.DeadlineExceeded, the d
	// arg will be the timeout value.
	Result(src *source.Source, d time.Duration, err error) error

	// Close is called after all results have been received.
	Close() error
}

PingWriter writes ping results.

type Printing added in v0.34.0

type Printing struct {

	// FlushThreshold is the size in bytes after which an output writer
	// should flush any internal buffer.
	FlushThreshold int

	// ShowHeader indicates that a header (e.g. a header row) should
	// be printed where applicable.
	ShowHeader bool

	// Verbose indicates that verbose output should be printed where
	// applicable.
	Verbose bool

	// Compact indicates that output should not be pretty-printed.
	// Typically this means indentation, new lines, etc., but
	// varies by output format.
	Compact bool

	// Indent is the indent string to use when pretty-printing,
	// typically two spaces.
	Indent string

	// Redact indicates that sensitive fields (such as passwords)
	// should be redacted (hidden/masked).
	//
	// TODO: Redact is not being honored by the writers.
	Redact bool

	// FormatDatetime formats a timestamp e.g. 2020-11-12T13:14:15Z.
	// Defaults to timez.DefaultDatetime.
	FormatDatetime func(time time.Time) string

	// FormatDatetimeAsNumber indicates that datetime values should be
	// rendered as naked numbers (instead of as a string) if possible.
	// See cli.OptDatetimeFormatAsNumber.
	FormatDatetimeAsNumber bool

	// FormatTime formats a time of day, e.g. 13:14:15.
	// Defaults to timez.DefaultTime.
	FormatTime func(time time.Time) string

	// FormatTimeAsNumber indicates that time values should be
	// rendered as naked numbers (instead of as a string) if possible.
	// See cli.OptTimeFormatAsNumber.
	FormatTimeAsNumber bool

	// FormatDate formats a date, e.g. 2020-11-12.
	// Defaults to timez.DefaultDate.
	FormatDate func(time time.Time) string

	// FormatDateAsNumber indicates that date values should be
	// rendered as naked numbers (instead of as a string) if possible.
	// See cli.OptDateFormatAsNumber.
	FormatDateAsNumber bool

	// Active is the color for an active handle (or group, etc).
	Active *color.Color

	// Bold is the color for bold elements. Frequently Punc will just be color.Bold.
	Bold *color.Color

	// Bool is the color for boolean values.
	Bool *color.Color

	// Bytes is the color for byte / binary values.
	Bytes *color.Color

	// Datetime is the color for time-related values.
	Datetime *color.Color

	// Duration is the color for time duration values.
	Duration *color.Color

	// Error is the color for error elements such as an error message.
	Error *color.Color

	// Faint is the color for faint elements - the opposite of Hilite.
	Faint *color.Color

	// Handle is the color for source handles such as "@sakila"
	Handle *color.Color

	// Header is the color for header elements in a table.
	Header *color.Color

	// Hilite is the color for highlighted elements.
	Hilite *color.Color

	// Key is the color for keys such as a JSON field name.
	Key *color.Color

	// Location is the color for Source.Location values.
	Location *color.Color

	// Null is the color for null.
	Null *color.Color

	// Normal is the default color.
	Normal *color.Color

	// Number is the color for number values, including int, float, decimal etc.
	Number *color.Color

	// Punc is the color for punctuation such as colons, braces, etc.
	Punc *color.Color

	// String is the color for string values.
	String *color.Color

	// Success is the color for success elements.
	Success *color.Color
	// contains filtered or unexported fields
}

Printing describes color and pretty-printing options.

func NewPrinting added in v0.34.0

func NewPrinting() *Printing

NewPrinting returns a Printing instance. Color and pretty-print are enabled. The default indent is two spaces.

func (*Printing) EnableColor added in v0.34.0

func (pr *Printing) EnableColor(enable bool)

EnableColor enables or disables all colors.

func (*Printing) IsMonochrome added in v0.34.0

func (pr *Printing) IsMonochrome() bool

IsMonochrome returns true if in monochrome (no color) mode. Default is false (color enabled) for a new instance.

func (*Printing) LogValue added in v0.34.0

func (pr *Printing) LogValue() slog.Value

LogValue implements slog.LogValuer.

type RecordWriter

type RecordWriter interface {
	// Open instructs the writer to prepare to write records
	// described by recMeta.
	Open(recMeta sqlz.RecordMeta) error

	// WriteRecords writes rec to the destination.
	WriteRecords(recs []sqlz.Record) error

	// Flush advises the writer to flush any internal
	// buffer. Note that the writer may implement an independent
	// flushing strategy, or may not buffer at all.
	Flush() error

	// Close closes the writer after flushing any internal buffer.
	Close() error
}

RecordWriter is an interface for writing records to a destination. In effect, it is a synchronous counterpart to the asynchronous libsq.RecordWriter interface. Being a synchronous interface, it is less tricky to implement than libsq.RecordWriter. The RecordWriterAdapter type defined in this package bridges the two interfaces.

The Open method must be invoked before WriteRecords. Close must be invoked when all records are written. The Flush method advises the writer to flush any internal buffers.

type RecordWriterAdapter

type RecordWriterAdapter struct {

	// FlushAfterN indicates that the writer's Flush method
	// should be invoked after N invocations of WriteRecords.
	// A value of 0 will flush every time a record is written.
	// Set to -1 to disable.
	FlushAfterN int64

	// FlushAfterDuration controls whether the writer's Flush method
	// is invoked periodically. A duration <= 0 disables periodic flushing.
	FlushAfterDuration time.Duration
	// contains filtered or unexported fields
}

RecordWriterAdapter implements libsq.RecordWriter and wraps an output.RecordWriter instance, providing a bridge between the asynchronous libsq.RecordWriter and synchronous output.RecordWriter interfaces.

Note that a writer implementation such as the JSON or CSV writer could directly implement libsq.RecordWriter. But that interface is non-trivial to implement, hence this bridge type.

The FlushAfterN and FlushAfterDuration fields control flushing of the writer.

func NewRecordWriterAdapter

func NewRecordWriterAdapter(rw RecordWriter) *RecordWriterAdapter

NewRecordWriterAdapter returns a new RecordWriterAdapter.

func (*RecordWriterAdapter) Open

func (w *RecordWriterAdapter) Open(ctx context.Context, cancelFn context.CancelFunc,
	recMeta sqlz.RecordMeta,
) (chan<- sqlz.Record, <-chan error, error)

Open implements libsq.RecordWriter.

func (*RecordWriterAdapter) Wait

func (w *RecordWriterAdapter) Wait() (written int64, err error)

Wait implements libsq.RecordWriter.

type SourceWriter

type SourceWriter interface {
	// Collection outputs details of the collection. Specifically it prints
	// the sources from coll's active group.
	Collection(coll *source.Collection) error

	// Source outputs details of the source.
	Source(coll *source.Collection, src *source.Source) error

	// Removed is called when sources are removed from the collection.
	Removed(srcs ...*source.Source) error

	// Group prints the group.
	Group(group *source.Group) error

	// SetActiveGroup is called when the group is set.
	SetActiveGroup(group *source.Group) error

	// Groups prints a list of groups.
	Groups(tree *source.Group) error
}

SourceWriter can output data source details.

type VersionWriter added in v0.21.0

type VersionWriter interface {
	// Version prints version info. Arg latestVersion is the latest
	// version available from the homebrew repository. The value
	// may be empty.
	Version(info buildinfo.BuildInfo, latestVersion string) error
}

VersionWriter prints the CLI version.

Directories

Path Synopsis
Package csvw implements writers for CSV.
Package csvw implements writers for CSV.
Package htmlw implements a RecordWriter for HTML.
Package htmlw implements a RecordWriter for HTML.
Package jsonw implements output writers for JSON.
Package jsonw implements output writers for JSON.
Package markdownw implements writers for Markdown.
Package markdownw implements writers for Markdown.
Package outputx contains extensions to pkg output, and helpers for implementing output writers.
Package outputx contains extensions to pkg output, and helpers for implementing output writers.
Package tablew implements text table output writers.
Package tablew implements text table output writers.
internal
Package tablewriter creates & generates text based table
Package tablewriter creates & generates text based table
Package xlsxw implements output writers for Microsoft Excel.
Package xlsxw implements output writers for Microsoft Excel.
Package xmlw implements output writers for XML.
Package xmlw implements output writers for XML.
Package yamlw implements output writers for YAML.
Package yamlw implements output writers for YAML.

Jump to

Keyboard shortcuts

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