go-columnize: github.com/Gira-X/go-columnize Index | Files

package columnize

import "github.com/Gira-X/go-columnize"

Package columnize formats a slice or array into a single string with embedded newlines. On printing the string, the columns are aligned.


Return a string from an array with embedded newlines formatted so that when printed the columns are aligned.

data := []int{1, 2, 3, 4}
opts := columnize.DefaultOptions()
opts.DisplayWidth = 10
fmt.Println(columnize.Format(data, opts))
# prints "1  3\n2  4\n"


ArrangeArray bool	format string as a go array
ArrangeVertical bool	format entries top down and left to right
			Otherwise left to right and top to bottom
ArrayPrefix string	Add this to the beginning of the string
ArraySuffix string	Add this to the end of the string
CellFmt			A format specify for formatting each item each array
			item to a string
ColSep string		Add this string between columns
DisplayWidth int	the maximum line display width
LinePrefix string	Add this prefix for each line
LineSuffix string	Add this suffix for each line
LJustify bool		whether to left justify text instead of right justify


data := []int{1, 2, 3, 4}
opts := columnize.DefaultOptions()
fmt.Println(columnize.Format(data, opts))

opts.DisplayWidth = 8
opts.CellFmt = "%02d"
fmt.Println(columnize.Format(data, opts))

opts.ArrangeArray = true
opts.CellFmt = ""
fmt.Println(columnize.Format(data, opts))


Rocky Bernstein	<rocky@gnu.org>

Also available in Python (columnize), and Perl
(Array::Format) and Ruby (columnize)

Copyright 2013 Rocky Bernstein.


Package Files

columnize.go doc.go

func Format Uses

func Format(list interface{}, opts Options) string

Format returns a string from an array with embedded newlines formatted so that when printed the columns are aligned.

For example:, for a line width of 4 characters (arranged vertically):

a = [] int{1,2,3,4}
columnize.Format(a) => '1  3\n2  4\n'

Arranged horizontally:

opts := columnize.DefaultOptions()
opts.ArrangeVertical = false
columnize.Format(a) =>  '1  2\n3  4\n'

Each column is only as wide as necessary. By default, columns are separated by two spaces.

func FormatStringList Uses

func FormatStringList(list []string, opts Options) string

FormatStringList works like Format(), but only accepts a string slice.

type Options Uses

type Options struct {
    ArrangeVertical bool
    LJustify        bool
    ArrayPrefix     string
    ArraySuffix     string
    CellFmt         string
    ColSep          string
    DisplayWidth    int
    LinePrefix      string
    LineSuffix      string

Options contains all the available configuration for use in Format().

Use DefaultOptions() or ArrayOptions() to retrieve an object with sane defaults.

func ArrayOptions Uses

func ArrayOptions() Options

ArrayOptions returns a format like this:

["1", "2", "3", "4", ]

func DefaultOptions Uses

func DefaultOptions() Options

DefaultOptions returns the defaultDisplayWidth with the new line as the separator.

This is basically the same format 'ls' would give one.

Package columnize imports 2 packages (graph). Updated 2020-09-28. Refresh now. Tools for package owners.