colorjson

package module
v0.0.0-...-36675ef Latest Latest
Warning

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

Go to latest
Published: Jun 13, 2023 License: MIT Imports: 9 Imported by: 0

README

(Yet Another) colorjson

colorjson is a package for formatting and colorizing JSON output for the terminal.

Performance is not a priority of this package. It's intended to be used for low traffic output for human consumption.

Usage

go get github.com/abrander/colorjson
package main

import (
	"os"

	"github.com/abrander/colorjson"
)

func main() {
	data := struct {
		Name string
		Age  int
	}{
		Name: "John Doe",
		Age:  42,
	}

	encoder := colorjson.NewEncoder(os.Stdout, colorjson.Default)
	encoder.Encode(data)
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Default = &Settings{
	EndWithNewline: true,
	Newlines:       true,
	Indent:         "  ",
	Separator:      " ",
	Color:          nil,
}

Default contains the default settings used to colorize and format the JSON document.

View Source
var DefaultColors = &ColorSettings{
	Ident:  Cyan | Bold,
	String: Green,
	Number: Yellow,
	Null:   Grey,
	False:  Red,
	True:   Green,
}

DefaultColors is the default color scheme.

View Source
var Jq = &Settings{
	EndWithNewline: true,
	Newlines:       true,
	Indent:         "  ",
	Separator:      " ",
	Color:          JqColors,
}

Jq contains the settings used to colorize and format the JSON document to look like the output from jq.

View Source
var JqColors = &ColorSettings{
	Ident:  Blue | Bold,
	String: Green,
	Number: Reset,
	Null:   Grey,
	False:  Reset,
	True:   Reset,
}

JqColors is the color scheme used by jq.

Functions

func ColorizeData

func ColorizeData(data []byte, s *Settings) ([]byte, error)

ColorizeData colorizes the given data and returns the result. If s is nil, the default settings are used.

func Marshal

func Marshal(v any, s *Settings) ([]byte, error)

Marshal returns the colored JSON encoding of v. If s is nil, the default settings are used.

Types

type Color

type Color int

Color is a color used to highlight something in the JSON document.

const (
	Reset   Color = 0
	Grey    Color = 30 | Bold
	Red     Color = 31
	Green   Color = 32
	Yellow  Color = 33
	Blue    Color = 34
	Magenta Color = 35
	Cyan    Color = 36
	White   Color = 37
)
const (
	Bold Color = 0x100
)

func (Color) String

func (c Color) String() string

type ColorMode

type ColorMode int

ColorMode is used to determine when to colorize the output.

const (
	// Never disables colorization.
	Never ColorMode = -1

	// Auto enables colorization if the output is a terminal.
	Auto ColorMode = 0

	// Always enables colorization.
	Always ColorMode = 1
)

func (ColorMode) UseColors

func (c ColorMode) UseColors(w io.Writer) bool

UseColors returns true if the output should be colorized.

type ColorSettings

type ColorSettings struct {
	Ident  Color
	String Color
	Number Color
	Null   Color
	False  Color
	True   Color
}

ColorSettings contains the colors used to highlight the different parts of the JSON document.

type Encoder

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

Encoder is a JSON encoder that colorizes the output.

func NewEncoder

func NewEncoder(w io.Writer, settings *Settings) *Encoder

NewEncoder returns a new encoder that writes to w. If settings is nil, the default settings are used.

func (*Encoder) Colorize

func (e *Encoder) Colorize(r io.Reader) error

Colorize reads JSON from r and writes colorized JSON to the stream.

func (*Encoder) ColorizeData

func (e *Encoder) ColorizeData(data []byte) error

ColorizeData colorizes the JSON data and writes it to the stream.

func (*Encoder) Encode

func (e *Encoder) Encode(v any) error

Encode writes the JSON encoding of v to the stream.

func (*Encoder) Write

func (e *Encoder) Write(p []byte) (n int, err error)

Write writes p to the stream. The function expects p to contain valid JSON data.

type Settings

type Settings struct {
	EndWithNewline bool
	Newlines       bool
	Indent         string
	Separator      string
	ColorMode      ColorMode
	Color          *ColorSettings
}

Settings contains the settings used to colorize and format the JSON document.

Jump to

Keyboard shortcuts

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