open-vault/vendor/github.com/ryanuber/columnize/columnize.go

135 lines
3.3 KiB
Go
Raw Normal View History

2015-04-27 23:19:51 +00:00
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
2015-09-22 15:44:21 +00:00
// A replacement string to replace empty fields
2015-09-22 15:44:21 +00:00
Empty string
2015-04-27 23:19:51 +00:00
}
// Returns a Config with default values.
2015-04-27 23:19:51 +00:00
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)
2016-07-23 00:11:47 +00:00
if value == "" && config.Empty != "" {
value = config.Empty
}
elements = append(elements, value)
2015-04-27 23:19:51 +00:00
}
2016-07-23 00:11:47 +00:00
return elements
}
2015-04-27 23:19:51 +00:00
// 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
2015-04-27 23:19:51 +00:00
2016-07-23 00:11:47 +00:00
for _, line := range lines {
elems := getElementsFromLine(config, line)
2016-07-23 00:11:47 +00:00
for i := 0; i < len(elems); i++ {
l := len(elems[i].(string))
2016-07-23 00:11:47 +00:00
if len(widths) <= i {
widths = append(widths, l)
} else if widths[i] < l {
widths[i] = l
}
}
}
return widths
2015-04-27 23:19:51 +00:00
}
// 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
2015-04-27 23:19:51 +00:00
// 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)
}
2016-06-30 18:19:03 +00:00
}
return stringfmt
}
2016-06-30 18:19:03 +00:00
// 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
2016-06-30 18:19:03 +00:00
// 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)
2016-06-30 18:19:03 +00:00
2015-04-27 23:19:51 +00:00
// 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...)
2015-04-27 23:19:51 +00:00
}
// 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.
2015-04-27 23:19:51 +00:00
func SimpleFormat(lines []string) string {
return Format(lines, nil)
}