output

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Apr 24, 2026 License: MIT Imports: 23 Imported by: 0

Documentation

Overview

Package output provides output formatting for matter-cli commands. It supports table, JSON, and YAML formats with automatic TTY detection.

Index

Constants

This section is empty.

Variables

View Source
var (

	// StyleSuccess renders text in green.
	StyleSuccess = lipgloss.NewStyle().Foreground(colorGreen)
	// StyleError renders text in red and bold.
	StyleError = lipgloss.NewStyle().Foreground(colorRed).Bold(true)
	// StyleWarning renders text in yellow.
	StyleWarning = lipgloss.NewStyle().Foreground(colorYellow)
	// StyleInfo renders text in blue.
	StyleInfo = lipgloss.NewStyle().Foreground(colorBlue)
	// StyleBold renders text in bold.
	StyleBold = lipgloss.NewStyle().Bold(true)
	// StyleDim renders text with reduced intensity using the terminal's own
	// faint/dim attribute (ANSI SGR 2) rather than a fixed gray color.
	StyleDim = lipgloss.NewStyle().Faint(true)

	// StyleLabel renders a key/label (e.g. "Vendor:") in cyan bold.
	StyleLabel = lipgloss.NewStyle().Foreground(colorCyan).Bold(true)
	// StyleValue renders a value in light gray (ANSI 7).
	StyleValue = lipgloss.NewStyle().Foreground(colorLightGray)
	// StyleAccent renders text in magenta (used for IDs, hex values).
	StyleAccent = lipgloss.NewStyle().Foreground(colorMagenta)
	// StyleHeader renders table/section headers in blue bold.
	StyleHeader = lipgloss.NewStyle().Foreground(colorBlue).Bold(true)
	// StyleMuted renders secondary/less-important text in ANSI color 8
	// (Bright Black / Dark Gray). This is the canonical "comment" color across
	// terminal themes (Solarized, Nord, Dracula, Catppuccin, …) and gives a
	// reliably subdued appearance in both dark and light palettes, unlike the
	// faint attribute (SGR 2) which many terminals render inconsistently.
	StyleMuted = lipgloss.NewStyle().Foreground(colorGray)

	// StyleCommand renders command names in cyan.
	StyleCommand = lipgloss.NewStyle().Foreground(colorCyan)
	// StyleFlag renders flag names in yellow.
	StyleFlag = lipgloss.NewStyle().Foreground(colorYellow)
)

Functions

func Accent

func Accent(s string) string

Accent formats text in magenta (for IDs, hex values).

func Bold

func Bold(s string) string

Bold formats text in bold.

func Command

func Command(s string) string

Command formats a command name in cyan.

func Dim

func Dim(s string) string

Dim formats text with reduced intensity.

func Error

func Error(s string) string

Error formats text as an error message (red bold).

func ErrorIcon

func ErrorIcon() string

ErrorIcon returns a styled X mark.

func Flag

func Flag(s string) string

Flag formats a flag name in yellow.

func FormatRichTree

func FormatRichTree(w io.Writer, data *TreeData) error

FormatRichTree renders the device tree with depth controlled by data.Level:

1 – endpoints only
2 – endpoints + clusters
3 – endpoints + clusters + attribute names
4 – endpoints + clusters + attribute names + values

On wide terminals endpoints are shown side-by-side: a branch bar fans out from the node and vertical drop lines connect down to each endpoint column. On narrow terminals or pipes the classic top-down tree is used instead.

func FormatTree

func FormatTree(w io.Writer, node *store.Node) error

FormatTree writes a tree-style representation of a node to w, showing endpoints, clusters, and device types.

func Header(s string) string

Header formats section headers in blue bold.

func Info

func Info(s string) string

Info formats text as an informational message (blue).

func InfoIcon

func InfoIcon() string

InfoIcon returns a styled info marker.

func IsTTY

func IsTTY() bool

IsTTY reports whether stdout is a terminal.

func Label

func Label(s string) string

Label formats a key/label in cyan bold.

func Muted

func Muted(s string) string

Muted formats secondary/less important text.

func NoColor

func NoColor() bool

NoColor reports whether the NO_COLOR environment variable is set.

func RenderQRCode

func RenderQRCode(w io.Writer, payload string) bool

RenderQRCode writes a scannable 2D QR code for payload to w using Unicode half-block characters (two QR modules per terminal cell). Returns false when no visual QR was emitted — callers may still want to print the textual code.

The QR uses error-correction level M (matches Matter controllers such as Apple Home / Google Home which also encode at level M) and includes the standard 4-module quiet zone so phone-camera commissioners can lock onto it.

func RenderTreeSVG

func RenderTreeSVG(data *TreeData, filename string) error

RenderTreeSVG converts TreeData into a D2 diagram and renders it as SVG to the given file path.

func SpinnerIcon

func SpinnerIcon() string

SpinnerIcon returns a styled progress/spinner marker.

func Success

func Success(s string) string

Success formats text as a success message (green).

func SuccessIcon

func SuccessIcon() string

SuccessIcon returns a styled checkmark.

func TermWidth

func TermWidth() int

TermWidth returns the current terminal column width. Returns 0 if stdout is not a terminal or the width cannot be determined.

func Value

func Value(s string) string

Value formats a value in the default foreground color.

func Warning

func Warning(s string) string

Warning formats text as a warning message (yellow).

func WarningIcon

func WarningIcon() string

WarningIcon returns a styled warning marker.

Types

type FormatType

type FormatType string

FormatType identifies an output format.

const (
	// FormatTable renders data as an aligned text table.
	FormatTable FormatType = "table"
	// FormatJSON renders data as JSON.
	FormatJSON FormatType = "json"
	// FormatYAML renders data as YAML.
	FormatYAML FormatType = "yaml"
)

type Formatter

type Formatter interface {
	// Format writes the formatted representation of data to w.
	Format(w io.Writer, data any) error
}

Formatter formats data for output.

func New

func New(format string) Formatter

New returns a Formatter for the given format type. If format is empty, it selects table for TTY and JSON for pipes.

type JSONFormatter

type JSONFormatter struct {
	Pretty bool
}

JSONFormatter formats data as JSON. When Pretty is true, the output is indented for human readability; otherwise it is compact for piping.

func (*JSONFormatter) Format

func (f *JSONFormatter) Format(w io.Writer, data any) error

Format writes data as JSON to w.

type LogHandler

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

LogHandler is a custom slog.Handler that produces compact, colored log output with timestamps, aligned level names, and level-based coloring.

23:41:21 DEBUG  controller: sending          bytes=42 to=192.168.1.100:5540
23:41:21 WARN   controller: receive error    err=timeout

func NewLogHandler

func NewLogHandler(w io.Writer, level slog.Leveler) *LogHandler

NewLogHandler returns a handler that writes colored, compact log lines to w.

func (*LogHandler) Enabled

func (h *LogHandler) Enabled(_ context.Context, level slog.Level) bool

func (*LogHandler) Handle

func (h *LogHandler) Handle(_ context.Context, r slog.Record) error

func (*LogHandler) WithAttrs

func (h *LogHandler) WithAttrs(attrs []slog.Attr) slog.Handler

func (*LogHandler) WithGroup

func (h *LogHandler) WithGroup(_ string) slog.Handler

type Stepper

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

Stepper manages step-by-step progress output with optional spinner animation.

Three modes based on context:

  • Verbose: delegates all output to slog so steps and debug logs share one format.
  • Interactive (TTY, non-verbose): animated spinner for in-progress, green dot on completion.
  • Piped (non-TTY, non-verbose): static lines with dots to stdout.

When silent is true (NewProgressStepper), completed steps leave no permanent trace: in animate mode the spinner line is overwritten in-place; in static mode nothing is printed at all.

Every completed step and terminal call (Success/Fail) appends its elapsed duration as muted text, e.g. "● Establishing PASE session 1.2s".

func NewProgressStepper

func NewProgressStepper(w io.Writer, verbose bool) *Stepper

NewProgressStepper is like NewStepper but uses silent mode: completed steps leave no permanent trace. In animate (TTY) mode the spinner overwrites a single line continuously. In static (pipe) mode nothing is printed at all.

func NewStepper

func NewStepper(w io.Writer, verbose bool) *Stepper

NewStepper creates a Stepper that writes to w. When verbose is true, all output is routed through slog for a unified log format. Otherwise, the stepper uses a spinner animation on TTY or static dots when piped.

func (*Stepper) Clear

func (s *Stepper) Clear()

Clear stops the current step (marking it successful) without printing an additional status line. Use this before rendering output so the spinner is cleanly stopped and the terminal cursor is left on a fresh line.

func (*Stepper) Fail

func (s *Stepper) Fail(msg string)

Fail marks the current step as failed and prints an error message with the total elapsed time since the first Step() call (or since this call if no prior step was ever started).

func (*Stepper) Step

func (s *Stepper) Step(msg string)

Step marks the previous step as complete (green) and starts a new step.

func (*Stepper) Success

func (s *Stepper) Success(msg string)

Success marks the current step as complete and prints a success message with the total elapsed time since the first Step() call (or since this call if no prior step was ever started).

type TableData

type TableData struct {
	Headers []string
	Rows    [][]string
}

TableData represents data that can be rendered as a table.

type TableFormatter

type TableFormatter struct{}

TableFormatter formats TableData as a styled text table using lipgloss.

func (*TableFormatter) Format

func (f *TableFormatter) Format(w io.Writer, data any) error

Format writes data as a styled table to w. If data is not a *TableData, it falls back to fmt.Fprintf.

type TreeAttribute

type TreeAttribute struct {
	ID    uint32
	Name  string // resolved display name, or "0x%04X" fallback
	Value string // empty unless level >= 4
	Err   string // non-empty if read failed (e.g. "<timeout>")
}

TreeAttribute holds a single attribute entry in the device tree.

type TreeCluster

type TreeCluster struct {
	ID      uint32
	Name    string
	Side    string          // "server" or "client"
	ListErr string          // non-empty if AttributeList read failed
	Attrs   []TreeAttribute // populated for level >= 3
}

TreeCluster holds a cluster entry with optional attribute data.

type TreeData

type TreeData struct {
	NodeID               uint64
	NodeName             string
	VendorID             uint16
	ProductID            uint16
	SpecificationVersion uint32
	SoftwareVersion      uint32
	SerialNumber         string
	LastAddress          string
	Endpoints            []TreeEndpoint
	Level                int
}

TreeData is the full device tree data passed to FormatRichTree.

type TreeEndpoint

type TreeEndpoint struct {
	ID          uint16
	DeviceTypes []store.DeviceType
	Clusters    []TreeCluster
}

TreeEndpoint holds an endpoint entry with cluster data.

Jump to

Keyboard shortcuts

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