columnize

package module
v2.1.2+incompatible Latest Latest
Warning

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

Go to latest
Published: Aug 19, 2020 License: MIT Imports: 3 Imported by: 978

README

Columnize

Easy column-formatted output for golang

Build Status GoDoc

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",
    }
    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 = ""
config.NoTrim = false
  • 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
  • NoTrim is a boolean used to disable the automatic trimming of input values

You can then pass the Config in using the Format method (signature below) to have text formatted to your liking.

See the godoc page for usage.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Format

func Format(lines []string, config *Config) string

Format is the public-facing interface that takes a list of strings and returns nicely aligned column-formatted text.

func SimpleFormat

func SimpleFormat(lines []string) string

SimpleFormat is a convenience function to format text with the defaults.

Types

type Config added in v1.1.0

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

	// NoTrim disables automatic trimming of inputs.
	NoTrim bool
}

Config can be used to tune certain parameters which affect the way in which Columnize will format output text.

func DefaultConfig added in v1.1.0

func DefaultConfig() *Config

DefaultConfig returns a *Config with default values.

func MergeConfig

func MergeConfig(a, b *Config) *Config

MergeConfig merges two config objects together and returns the resulting configuration. Values from the right take precedence over the left side.

Jump to

Keyboard shortcuts

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