TableWriter

package module
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Dec 6, 2025 License: MIT Imports: 8 Imported by: 0

README ΒΆ

πŸ“ TableWriter

Go Reference License

TableWriter is a Go package that implements the standard io.Writer interface to automatically format tab-separated text (\t) into properly aligned and stylized tables, designed for console output (CLI).

It uses the writer concept to process the input data buffer, calculate the optimal column width based on the terminal size, and send the formatted table to the desired output.

πŸš€ Installation

To add TableWriter to your Go project:

go get github.com/Scrayil/TableWriter

πŸ’‘ Basic Usage

The TableWriter package uses Go's io.Writer interface to process data. You should instantiate a new Writer by specifying the destination output (e.g., os.Stdout) and configuration flags.
Data for the table is written to the Writer, where columns are separated by the tab character (\t) and rows by the newline character (\n). The table is only rendered when the Flush() method is called.

package main

import (
	"fmt"
	"os"

	"github.com/Scrayil/TableWriter"
)

func main() {
	// Creates a new Writer that writes to Stdout, without specifying any flag
	w := TableWriter.NewWriter(os.Stdout, 0)

	// Writing table rows
	fmt.Fprintf(w, "%s\t%s\n", "Name", "Age")
	fmt.Fprintf(w, "%s\t%d\n", "Alice", 30)
	fmt.Fprintf(w, "%s\t%d\n", "Bob", 25)

	// Flush renders the actual table
	if err := w.Flush(); err != nil {
		fmt.Println("Flushing error:", err)
	}

	// Output result
	//β”Œβ”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”
	//β”‚Name  β”‚Age β”‚
	//β”œβ”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€
	//β”‚Alice β”‚30  β”‚
	//β”œβ”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€
	//β”‚Bob   β”‚25  β”‚
	//β””β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”˜
}

βš™οΈ Configuration Flags

You can customise the behaviour and style of the table by passing one or more flags when creating the Writer (using the bitwise OR operator |).

Constant Value Description
0 0 Left alignment, Unicode borders and enabled truncation.
TableWriter.StripColours 1 << 0 Removes ANSI colour codes from the output.
TableWriter.AlignMiddle 1 << 1 Aligns the content of each column to the Centre.
TableWriter.AlignRight 1 << 2 Aligns the contents of each column to the Right.
TableWriter.RemoveLeastPad 1 << 3 Removes the minimum padding space (1 byte) used to separate text from neighbouring columns.
TableWriter.PreserveLongFields 1 << 4 Disables truncation of long strings. This completely disables padding if the column width exceeds the terminal width, allowing long lines to wrap.
TableWriter.AsciiTable 1 << 5 Uses only ASCII separator characters (+, -, |)

Note on Alignment: The AlignMiddle and AlignRight flags are mutually exclusive. If both are specified, AlignRight logically prevails due to the implementation.

Example with Flags
// Create a Writer with centre alignment and remove ANSI colour codes
flags := TableWriter.AlignMiddle | TableWriter.StripColours

w := TableWriter.NewWriter(os.Stdout, flags)
// ... write data ...
w.Flush()

πŸ› οΈ Main Methods

NewWriter(output io.Writer, flags uint) *Writer Instantiates and initialises a new Writer with the specified output and configuration flags.

Write(buf []byte) (n int, err error) Implements the io.Writer interface. Appends tabulated data to the internal buffer of the Writer.

Flush() (err error) Processes the internal buffer, calculates the table formatting (column width, truncation, alignment) and writes the formatted table to the destination io.Writer. Must be called to display the table. Clear() Resets the internal state of the Writer (buffer, columns, and rows), removing any traces of previously processed content. It is automatically called by Flush().

🎨 ANSI Colour Support

The package can handle ANSI colour codes within cells. When colour codes are present, the package calculates the column width based on visual length (ignoring escape codes). If a string is truncated and contains colour codes, the package attempts to preserve the colours and insert orange [...] notation.

Example output with ANSI colours and truncated fields

image

Documentation ΒΆ

Index ΒΆ

Constants ΒΆ

View Source
const (
	// StripColours Removes ANSI color codes from output text
	StripColours uint = 1 << iota
	// AlignMiddle centers the text horizontally in the column
	AlignMiddle
	// AlignRight shifts each cell content to the right
	AlignRight
	// RemoveLeastPad removes the minimum padding bytes used to separate text from nearby columns.
	RemoveLeastPad
	// PreserveLongFields disables the logic used to truncate long strings in the table.
	// This flag disables padding completely
	PreserveLongFields
	// AsciiTable allows using only ASCII divider.
	// Useful for environments that do not support utf-8 encodings
	AsciiTable
)

Variables ΒΆ

This section is empty.

Functions ΒΆ

This section is empty.

Types ΒΆ

type Writer ΒΆ

type Writer struct {
	// contains filtered or unexported fields
}

Writer the io.Writer struct used to process and format received text in order to create nice looking tables and style them according to the specified flags

func NewWriter ΒΆ

func NewWriter(output io.Writer, flags uint) *Writer

NewWriter allocates and initializes a new Writer. The parameters are the same as for the init function.

func (*Writer) Clear ΒΆ

func (w *Writer) Clear()

Clear resets the state of the Writer to remove any traces of previously flushed content

func (*Writer) Flush ΒΆ

func (w *Writer) Flush() (err error)

Flush processes the output buffer by creating the corresponding table content and sends it to the chosen output's file descriptor

func (*Writer) Write ΒΆ

func (w *Writer) Write(buf []byte) (n int, err error)

Write appends the external content received to the Writer's internal buffer This is automatically called by functions piping data into this io.Writer

Jump to

Keyboard shortcuts

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