stripes

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: May 3, 2026 License: MIT Imports: 35 Imported by: 0

README

stripes CI Go Reference

stripes

Streaming pretty-printer for structured data formats — JSON, YAML, XML, HTML, CSV, Dockerfile, markdown, protobuf, plain text, source code (via chroma) — usable as a Go library or as a standalone CLI.

Motivation

Pretty-printers in the Unix toolchain are fragmented: jq for JSON, yq for YAML, browsers for HTML, no canonical option for protobuf. They share neither flags nor styling, which makes uniform terminal output hard to assemble inside a single Go program emitting mixed structured payloads (logs, traces, debug dumps, RPC responses).

stripes collapses that surface to a single library and CLI:

  • Renderers stream — bytes are emitted as they arrive, no whole-input load. Works for tail -f, large objects, and HTTP response bodies.
  • Format dispatch is by MIME type, so any program already carrying a content type can pick a renderer without parsing its own input.
  • The same library powers the CLI; no separate process required when a Go program wants colored output.
  • One binary, one set of flags, one styling model across all supported formats.

Library

stripes.Func

Pick a Renderer by MIME type. Returns nil if the content type is unsupported.

import "github.com/firetiger-oss/stripes"

renderer := stripes.Func("application/json", "")
renderer(os.Stdout, body, stripes.DefaultStyles)

For application/protobuf, pass the message's full name as the second argument so the dynamic descriptor lookup can resolve fields.

stripes.Detect

Resolve a content type from a filename and/or the leading bytes of a stream.

buf, _ := bufio.NewReader(input).Peek(512)
ct := stripes.Detect("payload.yaml", buf)
renderer := stripes.Func(ct, "")
Format functions
Content type Function
application/json JSON
application/yaml YAML
application/xml XML
text/html HTML
text/csv CSV
text/x-dockerfile Dockerfile
text/markdown Markdown
text/x-source-code Code (factory; pass chroma lexer name)
text/plain Text
application/protobuf Protobuf
(passthrough) Plain

All share the Renderer signature: func(io.Writer, io.Reader, *Styles).

stripes.Styles

Pass stripes.DefaultStyles for the built-in grayscale theme, a Clone() to customize, or &stripes.Styles{} for unstyled output.

CLI

go install github.com/firetiger-oss/stripes/cmd/stripes@latest
$ stripes --help
Usage: stripes [flags] [file]

Pretty-print structured data (JSON, YAML, XML, HTML, CSV, Dockerfile, markdown,
protobuf, text, source code) with ANSI colors and optional paging.

Flags:
  -f, --format string         json|yaml|xml|html|csv|dockerfile|markdown|text|code|protobuf|auto (default auto)
      --content-type string   Override MIME type (e.g. application/vnd.foo+json)
      --schema string         Schema URL (protobuf full name)
      --color string          always|never|auto (default auto)
  -w, --width int             Output width (default: terminal width or 100)
  -p, --pager string          Pager command (e.g. "less -R", "bat --plain").
                              Use "cat" to bypass paging on a TTY.

Pager resolution: -p flag > $STRIPES_PAGER > $PAGER > "less -R"
Color is auto-disabled when NO_COLOR is set or stdout is not a terminal.
Shell aliases
alias scat='stripes'              # cat-like, with paging on a TTY
alias spcat='stripes -p cat'      # always-stream, never page

Contributing

Contributions are welcome! To get started:

  1. Ensure you have Go 1.25+ installed
  2. Run go test ./... to verify tests pass

Please report bugs and feature requests via GitHub Issues.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Documentation

Overview

Package stripes is a streaming, ANSI-colored pretty-printer for structured data formats: JSON, YAML, XML, HTML, CSV, protobuf, and plain text.

All renderers share a common shape:

stripes.JSON(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. Func dispatches by MIME type, and Detect sniffs an unknown stream into a content-type string.

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

Constants

This section is empty.

Variables

View Source
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("7")),
	Null:    lipgloss.NewStyle().Foreground(lipgloss.Color("8")),
	Syntax:  lipgloss.NewStyle().Foreground(lipgloss.Color("7")).Bold(true),
	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),
	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

Functions

func CSV

func CSV(w io.Writer, r io.Reader, styles *Styles)

func Detect

func Detect(name string, peek []byte) string

Detect resolves a content type for a stream.

The lookup order is:

  1. Filename extension (when name has a recognized extension).
  2. Magic-byte sniffing of peek (first ~512 bytes of the stream).
  3. net/http.DetectContentType fallback.
  4. "text/plain" if nothing else matched.

The returned string is a MIME media type compatible with Func. Empty name and nil peek both return "text/plain".

func Dockerfile added in v0.2.0

func Dockerfile(w io.Writer, r io.Reader, styles *Styles)

Dockerfile renders a Dockerfile (or Containerfile) with ANSI styling applied to instruction keywords, comments, flags, quoted strings, heredoc bodies, and line-continuation backslashes.

Parsing is done via github.com/moby/buildkit/frontend/dockerfile/parser so that line continuations, escape directives, parser directives, and heredocs are handled the same way docker build itself handles them.

func HTML

func HTML(w io.Writer, r io.Reader, styles *Styles)

func JSON

func JSON(w io.Writer, r io.Reader, styles *Styles)

func Markdown added in v0.2.0

func Markdown(w io.Writer, r io.Reader, styles *Styles)

func NewPrefixWriter

func NewPrefixWriter(writer io.Writer, prefix string) io.Writer

func Plain

func Plain(w io.Writer, r io.Reader, _ *Styles)

Plain renders content without any formatting or styling

func Text

func Text(w io.Writer, r io.Reader, styles *Styles)

func XML

func XML(w io.Writer, r io.Reader, styles *Styles)

func YAML

func YAML(w io.Writer, r io.Reader, styles *Styles)

Types

type Renderer

type Renderer func(io.Writer, io.Reader, *Styles)

Renderer writes styled output for a single input format. All format functions in this package (JSON, YAML, XML, ...) match this signature.

func Code added in v0.2.0

func Code(lang string) Renderer

Code returns a Renderer that highlights source code using the chroma lexer named lang. When lang is empty the renderer falls back to chroma's content-based language detection; if no lexer can be resolved the input is written verbatim.

func Func

func Func(contentType, schemaURL string) Renderer

Func returns the Renderer matching contentType (a MIME media type), or nil if the content type is unsupported. 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.

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
	Anchor  lipgloss.Style
	Comment lipgloss.Style
	Title   lipgloss.Style
	Columns lipgloss.Style
	Rows    lipgloss.Style
	Border  lipgloss.Border
	Indent  string
	Width   int
}

Styles defines the styling configuration for rendering various data types

func (*Styles) Clone

func (s *Styles) Clone() *Styles

Clone creates a copy of the Styles struct

Directories

Path Synopsis
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.

Jump to

Keyboard shortcuts

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