Documentation
¶
Overview ¶
Package stripes is a streaming, ANSI-colored pretty-printer for structured data formats: JSON, YAML, XML, HTML, CSV, protobuf, parquet, Dockerfiles, the Go toolchain's flat files, markdown, source code, txtar archives, WebAssembly, and plain text.
All renderers share a common shape:
renderer(w, r, stripes.DefaultStyles)
where w is the styled output sink, r is the raw input stream, and the Styles value selects colors and layout.
Format registry ¶
The root package holds only the registry and shared primitives; each format lives in its own sub-package that registers itself at init. Import the sub-packages for the formats you need:
import (
"github.com/firetiger-oss/stripes"
_ "github.com/firetiger-oss/stripes/json"
_ "github.com/firetiger-oss/stripes/yaml"
)
or import github.com/firetiger-oss/stripes/all for the full set. Func dispatches by MIME type against the registered formats, and Detect sniffs an unknown stream into a content-type string using their registered filenames, extensions, magic bytes, and heuristics. A format whose sub-package has not been imported is simply absent: Func returns nil and Detect falls through to the next candidate.
Third-party code can register additional formats with Register.
The companion command github.com/firetiger-oss/stripes/cmd/stripes is a file viewer that wraps these primitives with format auto-detection, TTY-aware coloring, and built-in paging.
Index ¶
- Variables
- func Detect(name string, peek []byte) string
- func IsANSIEnabled(s *Styles) bool
- func IsNumeric(s string) bool
- func ListProfiles() []string
- func NewPrefixWriter(writer io.Writer, prefix string) io.Writer
- func Plain(w io.Writer, r io.Reader, _ *Styles)
- func Register(f Format)
- func RegisterFilenameFallback(fn func(name string) (contentType string, ok bool))
- func Simple(r Renderer) func(map[string]string, string) Renderer
- func Text(w io.Writer, r io.Reader, styles *Styles)
- type Format
- type Profile
- type ProfileStyles
- type Renderer
- type StyleSpec
- type Styles
Constants ¶
This section is empty.
Variables ¶
var DefaultStyles = &Styles{ Name: lipgloss.NewStyle().Foreground(lipgloss.Color("12")).Bold(true), Text: lipgloss.NewStyle().Foreground(lipgloss.Color("7")), String: lipgloss.NewStyle().Foreground(lipgloss.Color("2")), Number: lipgloss.NewStyle().Foreground(lipgloss.Color("7")), Boolean: lipgloss.NewStyle().Foreground(lipgloss.Color("13")), Null: lipgloss.NewStyle().Foreground(lipgloss.Color("8")), Syntax: lipgloss.NewStyle().Foreground(lipgloss.Color("7")).Bold(true), Code: lipgloss.NewStyle().Foreground(lipgloss.Color("183")), Anchor: lipgloss.NewStyle().Foreground(lipgloss.Color("12")), Comment: lipgloss.NewStyle().Foreground(lipgloss.Color("8")).Faint(true), Title: lipgloss.NewStyle().Foreground(lipgloss.Color("7")).Bold(true), LineNumber: lipgloss.NewStyle().Foreground(lipgloss.Color("244")), Insertion: lipgloss.NewStyle().Foreground(lipgloss.Color("2")), Deletion: lipgloss.NewStyle().Foreground(lipgloss.Color("1")), Heading: [6]lipgloss.Style{ lipgloss.NewStyle().Foreground(lipgloss.Color("12")).Bold(true), lipgloss.NewStyle().Foreground(lipgloss.Color("12")).Bold(true), lipgloss.NewStyle().Foreground(lipgloss.Color("12")).Bold(true), lipgloss.NewStyle().Foreground(lipgloss.Color("4")).Bold(true), lipgloss.NewStyle().Foreground(lipgloss.Color("4")), lipgloss.NewStyle().Foreground(lipgloss.Color("4")).Faint(true), }, Columns: lipgloss.NewStyle().Bold(true), Rows: lipgloss.NewStyle(), Border: lipgloss.NormalBorder(), Indent: " ", Width: 80, }
DefaultStyles provides a grayscale styling theme using shades of grey, dimming, and bold
var ErrProfileNotFound = errors.New("profile not found")
ErrProfileNotFound is returned by LoadProfile when no profile with the given name exists in either the user config dir or the bundled set.
Functions ¶
func Detect ¶
Detect resolves a content type for a stream.
The lookup order is:
- Exact filename match against registered [Format.Filenames].
- Registered [Format.MatchPath] callbacks (path-aware rules); these run before the extension match so e.g. vendor/modules.txt is not swallowed by a ".txt" registration.
- File-extension match against registered [Format.Extensions].
- Registered filename fallbacks (see RegisterFilenameFallback).
- Magic-byte prefixes from registered [Format.MagicBytes].
- Registered [Format.Detect] content-shape heuristics.
- net/http.DetectContentType fallback.
- "text/plain" if nothing else matched.
The returned string is a MIME media type compatible with Func. Only formats whose sub-packages have been imported participate; an empty registry resolves everything to "text/plain" (or the http fallback). Empty name and nil peek both return "text/plain".
func IsANSIEnabled ¶ added in v0.3.0
IsANSIEnabled reports whether styles emit ANSI escape codes. It samples a few styles that all renderers exercise; if any of them produces an escape byte, color output is considered enabled. Used by renderers that switch between styled and plain output paths (notably markdown's fenced-code blocks and source-code highlighting).
func IsNumeric ¶ added in v0.3.0
IsNumeric reports whether s looks like a number — optionally signed, with `.` or `,` as decimals, scientific notation, and a recognised unit suffix (`%`, time units, byte sizes, SI prefixes, `kg`). Empty strings are treated as numeric (a placeholder for missing values), so callers that care about presence must check separately.
func ListProfiles ¶ added in v0.3.0
func ListProfiles() []string
ListProfiles returns the sorted union of profile names available in the user config directory and the bundled set.
func Register ¶ added in v0.3.0
func Register(f Format)
Register adds f to the global format registry. It panics on a missing Name, ContentType, or RendererFor; on a duplicate Name or ContentType; or on duplicate Filenames/Extensions. Safe to call from init().
func RegisterFilenameFallback ¶ added in v0.3.0
RegisterFilenameFallback installs a name-based content-type resolver consulted by Detect after registered Filenames, Extensions, and MatchPath rules miss but before any MagicBytes or Detect callbacks fire. Used by stripes/code to plug chroma's filename-based language detection in without pulling chroma into the root package.
Types ¶
type Format ¶ added in v0.3.0
type Format struct {
// Name is a short identifier, e.g. "json". Must be unique across
// registered formats. The stripes CLI accepts Name as a --format
// alias.
Name string
// ContentType is the canonical MIME media type, e.g. "application/json".
// Must be unique.
ContentType string
// Filenames are exact basenames that imply this format
// (e.g. "Dockerfile", "go.mod"). Optional.
Filenames []string
// Extensions are lowercase file extensions including the dot
// (e.g. ".json"). Optional.
Extensions []string
// MagicBytes are exact byte prefixes that identify this format on
// content sniffing (e.g. {0x00, 'a', 's', 'm'} for WebAssembly).
// Optional.
MagicBytes [][]byte
// MatchPath is consulted by [Detect] after Filenames and Extensions
// miss but before MagicBytes and Detect. Used for path-aware rules
// such as vendor/modules.txt that need the parent directory name.
// Optional.
MatchPath func(path string) bool
// Detect inspects up to ~512 bytes of stream content and returns true
// when the bytes match this format. Runs after all MagicBytes checks
// across every registered format. Optional.
Detect func(peek []byte) bool
// RendererFor returns the [Renderer] for a parsed content type.
// params are the MIME parameters from contentType (e.g.
// map[string]string{"lang": "go"} for "text/x-source-code; lang=go").
// schemaURL is the optional schema reference passed to [Func]
// (used by protobuf for descriptor lookup; ignored elsewhere).
// Formats that ignore both arguments should wrap a static Renderer
// with [Simple]. Required.
RendererFor func(params map[string]string, schemaURL string) Renderer
}
Format describes one renderable content type. Sub-packages register at init time so that Func and Detect resolve them. The zero value is not valid; Name, ContentType, and RendererFor are required.
type Profile ¶ added in v0.3.0
type Profile struct {
Description string `yaml:"description,omitempty"`
CodeStyle string `yaml:"code-style,omitempty"`
Styles ProfileStyles `yaml:"styles"`
Border string `yaml:"border,omitempty"`
Indent string `yaml:"indent,omitempty"`
}
Profile is the on-disk schema for a stripes color profile.
func LoadProfile ¶ added in v0.3.0
LoadProfile resolves a profile reference. The reference may be:
- A bare name (e.g. "iterm-default") — searched first in $XDG_CONFIG_HOME/stripes/profiles (defaulting to ~/.config/stripes/profiles), then in the bundled set.
- A file path (anything containing a separator, starting with ~, absolute, or ending in .yaml/.yml) — loaded directly. Tilde expansion is applied.
ErrProfileNotFound wraps the underlying error when the reference resolves to nothing.
type ProfileStyles ¶ added in v0.3.0
type ProfileStyles struct {
Name StyleSpec `yaml:"name"`
Text StyleSpec `yaml:"text"`
String StyleSpec `yaml:"string"`
Number StyleSpec `yaml:"number"`
Boolean StyleSpec `yaml:"boolean"`
Null StyleSpec `yaml:"null"`
Syntax StyleSpec `yaml:"syntax"`
Code StyleSpec `yaml:"code"`
Anchor StyleSpec `yaml:"anchor"`
Comment StyleSpec `yaml:"comment"`
Title StyleSpec `yaml:"title"`
LineNumber StyleSpec `yaml:"line-number"`
Columns StyleSpec `yaml:"columns"`
Rows StyleSpec `yaml:"rows"`
Headings []StyleSpec `yaml:"headings"`
}
ProfileStyles is the per-element style table within a Profile.
type Renderer ¶
Renderer writes styled output for a single input format. All format functions in this package (JSON, YAML, XML, ...) match this signature.
func Func ¶
Func returns the Renderer matching contentType (a MIME media type), or nil if the content type is unsupported by the formats currently registered. For application/protobuf, schemaURL is interpreted as the full message name used to look up the descriptor in [protoregistry.GlobalTypes] / [protoregistry.GlobalFiles]; for other formats it is ignored.
To populate the registry, import the per-format sub-packages whose renderers you need (e.g. `_ "github.com/firetiger-oss/stripes/json"`), or `_ "github.com/firetiger-oss/stripes/all"` for the full set.
func WithLineNumbers ¶ added in v0.3.0
WithLineNumbers returns a Renderer that wraps r and prepends a right-aligned, styled line number to every output line. Each rendered line is prefixed with the styled gutter "<digits>│" followed by a single unstyled space before the content, e.g. " 12│ content". The column width is sized to fit the largest line number, so all numbers align in a single fixed column. Styling is taken from styles.LineNumber.
For renderers whose output preserves a 1:1 line correspondence with the input (Code, Text, Plain, Dockerfile, Markdown, HTML, XML, YAML), the numbers correspond to source input lines. For reformatting renderers (JSON pretty-print, CSV table) the numbers run sequentially over the rendered output.
The inner renderer's output is buffered in memory to determine the column width before any bytes are written to w. This is consistent with how stripes typically renders human-viewed files.
type StyleSpec ¶ added in v0.3.0
type StyleSpec struct {
Color string `yaml:"color,omitempty"`
Background string `yaml:"background,omitempty"`
Bold bool `yaml:"bold,omitempty"`
Faint bool `yaml:"faint,omitempty"`
Italic bool `yaml:"italic,omitempty"`
Underline bool `yaml:"underline,omitempty"`
}
StyleSpec describes one styled element. Color may be a hex value (#rrggbb), an ANSI palette index ("0".."255"), or any color name recognised by lipgloss.
type Styles ¶
type Styles struct {
Name lipgloss.Style
Text lipgloss.Style
String lipgloss.Style
Number lipgloss.Style
Boolean lipgloss.Style
Null lipgloss.Style
Syntax lipgloss.Style
Code lipgloss.Style
Anchor lipgloss.Style
Comment lipgloss.Style
Title lipgloss.Style
LineNumber lipgloss.Style
Insertion lipgloss.Style
Deletion lipgloss.Style
Heading [6]lipgloss.Style
Columns lipgloss.Style
Rows lipgloss.Style
Border lipgloss.Border
Indent string
Width int
// CodeStyle names the chroma style used for syntax highlighting in
// code blocks (see github.com/alecthomas/chroma/v2/styles). Empty
// falls back to "github-dark".
CodeStyle string
}
Styles defines the styling configuration for rendering various data types
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
Package all imports every built-in stripes format sub-package for side-effect registration.
|
Package all imports every built-in stripes format sub-package for side-effect registration. |
|
cmd
|
|
|
stripes
command
Command stripes pretty-prints structured data with ANSI styling and optional paging.
|
Command stripes pretty-prints structured data with ANSI styling and optional paging. |
|
Package cobra applies a restrained ANSI palette — aligned with github.com/firetiger-oss/stripes — to help, usage, and error output of commands built with github.com/spf13/cobra.
|
Package cobra applies a restrained ANSI palette — aligned with github.com/firetiger-oss/stripes — to help, usage, and error output of commands built with github.com/spf13/cobra. |
|
Package code registers the source-code renderer with the stripes registry.
|
Package code registers the source-code renderer with the stripes registry. |
|
Package csv registers the CSV renderer with the stripes registry.
|
Package csv registers the CSV renderer with the stripes registry. |
|
Package diff registers the unified / git diff renderer with the stripes registry.
|
Package diff registers the unified / git diff renderer with the stripes registry. |
|
Package dockerfile registers the Dockerfile renderer with the stripes registry.
|
Package dockerfile registers the Dockerfile renderer with the stripes registry. |
|
Package gomod registers stripes renderers for the Go toolchain's flat-file formats: go.mod, go.sum, go.work, and vendor/modules.txt.
|
Package gomod registers stripes renderers for the Go toolchain's flat-file formats: go.mod, go.sum, go.work, and vendor/modules.txt. |
|
Package html registers the HTML renderer with the stripes registry.
|
Package html registers the HTML renderer with the stripes registry. |
|
Package json registers the JSON renderer with the stripes registry.
|
Package json registers the JSON renderer with the stripes registry. |
|
Package markdown registers the Markdown renderer with the stripes registry.
|
Package markdown registers the Markdown renderer with the stripes registry. |
|
Package parquet registers the Parquet renderer with the stripes registry.
|
Package parquet registers the Parquet renderer with the stripes registry. |
|
Package protobuf registers the Protobuf renderer with the stripes registry.
|
Package protobuf registers the Protobuf renderer with the stripes registry. |
|
Package table renders typed iterators of struct values as styled CLI tables.
|
Package table renders typed iterators of struct values as styled CLI tables. |
|
Package txtar registers the txtar archive renderer with the stripes registry.
|
Package txtar registers the txtar archive renderer with the stripes registry. |
|
Package xml registers the XML renderer with the stripes registry.
|
Package xml registers the XML renderer with the stripes registry. |
|
Package yaml registers the YAML renderer with the stripes registry.
|
Package yaml registers the YAML renderer with the stripes registry. |
