# README
Flexwriter
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.
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
- the OG, standard library's text/tabwriter
- for a more full-fledged table writer, see github.com/olekukonko/tablewriter but note that as of Jan 2025 it doesn't correctly handle wrapping text with escape strings.
Thanks
- Gio UI for the initial inspiration to use the flex model
- github.com/MichaelMure/go-term-text for the escape-sequence aware text wrapping
# 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.
# 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
# Type aliases
No description provided by the author