• Stars
    star
    166
  • Rank 227,748 (Top 5 %)
  • Language
    Go
  • License
    MIT License
  • Created almost 11 years ago
  • Updated about 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Easy column formatted output for golang

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.

More Repositories

1

go-glob

Basic string globs in golang
Go
141
star
2

slide.sh

Create simple, text-based slide decks in your shell
Shell
71
star
3

go-license

Software licensing standardization library for Golang
Roff
69
star
4

pakrat

A Python library and CLI tool for mirroring and versioning YUM repositories
Python
60
star
5

readme-server

Go app to quickly render markdown README's and display them
Go
26
star
6

go-otp

Simple one-time pad crypto library for golang
Go
14
star
7

go-filecache

Cache data in flat files in Golang
Go
7
star
8

shortstr

Go
6
star
9

puppet-tell

Tell external parties about changes to resources
Ruby
6
star
10

puppet-packagelist

Dynamically create package resources from lists
Ruby
6
star
11

go-semver

Semantic versioning (semver.org) for golang
Go
5
star
12

iocap

Go package for rate limiting data streams using the familiar io.Reader and io.Writer interfaces.
Go
3
star
13

projects

Random projects in various languages
PHP
3
star
14

veneer

An experimental REST API framework for PHP
PHP
3
star
15

ruby-aptly

Ruby wrapper for managing deb repos with Aptly (http://aptly.info)
Ruby
3
star
16

terraform-tests

Various test Terraform configurations
HCL
2
star
17

nofork

Wrap a long process with a pidfile without forking
Go
2
star
18

yum-rocket

Fast, distributed, parallel downloads for YUM
Python
2
star
19

huck

Open-ended information sharing framework
Ruby
1
star
20

strategy-gem

Create runnable, stepped workflow plans in ruby
Ruby
1
star
21

oaf

Care-free web app prototyping using files and scripts
Ruby
1
star
22

go-duration-ruby

Golang duration parser/formatter for Ruby
Ruby
1
star
23

dotfiles

Only the important ones
Shell
1
star
24

sertify

Certify string data in multiple languages
Ruby
1
star
25

go-permtest

Cross-platform methods for checking if files or directories are writable
Go
1
star