2015-04-27 23:19:51 +00:00
|
|
|
Columnize
|
|
|
|
=========
|
|
|
|
|
|
|
|
Easy column-formatted output for golang
|
|
|
|
|
|
|
|
[![Build Status](https://travis-ci.org/ryanuber/columnize.svg)](https://travis-ci.org/ryanuber/columnize)
|
2016-07-23 00:11:47 +00:00
|
|
|
[![GoDoc](https://godoc.org/github.com/ryanuber/columnize?status.svg)](https://godoc.org/github.com/ryanuber/columnize)
|
2015-04-27 23:19:51 +00:00
|
|
|
|
|
|
|
Columnize is a really small Go package that makes building CLI's a little bit
|
|
|
|
easier. In some CLI designs, you want to output a number similar items in a
|
|
|
|
human-readable way with nicely aligned columns. However, figuring out how wide
|
|
|
|
to make each column is a boring problem to solve and eats your valuable time.
|
|
|
|
|
|
|
|
Here is an example:
|
|
|
|
|
|
|
|
```go
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"github.com/ryanuber/columnize"
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
output := []string{
|
|
|
|
"Name | Gender | Age",
|
|
|
|
"Bob | Male | 38",
|
|
|
|
"Sally | Female | 26",
|
|
|
|
}
|
|
|
|
result := columnize.SimpleFormat(output)
|
|
|
|
fmt.Println(result)
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
As you can see, you just pass in a list of strings. And the result:
|
|
|
|
|
|
|
|
```
|
|
|
|
Name Gender Age
|
|
|
|
Bob Male 38
|
|
|
|
Sally Female 26
|
|
|
|
```
|
|
|
|
|
|
|
|
Columnize is tolerant of missing or empty fields, or even empty lines, so
|
|
|
|
passing in extra lines for spacing should show up as you would expect.
|
|
|
|
|
|
|
|
Configuration
|
|
|
|
=============
|
|
|
|
|
|
|
|
Columnize is configured using a `Config`, which can be obtained by calling the
|
|
|
|
`DefaultConfig()` method. You can then tweak the settings in the resulting
|
|
|
|
`Config`:
|
|
|
|
|
|
|
|
```
|
|
|
|
config := columnize.DefaultConfig()
|
|
|
|
config.Delim = "|"
|
|
|
|
config.Glue = " "
|
|
|
|
config.Prefix = ""
|
2015-09-22 15:44:21 +00:00
|
|
|
config.Empty = ""
|
2015-04-27 23:19:51 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
* `Delim` is the string by which columns of **input** are delimited
|
|
|
|
* `Glue` is the string by which columns of **output** are delimited
|
|
|
|
* `Prefix` is a string by which each line of **output** is prefixed
|
2015-09-22 15:44:21 +00:00
|
|
|
* `Empty` is a string used to replace blank values found in output
|
2015-04-27 23:19:51 +00:00
|
|
|
|
|
|
|
You can then pass the `Config` in using the `Format` method (signature below) to
|
|
|
|
have text formatted to your liking.
|
|
|
|
|
2016-07-23 00:11:47 +00:00
|
|
|
See the [godoc](https://godoc.org/github.com/ryanuber/columnize) page for usage.
|