deps: vendor columnize
This commit is contained in:
parent
5737c0a13b
commit
b10f22484e
|
@ -1233,6 +1233,11 @@
|
||||||
"Comment": "v1.0.0-884-gc54bbac",
|
"Comment": "v1.0.0-884-gc54bbac",
|
||||||
"Rev": "c54bbac81d19eb4df3ad167764dbb6ff2e7194de"
|
"Rev": "c54bbac81d19eb4df3ad167764dbb6ff2e7194de"
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"ImportPath": "github.com/ryanuber/columnize",
|
||||||
|
"Comment": "v2.0.1-8-g983d3a5",
|
||||||
|
"Rev": "983d3a5fab1bf04d1b412465d2d9f8430e2e917e"
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"ImportPath": "github.com/satori/go.uuid",
|
"ImportPath": "github.com/satori/go.uuid",
|
||||||
"Rev": "d41af8bb6a7704f00bc3b7cba9355ae6a5a80048"
|
"Rev": "d41af8bb6a7704f00bc3b7cba9355ae6a5a80048"
|
||||||
|
|
|
@ -0,0 +1,3 @@
|
||||||
|
language: go
|
||||||
|
go:
|
||||||
|
- tip
|
|
@ -0,0 +1,20 @@
|
||||||
|
MIT LICENSE
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining
|
||||||
|
a copy of this software and associated documentation files (the
|
||||||
|
"Software"), to deal in the Software without restriction, including
|
||||||
|
without limitation the rights to use, copy, modify, merge, publish,
|
||||||
|
distribute, sublicense, and/or sell copies of the Software, and to
|
||||||
|
permit persons to whom the Software is furnished to do so, subject to
|
||||||
|
the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be
|
||||||
|
included in all copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||||
|
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||||
|
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||||
|
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||||
|
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||||
|
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||||
|
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
@ -0,0 +1,75 @@
|
||||||
|
Columnize
|
||||||
|
=========
|
||||||
|
|
||||||
|
Easy column-formatted output for golang
|
||||||
|
|
||||||
|
[![Build Status](https://travis-ci.org/ryanuber/columnize.svg)](https://travis-ci.org/ryanuber/columnize)
|
||||||
|
|
||||||
|
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 = ""
|
||||||
|
config.Empty = ""
|
||||||
|
```
|
||||||
|
|
||||||
|
* `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
|
||||||
|
* `Empty` is a string used to replace blank values found in output
|
||||||
|
|
||||||
|
You can then pass the `Config` in using the `Format` method (signature below) to
|
||||||
|
have text formatted to your liking.
|
||||||
|
|
||||||
|
Usage
|
||||||
|
=====
|
||||||
|
|
||||||
|
```go
|
||||||
|
SimpleFormat(intput []string) string
|
||||||
|
|
||||||
|
Format(input []string, config *Config) string
|
||||||
|
```
|
|
@ -0,0 +1,134 @@
|
||||||
|
package columnize
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Config struct {
|
||||||
|
// The string by which the lines of input will be split.
|
||||||
|
Delim string
|
||||||
|
|
||||||
|
// The string by which columns of output will be separated.
|
||||||
|
Glue string
|
||||||
|
|
||||||
|
// The string by which columns of output will be prefixed.
|
||||||
|
Prefix string
|
||||||
|
|
||||||
|
// A replacement string to replace empty fields
|
||||||
|
Empty string
|
||||||
|
}
|
||||||
|
|
||||||
|
// Returns a Config with default values.
|
||||||
|
func DefaultConfig() *Config {
|
||||||
|
return &Config{
|
||||||
|
Delim: "|",
|
||||||
|
Glue: " ",
|
||||||
|
Prefix: "",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Returns a list of elements, each representing a single item which will
|
||||||
|
// belong to a column of output.
|
||||||
|
func getElementsFromLine(config *Config, line string) []interface{} {
|
||||||
|
elements := make([]interface{}, 0)
|
||||||
|
for _, field := range strings.Split(line, config.Delim) {
|
||||||
|
value := strings.TrimSpace(field)
|
||||||
|
if value == "" && config.Empty != "" {
|
||||||
|
value = config.Empty
|
||||||
|
}
|
||||||
|
elements = append(elements, value)
|
||||||
|
}
|
||||||
|
return elements
|
||||||
|
}
|
||||||
|
|
||||||
|
// Examines a list of strings and determines how wide each column should be
|
||||||
|
// considering all of the elements that need to be printed within it.
|
||||||
|
func getWidthsFromLines(config *Config, lines []string) []int {
|
||||||
|
var widths []int
|
||||||
|
|
||||||
|
for _, line := range lines {
|
||||||
|
elems := getElementsFromLine(config, line)
|
||||||
|
for i := 0; i < len(elems); i++ {
|
||||||
|
l := len(elems[i].(string))
|
||||||
|
if len(widths) <= i {
|
||||||
|
widths = append(widths, l)
|
||||||
|
} else if widths[i] < l {
|
||||||
|
widths[i] = l
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return widths
|
||||||
|
}
|
||||||
|
|
||||||
|
// Given a set of column widths and the number of columns in the current line,
|
||||||
|
// returns a sprintf-style format string which can be used to print output
|
||||||
|
// aligned properly with other lines using the same widths set.
|
||||||
|
func (c *Config) getStringFormat(widths []int, columns int) string {
|
||||||
|
// Start with the prefix, if any was given.
|
||||||
|
stringfmt := c.Prefix
|
||||||
|
|
||||||
|
// Create the format string from the discovered widths
|
||||||
|
for i := 0; i < columns && i < len(widths); i++ {
|
||||||
|
if i == columns-1 {
|
||||||
|
stringfmt += "%s\n"
|
||||||
|
} else {
|
||||||
|
stringfmt += fmt.Sprintf("%%-%ds%s", widths[i], c.Glue)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return stringfmt
|
||||||
|
}
|
||||||
|
|
||||||
|
// MergeConfig merges two config objects together and returns the resulting
|
||||||
|
// configuration. Values from the right take precedence over the left side.
|
||||||
|
func MergeConfig(a, b *Config) *Config {
|
||||||
|
var result Config = *a
|
||||||
|
|
||||||
|
// Return quickly if either side was nil
|
||||||
|
if a == nil || b == nil {
|
||||||
|
return &result
|
||||||
|
}
|
||||||
|
|
||||||
|
if b.Delim != "" {
|
||||||
|
result.Delim = b.Delim
|
||||||
|
}
|
||||||
|
if b.Glue != "" {
|
||||||
|
result.Glue = b.Glue
|
||||||
|
}
|
||||||
|
if b.Prefix != "" {
|
||||||
|
result.Prefix = b.Prefix
|
||||||
|
}
|
||||||
|
if b.Empty != "" {
|
||||||
|
result.Empty = b.Empty
|
||||||
|
}
|
||||||
|
|
||||||
|
return &result
|
||||||
|
}
|
||||||
|
|
||||||
|
// Format is the public-facing interface that takes either a plain string
|
||||||
|
// or a list of strings and returns nicely aligned output.
|
||||||
|
func Format(lines []string, config *Config) string {
|
||||||
|
var result string
|
||||||
|
|
||||||
|
conf := MergeConfig(DefaultConfig(), config)
|
||||||
|
widths := getWidthsFromLines(conf, lines)
|
||||||
|
|
||||||
|
// Create the formatted output using the format string
|
||||||
|
for _, line := range lines {
|
||||||
|
elems := getElementsFromLine(conf, line)
|
||||||
|
stringfmt := conf.getStringFormat(widths, len(elems))
|
||||||
|
result += fmt.Sprintf(stringfmt, elems...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Remove trailing newline without removing leading/trailing space
|
||||||
|
if n := len(result); n > 0 && result[n-1] == '\n' {
|
||||||
|
result = result[:n-1]
|
||||||
|
}
|
||||||
|
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
// Convenience function for using Columnize as easy as possible.
|
||||||
|
func SimpleFormat(lines []string) string {
|
||||||
|
return Format(lines, nil)
|
||||||
|
}
|
Loading…
Reference in New Issue