https://github.com/inancgumus/prettyslice
Pretty prints slices to any io.Writer: I created this package to teach slices in my Go course.
https://github.com/inancgumus/prettyslice
go golang slice slices teaching-tool
Last synced: 11 months ago
JSON representation
Pretty prints slices to any io.Writer: I created this package to teach slices in my Go course.
- Host: GitHub
- URL: https://github.com/inancgumus/prettyslice
- Owner: inancgumus
- License: mit
- Created: 2018-09-24T14:55:06.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2019-03-05T22:09:11.000Z (almost 7 years ago)
- Last Synced: 2024-06-19T19:30:30.813Z (over 1 year ago)
- Topics: go, golang, slice, slices, teaching-tool
- Language: Go
- Homepage:
- Size: 187 KB
- Stars: 64
- Watchers: 5
- Forks: 9
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://goreportcard.com/report/github.com/inancgumus/prettyslice) [](https://godoc.org/github.com/inancgumus/prettyslice)
# Pretty Slice Printer
It pretty prints **any type of** slices to any [io.Writer](https://golang.org/pkg/io/#Writer) with adjustable **coloring** features.
## Example
```go
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
```go
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!