pax_global_header00006660000000000000000000000064123245237150014516gustar00rootroot0000000000000052 comment=785d943a7b6886e0bb2f139a60487b823dd8d9de columnize-2.0.1/000077500000000000000000000000001232452371500135235ustar00rootroot00000000000000columnize-2.0.1/.travis.yml000066400000000000000000000000311232452371500156260ustar00rootroot00000000000000language: go go: - tip columnize-2.0.1/COPYING000066400000000000000000000020141232452371500145530ustar00rootroot00000000000000MIT 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. columnize-2.0.1/README.md000066400000000000000000000030421232452371500150010ustar00rootroot00000000000000Columnize ========= 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 = " " ``` 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 ``` columnize-2.0.1/columnize.go000066400000000000000000000050361232452371500160630ustar00rootroot00000000000000package 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 } // Returns a Config with default values. func DefaultConfig() *Config { return &Config{ Delim: "|", Glue: " ", } } // Returns a list of elements, each representing a single item which will // belong to a column of output. func getElementsFromLine(line string, delim string) []interface{} { elements := make([]interface{}, 0) for _, field := range strings.Split(line, delim) { elements = append(elements, strings.TrimSpace(field)) } 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(lines []string, delim string) []int { var widths []int for _, line := range lines { elems := getElementsFromLine(line, delim) 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 getStringFormat(widths []int, columns int, space string) string { var stringfmt string // 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], space) } } return stringfmt } // 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 widths := getWidthsFromLines(lines, config.Delim) // Create the formatted output using the format string for _, line := range lines { elems := getElementsFromLine(line, config.Delim) stringfmt := getStringFormat(widths, len(elems), config.Glue) 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 { config := DefaultConfig() return Format(lines, config) } columnize-2.0.1/columnize_test.go000066400000000000000000000067511232452371500171270ustar00rootroot00000000000000package columnize import "testing" func TestListOfStringsInput(t *testing.T) { input := []string{ "Column A | Column B | Column C", "x | y | z", } config := DefaultConfig() output := Format(input, config) expected := "Column A Column B Column C\n" expected += "x y z" if output != expected { t.Fatalf("\nexpected:\n%s\n\ngot:\n%s", expected, output) } } func TestEmptyLinesOutput(t *testing.T) { input := []string{ "Column A | Column B | Column C", "", "x | y | z", } config := DefaultConfig() output := Format(input, config) expected := "Column A Column B Column C\n" expected += "\n" expected += "x y z" if output != expected { t.Fatalf("\nexpected:\n%s\n\ngot:\n%s", expected, output) } } func TestLeadingSpacePreserved(t *testing.T) { input := []string{ "| Column B | Column C", "x | y | z", } config := DefaultConfig() output := Format(input, config) expected := " Column B Column C\n" expected += "x y z" if output != expected { t.Fatalf("\nexpected:\n%s\n\ngot:\n%s", expected, output) } } func TestColumnWidthCalculator(t *testing.T) { input := []string{ "Column A | Column B | Column C", "Longer than A | Longer than B | Longer than C", "short | short | short", } config := DefaultConfig() output := Format(input, config) expected := "Column A Column B Column C\n" expected += "Longer than A Longer than B Longer than C\n" expected += "short short short" if output != expected { t.Fatalf("\nexpected:\n%s\n\ngot:\n%s", expected, output) } } func TestVariedInputSpacing(t *testing.T) { input := []string{ "Column A |Column B| Column C", "x|y| z", } config := DefaultConfig() output := Format(input, config) expected := "Column A Column B Column C\n" expected += "x y z" if output != expected { t.Fatalf("\nexpected:\n%s\n\ngot:\n%s", expected, output) } } func TestUnmatchedColumnCounts(t *testing.T) { input := []string{ "Column A | Column B | Column C", "Value A | Value B", "Value A | Value B | Value C | Value D", } config := DefaultConfig() output := Format(input, config) expected := "Column A Column B Column C\n" expected += "Value A Value B\n" expected += "Value A Value B Value C Value D" if output != expected { t.Fatalf("\nexpected:\n%s\n\ngot:\n%s", expected, output) } } func TestAlternateDelimiter(t *testing.T) { input := []string{ "Column | A % Column | B % Column | C", "Value A % Value B % Value C", } config := DefaultConfig() config.Delim = "%" output := Format(input, config) expected := "Column | A Column | B Column | C\n" expected += "Value A Value B Value C" if output != expected { t.Fatalf("\nexpected:\n%s\n\ngot:\n%s", expected, output) } } func TestAlternateSpacingString(t *testing.T) { input := []string{ "Column A | Column B | Column C", "x | y | z", } config := DefaultConfig() config.Glue = " " output := Format(input, config) expected := "Column A Column B Column C\n" expected += "x y z" if output != expected { t.Fatalf("\nexpected:\n%s\n\ngot:\n%s", expected, output) } } func TestSimpleFormat(t *testing.T) { input := []string{ "Column A | Column B | Column C", "x | y | z", } output := SimpleFormat(input) expected := "Column A Column B Column C\n" expected += "x y z" if output != expected { t.Fatalf("\nexpected:\n%s\n\ngot:\n%s", expected, output) } }