printer

package
v0.1.15 Latest Latest
Warning

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

Go to latest
Published: Aug 24, 2025 License: BSD-3-Clause Imports: 8 Imported by: 0

Documentation

Index

Constants

View Source
const (
	Black = 30 + iota
	Red
	Green
	Yellow
	Blue
	Magenta
	Cyan
	White
	Gray = White

	ColorReset = 0
)

Standard color codes, any color code can be passed to `Rich` package-level function, when the destination terminal supports.

Variables

This section is empty.

Functions

func IsNop

func IsNop(w io.Writer) bool

IsNop can check wether an `w` io.Writer is a NopOutput.

func NopOutput

func NopOutput() io.Writer

NopOutput returns a writer that discards all writes.

func Rich

func Rich(text string, colorCode int, options ...RichOption) string

Rich returns a formatted string with color and style codes. If colors are disabled, returns the plain text.

func SupportsColor

func SupportsColor(w io.Writer) bool

SupportsColor determines if the output supports ANSI color codes.

func WriteRich

func WriteRich(w io.Writer, text string, colorCode int, options ...RichOption) (int, error)

WriteRich writes a formatted string with color and style to the writer.

Types

type Printer

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

Printer is a simple printer that manages multiple output writers and provides thread-safe atomic writes.

func NewPrinter

func NewPrinter(writer io.Writer) *Printer

NewPrinter creates a new Printer with the given initial writer.

func (*Printer) AddOutput

func (p *Printer) AddOutput(writers ...io.Writer)

AddOutput adds one or more writers to the printer.

func (*Printer) Clone

func (p *Printer) Clone() *Printer

Clone creates a deep copy of the Printer, including its writers and rich map.

func (*Printer) Print

func (p *Printer) Print(v any) (int, error)

Print writes the string representation of v to all writers.

func (*Printer) Printf

func (p *Printer) Printf(format string, a ...any) (int, error)

Printf writes a formatted string to all writers.

func (*Printer) Println

func (p *Printer) Println(v any) (int, error)

Println writes the string representation of v followed by a newline to all writers.

func (*Printer) Scan

func (p *Printer) Scan(r io.Reader) (cancel func())

Scan scans from the provided reader and writes lines to the printer. It returns a cancel function to stop the scanning operation.

func (*Printer) SetOutput

func (p *Printer) SetOutput(w io.Writer)

SetOutput replaces all current writers with the single provided writer.

func (*Printer) Terminal

func (p *Printer) Terminal() (*Printer, bool)

Terminal returns a new Printer that includes the writers that output destination is a terminal kind. If no terminal writers exist, it returns nil and false.

func (*Printer) TerminalOrStderr

func (p *Printer) TerminalOrStderr() io.Writer

TerminalOrStderr returns an io.Writer that includes the writers that output destination is a terminal kind. If no terminal writers exist, it returns os.Stderr.

func (*Printer) TerminalOrStdout

func (p *Printer) TerminalOrStdout() io.Writer

TerminalOrStdout returns an io.Writer that includes the writers that output destination is a terminal kind. If no terminal writers exist, it returns os.Stdout.

func (*Printer) Write

func (p *Printer) Write(data []byte) (int, error)

Write writes data to all registered writers atomically.

func (*Printer) WriteRich

func (p *Printer) WriteRich(text string, colorCode int, options ...RichOption) (int, error)

WriteRich writes a formatted string with color and style to all registered writers. It checks each writer's support for rich text and writes accordingly.

func (*Printer) WriteString

func (p *Printer) WriteString(s string) (n int, err error)

WriteString writes a string to all registered writers atomically.

type RichOption

type RichOption int

Style options for rich text formatting

const (
	Background RichOption = iota
	Underline
	Bold
)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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