Categorygithub.com/hchargois/flexwriter
modulepackage
1.2.1
Repository: https://github.com/hchargois/flexwriter.git
Documentation: pkg.go.dev

# README

Flexwriter

Go Reference

go get github.com/hchargois/flexwriter

Flexwriter arranges rows of data into columns with configurable widths and alignments.

As the name suggests, it implements the CSS flexbox model to define column widths.

If the contents are too long, flexwriter automatically wraps the text over multiple lines. Text containing escape sequences (e.g. color codes) is correctly wrapped.

The output can be decorated with simple column separators or to look like tables.

demo screenshot showing features such as flexed columns, alignments and color support

Basic usage

import "github.com/hchargois/flexwriter"

// by default, the flexwriter will output to standard output; and all
// columns will default to being "shrinkable" columns (i.e. they will match
// their content size if it fits within the output width, but will shrink to
// match the output width if the content is too big to fit on a single line);
// and all columns will be separated by two spaces
writer := flexwriter.New()

// write some data (any non-string will pass through fmt.Sprint)
writer.WriteRow("deep", "thought", "says", ":")
writer.WriteRow("the", "answer", "is", 42)
writer.WriteRow(true, "or", false, "?")

// calling Flush() is required to actually output the rows
writer.Flush()

This will output:

deep  thought  says   :
the   answer   is     42
true  or       false  ?

Here's another example showing how to configure the columns and set a table decorator, and that shows how the Shrinkable columns shrinks to fit in the configured width of the output (70 columns wide):

writer := flexwriter.New()
writer.SetColumns(
    // first column, a Rigid, will not shrink and wrap
    flexwriter.Rigid{},
    // second column will
    flexwriter.Shrinkable{})
writer.SetDecorator(flexwriter.AsciiTableDecorator())
writer.SetWidth(70)

lorem := "Lorem ipsum dolor sit amet, consectetur adipiscing elit, "+
"sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim "+
"ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip"

writer.WriteRow("lorem ipsum says:", lorem)

writer.Flush()

This outputs:

+-------------------+------------------------------------------------+
| lorem ipsum says: | Lorem ipsum dolor sit amet, consectetur        |
|                   | adipiscing elit, sed do eiusmod tempor         |
|                   | incididunt ut labore et dolore magna aliqua.   |
|                   | Ut enim ad minim veniam, quis nostrud          |
|                   | exercitation ullamco laboris nisi ut aliquip   |
+-------------------+------------------------------------------------+

Many more examples can be found in the godoc.

Alternatives

Thanks

# Packages

Package flex implements a simplified flexbox layout algorithm.

# Functions

AsciiTableDecorator creates a table with ASCII characters + and - for an old-school look.
BoxDrawingTableDecorator creates a table with Unicode box drawing characters.
ColorizeDecorator wraps a decorator to make it colorful.
New creates a new flex writer with the default configuration: - write to standard output - a target width equal to the width of the standard output if it's a terminal, otherwise 80 - a gap of 2 spaces between columns, none on the sides - a default column setting of a left-aligned Shrinkable column.

# Constants

Auto can be set as the Basis of a [Flexbox] column to make the basis as large as the content.
No description provided by the author
No description provided by the author
No description provided by the author

# Structs

Flexbox columns allow you to specify the exact flex attributes as in CSS flexbox; however note that default values are all zero, there are no "smart" defaults as when using the "flex: ..." CSS syntax.
Flexed columns take a size proportional to their weight (vs all other flexed columns weights) within the available width, regardless of the size of their content.
GapDecorator is a simple decorator that adds a fixed gap between each column, as well as a left gap (before the left-most column) and a right gap (after the right-most column).
Omit columns will not appear in the output.
Rigid columns try to match the size of their content, as long as it is between Min and Max, regardless of the width of the output.
Shrinkable columns try to match the size of their content, but if the width of the output is too small, they can shrink up to their Min width.
TableDecorator is a decorator that creates a table with configurable borders and intersections.
No description provided by the author

# Interfaces

Column holds the configuration of a column for the flex writer.
Decorator is used to decorate the output.

# Type aliases

No description provided by the author