output

package
v0.48.3 Latest Latest
Warning

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

Go to latest
Published: Mar 12, 2024 License: MIT Imports: 22 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 string, origin config.Origin) 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

	// CacheLocation prints the cache location.
	CacheLocation(loc string) error

	// CacheStat prints cache info. Set arg size to -1 to indicate
	// that the size of the cache could not be calculated.
	CacheStat(loc string, enabled bool, size int64) error
}

ConfigWriter prints config.

type ErrorWriter

type ErrorWriter interface {
	// Error outputs error conditions. It's possible that systemErr and
	// humanErr differ; systemErr is the error that occurred, and humanErr
	// is the error that should be presented to the user.
	Error(systemErr, humanErr error)
}

ErrorWriter outputs errors.

type MetadataWriter

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

	// SourceMetadata writes the source metadata.
	SourceMetadata(srcMeta *metadata.Source, showSchema bool) error

	// DBProperties writes the DB properties.
	DBProperties(props map[string]any) error

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

	// Catalogs writes the list of catalogs.
	Catalogs(currentCatalog string, catalogs []string) error

	// Schemata writes the list of schemas.
	Schemata(currentSchema string, schemas []*metadata.Schema) error
}

MetadataWriter can output metadata.

type NewRecordWriterFunc added in v0.36.0

type NewRecordWriterFunc func(out io.Writer, pr *Printing) RecordWriter

NewRecordWriterFunc is a func type that returns an output.RecordWriter.

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 {
	// FormatDatetime formats a timestamp e.g. 2020-11-12T13:14:15Z.
	// Defaults to timez.DefaultDatetime.
	FormatDatetime func(time time.Time) string

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

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

	// 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

	// Diff contains colors for diff output.
	Diff *diffdoc.Colors

	// Disabled is the color for disabled elements.
	Disabled *color.Color

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

	// Enabled is the color for enabled elements.
	Enabled *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

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

	// Null is the color for null.
	Null *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

	// Stack is the color for stack traces.
	Stack *color.Color

	// StackError is the color for errors attached to a stack trace.
	StackError *color.Color

	// StackErrorType is the color for the error types attached to a stack trace.
	StackErrorType *color.Color

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

	// Success is the color for success elements.
	Success *color.Color

	// Warning is the color for warning elements.
	Warning *color.Color

	// NewBufferFn returns a new [ioz.Buffer] instance; it should be preferred
	// over [bytes.Buffer] when dealing large/unbounded data.
	NewBufferFn func() ioz.Buffer

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

	// ExcelDatetimeFormat is the format string for datetime values.
	// See excelw.OptDatetimeFormat.
	ExcelDatetimeFormat string

	// ExcelDateFormat is the format string for date values.
	// See excelw.OptDateFormat.
	ExcelDateFormat string

	// ExcelTimeFormat is the format string for time values.
	// See excelw.OptTimeFormat.
	ExcelTimeFormat string

	// 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

	// Redact indicates that sensitive fields (such as passwords)
	// should be redacted (hidden/masked).
	Redact bool

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

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

	// FormatDateAsNumber indicates that date values should be
	// rendered as naked numbers (instead of as a string) if possible.
	// See cli.OptDateFormatAsNumber.
	FormatDateAsNumber bool
	// 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) Clone added in v0.36.0

func (pr *Printing) Clone() *Printing

Clone returns a clone of pr.

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(ctx context.Context, recMeta record.Meta) error

	// WriteRecords writes rec to the destination.
	WriteRecords(ctx context.Context, recs []record.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(ctx context.Context) error

	// Close closes the writer after flushing any internal buffer.
	Close(ctx context.Context) 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(ctx context.Context, rw RecordWriter) *RecordWriterAdapter

NewRecordWriterAdapter returns a new RecordWriterAdapter. The size of the internal buffer is controlled by driver.OptTuningRecChanSize.

func (*RecordWriterAdapter) Open

func (w *RecordWriterAdapter) Open(ctx context.Context, cancelFn context.CancelFunc,
	recMeta record.Meta,
) (chan<- record.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

	// Added is called when src is added to the collection.
	Added(coll *source.Collection, src *source.Source) error

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

	// Moved is called when a source is moved from old to nu.
	Moved(coll *source.Collection, old, nu *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(bi buildinfo.Info, latestVersion string, si hostinfo.Info) error
}

VersionWriter prints the CLI version.

type Writers added in v0.36.0

type Writers struct {
	// PrOut is the printing config for stdout.
	PrOut *Printing

	// PrErr is the printing config for stderr.
	PrErr *Printing

	Record   RecordWriter
	Metadata MetadataWriter
	Source   SourceWriter
	Error    ErrorWriter
	Ping     PingWriter
	Version  VersionWriter
	Config   ConfigWriter
}

Writers is a container for the various output Writers.

Directories

Path Synopsis
Package commonw contains miscellaneous common output writer functionality.
Package commonw contains miscellaneous common output writer functionality.
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 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