jpp

package module
v0.0.6 Latest Latest
Warning

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

Go to latest
Published: Oct 1, 2019 License: MIT Imports: 8 Imported by: 0

README

jpp

Build Status Codacy Badge

JSON Prettier Printer that occupies a minimal number of lines while pretty-printing given JSON, using prettier which is Go implementation of "Wadler's "A Prettier Printer".

jpp is quite useful when we want to pretty print the JSON whose each node has a lot of children scalar values.

screenshot from 2018-10-28 16-57-16 This example.json cites from https://json.org/example.html

Instalation

Homebrew
$ brew install tanishiking/jpp/jpp
Download binary from GitHub Releases

https://github.com/tanishiking/jpp/releases

Build from source
$ go get -u github.com/tanishiking/jpp/cmd/jpp
Develop
$ make build # build binary into ./bin/jpp
$ make test # run all unit tests

jpp command

Options
  • -w: width (default: your terminal width)
    • Note that this command does not guarantee there are no lines longer than width
    • It just attempts to keep lines within this length when possible.
  • -i: indent string (default: ' ')
Environment Variables

We can specify the color of output string using following environment variables

  • JPP_NULL
  • JPP_BOOL
  • JPP_NUMBER
  • JPP_STRING
  • JPP_FIELDNAME

and builtin colors:

  • black
  • red
  • green
  • brown
  • blue
  • magenta
  • cyan
  • gray
  • bold_black
  • bold_red
  • bold_green
  • bold_brown
  • bold_blue
  • bold_magenta
  • bold_cyan
  • bold_gray

For example, cat some.json | JPP_NUMBER=bold_red jpp. In addition, we can specify complicated styles using SGR code. See: https://en.wikipedia.org/wiki/ANSI_escape_code

$ go get -u github.com/tanishiking/jpp/cmd/jpp
$ cat numbers.json
[
  [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ],
  [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 ],
  [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 ]
]
$ cat numbers.json | jpp -w 20
[
  [
    1, 2, 3, 4, 5,
    6, 7, 8, 9, 10
  ],
  [
    1, 2, 3, 4, 5,
    6, 7, 8, 9, 10,
    11, 12, 13, 14,
    15
  ],
  [
    1, 2, 3, 4, 5,
    6, 7, 8, 9, 10,
    11, 12, 13, 14,
    15, 16, 17, 18,
    19, 20
  ]
]

Package Usage

import (
	"fmt"

	"github.com/tanishiking/jpp"
)

func main() {
	jsonStr := `
[
  [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ],
  [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 ],
  [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 ]
]
`
	res, _ := jpp.Pretty(jsonStr, "  ", 20, nil)
	fmt.Println(res)
	// [
	//   [
	//     1, 2, 3, 4, 5,
	//     6, 7, 8, 9, 10
	//   ],
	//   [
	//     1, 2, 3, 4, 5,
	//     6, 7, 8, 9, 10,
	//     11, 12, 13, 14,
	//     15
	//   ],
	//   [
	//     1, 2, 3, 4, 5,
	//     6, 7, 8, 9, 10,
	//     11, 12, 13, 14,
	//     15, 16, 17, 18,
	//     19, 20
	//   ]
	// ]
}

Documentation

Overview

Example
package main

import (
	"fmt"

	"github.com/tanishiking/jpp"
)

func main() {
	jsonStr := `
[
  [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ],
  [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 ],
  [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 ]
]
`
	res, _ := jpp.Pretty(jsonStr, "  ", 20, nil)
	fmt.Println(res)
	// [
	//   [
	//     1, 2, 3, 4, 5,
	//     6, 7, 8, 9, 10
	//   ],
	//   [
	//     1, 2, 3, 4, 5,
	//     6, 7, 8, 9, 10,
	//     11, 12, 13, 14,
	//     15
	//   ],
	//   [
	//     1, 2, 3, 4, 5,
	//     6, 7, 8, 9, 10,
	//     11, 12, 13, 14,
	//     15, 16, 17, 18,
	//     19, 20
	//   ]
	// ]
}
Output:

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// DefaultScheme is plain color scheme that doesn't specify
	// any colors or SGRs.
	// Note that the default color scheme for CLI tool is not this.
	DefaultScheme = &ColorScheme{
		Null:      NoColor,
		Bool:      NoColor,
		Number:    NoColor,
		String:    NoColor,
		FieldName: NoColor,
	}
)

Functions

func Black

func Black(format string, args ...interface{}) string

Black formats according to a format specifier and returns the resulting string colored by black.

func Blue

func Blue(format string, args ...interface{}) string

Blue formats according to a format specifier and returns the resulting string colored by blue.

func BoldBlack

func BoldBlack(format string, args ...interface{}) string

BoldBlack formats according to a format specifier and returns the resulting bold string colored by black.

func BoldBlue

func BoldBlue(format string, args ...interface{}) string

BoldBlue formats according to a format specifier and returns the resulting bold string colored by blue.

func BoldBrown

func BoldBrown(format string, args ...interface{}) string

BoldBrown formats according to a format specifier and returns the resulting bold string colored by brown.

func BoldCyan

func BoldCyan(format string, args ...interface{}) string

BoldCyan formats according to a format specifier and returns the resulting bold string colored by cyan.

func BoldGray

func BoldGray(format string, args ...interface{}) string

BoldGray formats according to a format specifier and returns the resulting bold string colored by gray.

func BoldGreen

func BoldGreen(format string, args ...interface{}) string

BoldGreen formats according to a format specifier and returns the resulting bold string colored by green.

func BoldMagenta

func BoldMagenta(format string, args ...interface{}) string

BoldMagenta formats according to a format specifier and returns the resulting bold string colored by magenta.

func BoldRed

func BoldRed(format string, args ...interface{}) string

BoldRed formats according to a format specifier and returns the resulting bold string colored by red.

func Brown

func Brown(format string, args ...interface{}) string

Brown formats according to a format specifier and returns the resulting string colored by brown.

func Cyan

func Cyan(format string, args ...interface{}) string

Cyan formats according to a format specifier and returns the resulting string colored by cyan.

func Gray

func Gray(format string, args ...interface{}) string

Gray formats according to a format specifier and returns the resulting string colored by gray.

func Green

func Green(format string, args ...interface{}) string

Green formats according to a format specifier and returns the resulting string colored by green.

func Magenta

func Magenta(format string, args ...interface{}) string

Magenta formats according to a format specifier and returns the resulting string colored by magenta.

func NoColor

func NoColor(format string, args ...interface{}) string

NoColor formats according to a format specifier and returns the resulting string.

func Pretty

func Pretty(jsonStr string, i string, w int, colorScheme *ColorScheme) (string, error)

Pretty prettifies specified json string.

func Red

func Red(format string, args ...interface{}) string

Red formats according to a format specifier and returns the resulting string colored by red.

Types

type ColorScheme

type ColorScheme struct {
	Null      ColoredFormat
	Bool      ColoredFormat
	Number    ColoredFormat
	String    ColoredFormat
	FieldName ColoredFormat
}

ColorScheme is used for specifying the output color or SGR using https://github.com/fatih/color

type ColoredFormat

type ColoredFormat = func(string, ...interface{}) string

ColoredFormat formats according to a format specifier and returns the resulting string.

Directories

Path Synopsis
cmd
jpp

Jump to

Keyboard shortcuts

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