columnize

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 15, 2014 License: MIT Imports: 2 Imported by: 1,089

README

Columnize

Easy column-formatted output for golang

Build Status

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:

package main

import (
    "fmt"
    "github.com/ryanuber/columnize"
)

func main() {
    output := []string{
        "Name | Gender | Age",
        "Bob | Male | 38",
        "Sally | Female | 26",
    }
    fmt.Println(columnize.SimpleFormat(output))
}

As you can see, you just give it a list of strings and a delimiter. 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.

Columnize will also accept a plain string as input. This makes it easy if you already have a CLI that can build up some output and just pass it through Columnize, like so

output := "Name | Gender | Age\n"
output += "Bob | Male | 38\n"
output += "Sally | Female | 26\n"

fmt.Println(columnize.SimpleFormat(output))

You can fine-tune the format of the output by calling the Format method. This lets you set spacing and delimiter selection.

columnize.Format(input string, delim string, space string)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Format

func Format(input interface{}, delim string, space string) (string, error)

Format is the public-facing interface that takes either a plain string or a list of strings, plus a delimiter, and returns nicely aligned output.

func SimpleFormat

func SimpleFormat(input interface{}) (string, error)

Convenience function for using Columnize as easy as possible.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL