output

package
v0.24.4 Latest Latest
Warning

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

Go to latest
Published: Mar 15, 2023 License: MIT Imports: 10 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

View Source
const FlushThreshold = 1000

FlushThreshold is the size in bytes after which a writer should flush any internal buffer.

Variables

This section is empty.

Functions

This section is empty.

Types

type ErrorWriter

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

ErrorWriter outputs errors.

type Formatting

type Formatting struct {

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

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

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

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

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

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

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

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

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

	// Handle is the color for source handles such as "@my_db"
	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

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

	// Bold is the color for bold elements.
	Bold *color.Color

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

	// Punc is the color for punctuation such as colons, braces, etc.
	// Frequently Punc will just be color.Bold.
	Punc *color.Color
	// contains filtered or unexported fields
}

Formatting describes color and pretty-printing options.

func NewFormatting

func NewFormatting() *Formatting

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

func (*Formatting) EnableColor

func (f *Formatting) EnableColor(enable bool)

EnableColor enables or disables all colors.

func (*Formatting) IsMonochrome

func (f *Formatting) IsMonochrome() bool

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

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 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 {
	// SourceSet outputs details of the source set.
	SourceSet(srcs *source.Set) error

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

	// Removed is called when sources are removed from the source set.
	Removed(srcs ...*source.Source) 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 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.

Jump to

Keyboard shortcuts

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