prettyslice

package module
v0.0.0-...-d802ba5 Latest Latest
Warning

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

Go to latest
Published: Mar 5, 2019 License: MIT Imports: 8 Imported by: 0

README

Go Report Card Go Doc

Pretty Slice Printer

It pretty prints any type of slices to any io.Writer with adjustable coloring features.

Example

package main

import s "github.com/inancgumus/prettyslice"

func main() {
	nums := []int{1, 3, 5, 2, 4, 8}
	odds := nums[:3]
	evens := nums[3:]

	nums[1], nums[3] = 9, 6
	s.Show("nums", nums)
	s.Show("odds : nums[:3]", odds)
	s.Show("evens: nums[3:]", evens)
}
Output:

Example #2 — Render Colorless

package main

import s "github.com/inancgumus/prettyslice"

func main() {
	// Render colorless output to a file
	f, _ := os.Create("out.txt")
	defer f.Close()

	nums := []int{1, 3, 5, 2, 4, 8}

	s.Writer = f
	s.Colors(false)
	s.Show("nums", nums)
}

Printing Options

  • Writer: Control where to draw the output. Default: colors.Output (It's like os.Stdout but with colors).
  • PrintBacking: Whether to print the backing array. Default: false.
  • PrettyByteRune: Prints the bytes and runes as characters instead of numbers. Default: true.
  • MaxPerLine: Maximum number of slice items on a line. Default: 5.
  • MaxElements: Limits the number of elements printed. 0 means printing all elements. Default: 0.
  • Width: Number of space characters (padding) between the header message and the slice details like len, cap and ptr. Default: 45.
  • NormalizePointers: Prints the addresses of the slice elements as if they're contiguous. It basically normalizes by the element type size. See the source code for more information. Default: false.
  • PrintHex: Prints the pointers as hexadecimals. Default: false.
  • PrintBytesHex: Prints byte elements as hex digits. Overrides the PrettyByteRune option for byte values. Default: false.
  • PrintElementAddr: Prints the element addresses. Default: false.

Coloring Options

  • ColorHeader: Sets the color for the header. Default: color.New(color.BgHiBlack, color.FgMagenta, color.Bold).
  • ColorSlice: Sets the color for the slice elements. Default: color.New(color.FgCyan).
  • ColorBacker: Sets the color for the backing array elements. Default: color.New(color.FgHiBlack).
  • ColorIndex: Sets the color for the index numbers. Default: ColorBacker.
  • ColorAddr: Sets the color for the element addresses. Default: ColorBacker.

Have fun!

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ColorHeader sets the color for the header
	ColorHeader = color.New(
		color.BgHiBlack,
		color.FgMagenta,
		color.Bold)

	// ColorSlice sets the color for the slice's items
	ColorSlice = color.New(color.FgCyan)

	// ColorBacker sets the color for the backing array's items
	ColorBacker = color.New(color.FgHiBlack)

	// ColorIndex sets the color for the index numbers of the elements
	ColorIndex = ColorBacker

	// ColorAddr sets the color for the element addresses
	ColorAddr = ColorBacker

	// MaxPerLine is maximum number of slice items on a line
	MaxPerLine = 5

	// MaxElements limits the number of elements printed
	// 0 means print all the elements.
	MaxElements = 0

	// Width is the width of the header
	// It will separate the header message and the slice details with empty spaces
	Width = 45

	// PrettyByteRune prints byte and rune elements as chars
	PrettyByteRune = true

	// PrintBacking prints the backing array if it's true
	PrintBacking = false

	// PrintElementAddr prints the addresses of each element
	PrintElementAddr = false

	// PrintHex prints the pointers in hexadecimals
	//
	// When it's false, only the last 4 digits of the pointers will be printed as decimals.
	//
	// When it's true, all the digits of the pointers will be printed as hexadecimals.
	PrintHex = false

	// PrintBytesHex prints byte elements as hex digits
	PrintBytesHex = false

	// SpaceCharacter gets printed when a space character is found.
	// (only if PrettyByteRune is true)
	SpaceCharacter = ' '

	// NormalizePointers prints pointers as if they're contiguous.
	//
	// Let's say you've []int64{1, 2}
	//
	//              Memory addresses
	// 1st element: 8000
	// 2nd element: 8008
	//
	// This option prints them like this instead:
	//
	//              Memory addresses
	// 1st element: 8000
	// 2nd element: 8001
	//
	// So, it basically normalizes by the element type size.
	NormalizePointers = false

	// Writer controls where to draw the slices
	Writer = color.Output
)

Functions

func Colors

func Colors(enabled bool)

Colors is used to enable/disable the color data from the output

func Show

func Show(msg string, slices ...interface{})

Show pretty prints slices

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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