mintab

package module
v0.0.57 Latest Latest
Warning

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

Go to latest
Published: Jan 7, 2025 License: MIT Imports: 9 Imported by: 4

README

mintab

CI codecov Go Reference Go Report Card

mintab is a minimum ASCII table utilities for Go

Motivation

While tablewriter is useful, I wanted a smaller package with features such as backlog format support that tablewriter does not have.

Format

Text

+----------+------+-----------+----------+------+-------+-------------+---------------+
| Instance | SG   | Direction | Protocol | From | To    | AddressType | CidrBlock     |
+----------+------+-----------+----------+------+-------+-------------+---------------+
| i-1      | sg-1 | Ingress   | tcp      |   22 |    22 | SG          | sg-10         |
+----------+------+-----------+----------+------+-------+-------------+---------------+
| i-1      | sg-1 | Egress    |       -1 |    0 |     0 | Ipv4        | 0.0.0.0/0     |
+----------+------+-----------+----------+------+-------+-------------+---------------+
| i-1      | sg-2 | Ingress   | tcp      |  443 |   443 | Ipv4        | 0.0.0.0/0     |
+----------+------+-----------+----------+------+-------+-------------+---------------+
| i-1      | sg-2 | Egress    |       -1 |    0 |     0 | Ipv4        | 0.0.0.0/0     |
+----------+------+-----------+----------+------+-------+-------------+---------------+
| i-2      | sg-3 | Ingress   | icmp     |   -1 |    -1 | SG          | sg-11         |
+----------+------+-----------+----------+------+-------+-------------+---------------+
| i-2      | sg-3 | Ingress   | tcp      | 3389 |  3389 | Ipv4        | 10.1.0.0/16   |
+----------+------+-----------+----------+------+-------+-------------+---------------+
| i-2      | sg-3 | Ingress   | tcp      |    0 | 65535 | PrefixList  | pl-id/pl-name |
+----------+------+-----------+----------+------+-------+-------------+---------------+
| i-2      | sg-3 | Egress    |       -1 |    0 |     0 | Ipv4        | 0.0.0.0/0     |
+----------+------+-----------+----------+------+-------+-------------+---------------+

Text merged

+----------+------+-----------+----------+------+-------+-------------+---------------+
| Instance | SG   | Direction | Protocol | From | To    | AddressType | CidrBlock     |
+----------+------+-----------+----------+------+-------+-------------+---------------+
| i-1      | sg-1 | Ingress   | tcp      |   22 |    22 | SG          | sg-10         |
+          +      +-----------+----------+------+-------+-------------+---------------+
|          |      | Egress    |       -1 |    0 |     0 | Ipv4        | 0.0.0.0/0     |
+          +------+-----------+----------+------+-------+-------------+---------------+
|          | sg-2 | Ingress   | tcp      |  443 |   443 | Ipv4        | 0.0.0.0/0     |
+          +      +-----------+----------+------+-------+-------------+---------------+
|          |      | Egress    |       -1 |    0 |     0 | Ipv4        | 0.0.0.0/0     |
+----------+------+-----------+----------+------+-------+-------------+---------------+
| i-2      | sg-3 | Ingress   | icmp     |   -1 |    -1 | SG          | sg-11         |
+          +      +           +----------+------+-------+-------------+---------------+
|          |      |           | tcp      | 3389 |  3389 | Ipv4        | 10.1.0.0/16   |
+          +      +           +          +------+-------+-------------+---------------+
|          |      |           |          |    0 | 65535 | PrefixList  | pl-id/pl-name |
+          +      +-----------+----------+------+-------+-------------+---------------+
|          |      | Egress    |       -1 |    0 |     0 | Ipv4        | 0.0.0.0/0     |
+----------+------+-----------+----------+------+-------+-------------+---------------+

Compressed-text merged

+----------+------+-----------+----------+------+-------+-------------+---------------+
| Instance | SG   | Direction | Protocol | From | To    | AddressType | CidrBlock     |
+----------+------+-----------+----------+------+-------+-------------+---------------+
| i-1      | sg-1 | Ingress   | tcp      |   22 |    22 | SG          | sg-10         |
|          |      | Egress    |       -1 |    0 |     0 | Ipv4        | 0.0.0.0/0     |
|          | sg-2 | Ingress   | tcp      |  443 |   443 | Ipv4        | 0.0.0.0/0     |
|          |      | Egress    |       -1 |    0 |     0 | Ipv4        | 0.0.0.0/0     |
+----------+------+-----------+----------+------+-------+-------------+---------------+
| i-2      | sg-3 | Ingress   | icmp     |   -1 |    -1 | SG          | sg-11         |
|          |      |           | tcp      | 3389 |  3389 | Ipv4        | 10.1.0.0/16   |
|          |      |           |          |    0 | 65535 | PrefixList  | pl-id/pl-name |
|          |      | Egress    |       -1 |    0 |     0 | Ipv4        | 0.0.0.0/0     |
+----------+------+-----------+----------+------+-------+-------------+---------------+

Markdown merged

| Instance | SG   | Direction | Protocol | From | To    | AddressType | CidrBlock     |
| -------- | ---- | --------- | -------- | ---- | ----- | ----------- | ------------- |
| i-1      | sg-1 | Ingress   | tcp      | 22   | 22    | SG          | sg-10         |
|          |      | Egress    | -1       | 0    | 0     | Ipv4        | 0.0.0.0/0     |
|          | sg-2 | Ingress   | tcp      | 443  | 443   | Ipv4        | 0.0.0.0/0     |
|          |      | Egress    | -1       | 0    | 0     | Ipv4        | 0.0.0.0/0     |
| i-2      | sg-3 | Ingress   | icmp     | -1   | -1    | SG          | sg-11         |
|          |      |           | tcp      | 3389 | 3389  | Ipv4        | 10.1.0.0/16   |
|          |      |           |          | 0    | 65535 | PrefixList  | pl-id/pl-name |
|          |      | Egress    | -1       | 0    | 0     | Ipv4        | 0.0.0.0/0     |

Backlog merged

| Instance | SG   | Direction | Protocol | From | To    | AddressType | CidrBlock     |h
| i-1      | sg-1 | Ingress   | tcp      |   22 |    22 | SG          | sg-10         |
|          |      | Egress    |       -1 |    0 |     0 | Ipv4        | 0.0.0.0/0     |
|          | sg-2 | Ingress   | tcp      |  443 |   443 | Ipv4        | 0.0.0.0/0     |
|          |      | Egress    |       -1 |    0 |     0 | Ipv4        | 0.0.0.0/0     |
| i-2      | sg-3 | Ingress   | icmp     |   -1 |    -1 | SG          | sg-11         |
|          |      |           | tcp      | 3389 |  3389 | Ipv4        | 10.1.0.0/16   |
|          |      |           |          |    0 | 65535 | PrefixList  | pl-id/pl-name |
|          |      | Egress    |       -1 |    0 |     0 | Ipv4        | 0.0.0.0/0     |

Support

  • Support markdown table format
  • Support backlog table format
  • Support multiple lines in a row
  • Support direct loading of struct slices
  • Support for column merging based on previous field values
  • Support for column exclusion
  • Support for HTML special character escapes (designed primarily for markdown)
  • Support for string concatenation when the field is a slice of the primitive type values
  • Support automatic string conversion of byte slices

Benchmark

mintab is memory-efficient.

$ make bench
go test -run=^$ -bench=. -benchmem -count 5 -cpuprofile=cpu.prof -memprofile=mem.prof
goos: darwin
goarch: arm64
pkg: benchmarks
cpu: Apple M2
BenchmarkMintabInput-8             38288             30660 ns/op            2488 B/op         41 allocs/op
BenchmarkMintabInput-8             39964             30058 ns/op            2488 B/op         41 allocs/op
BenchmarkMintabInput-8             38894             29954 ns/op            2488 B/op         41 allocs/op
BenchmarkMintabInput-8             39504             30917 ns/op            2488 B/op         41 allocs/op
BenchmarkMintabInput-8             39993             29877 ns/op            2488 B/op         41 allocs/op
BenchmarkMintabStruct-8            37072             31838 ns/op            2920 B/op         80 allocs/op
BenchmarkMintabStruct-8            38244             32118 ns/op            2920 B/op         80 allocs/op
BenchmarkMintabStruct-8            38517             31612 ns/op            2920 B/op         80 allocs/op
BenchmarkMintabStruct-8            37584             31750 ns/op            2920 B/op         80 allocs/op
BenchmarkMintabStruct-8            38352             31989 ns/op            2920 B/op         80 allocs/op
BenchmarkTableWriter-8             12877             95667 ns/op           11454 B/op        639 allocs/op
BenchmarkTableWriter-8             12712             95992 ns/op           11457 B/op        639 allocs/op
BenchmarkTableWriter-8             12834             95187 ns/op           11443 B/op        639 allocs/op
BenchmarkTableWriter-8             12716             95045 ns/op           11420 B/op        639 allocs/op
BenchmarkTableWriter-8             12680             94409 ns/op           11449 B/op        639 allocs/op
BenchmarkGoPrettyTable-8           65367             16973 ns/op            6540 B/op        192 allocs/op
BenchmarkGoPrettyTable-8           68565             17483 ns/op            6540 B/op        192 allocs/op
BenchmarkGoPrettyTable-8           70461             17172 ns/op            6540 B/op        192 allocs/op
BenchmarkGoPrettyTable-8           65881             17300 ns/op            6540 B/op        192 allocs/op
BenchmarkGoPrettyTable-8           70099             17335 ns/op            6540 B/op        192 allocs/op
PASS
ok      benchmarks      33.220s

Notes

  • Nested structs are not supported
  • Using reflect

Usage

Example

Todo

  • Add pre-loading support for streaming processing
  • Add paging for large inputs
  • Add minimal styling
  • Add caption
  • Add escape sequence support
  • Add word wrapping with new line
  • Improve performance and reduce memory allocations

Author

nekrassov01

License

MIT

Documentation

Index

Constants

View Source
const (
	// Default placeholder when field is empty in text table format.
	TextDefaultPlaceholder = "-"

	// Default word delimiter in text table format.
	TextDefaultWordDelimiter = textNewLine

	// Default placeholder when field is empty in markdown table format.
	MarkdownDefaultPlaceholder = "\\" + TextDefaultPlaceholder

	// Default word delimiter in markdown table format.
	MarkdownDefaultWordDelimiter = markdownNewLine

	// Default placeholder when field is empty in backlog table format.
	BacklogDefaultPlaceholder = TextDefaultPlaceholder

	// Default word delimiter in backlog table format.
	BacklogDefaultWordDelimiter = backlogNewLine
)
View Source
const Version = "0.0.57"

Variables

This section is empty.

Functions

This section is empty.

Types

type Format added in v0.0.30

type Format int

A Format represents the output format.

const (
	TextFormat           Format = iota // Text table format.
	CompressedTextFormat               // Compressed text table format.
	MarkdownFormat                     // Markdown table format.
	BacklogFormat                      // Backlog-specific table format.
)

func ParseFormat added in v0.0.54

func ParseFormat(s string) (Format, error)

ParseFormat parses a string into a Format.

func (Format) MarshalJSON added in v0.0.54

func (t Format) MarshalJSON() ([]byte, error)

MarshalJSON marshals a Format into JSON.

func (Format) String added in v0.0.30

func (t Format) String() string

String returns the string representation of a Format.

func (Format) Values added in v0.0.57

func (t Format) Values() []Format

Values returns the list of supported formats.

type Input added in v0.0.51

type Input struct {
	Header []string // table header
	Data   [][]any  // matrix with any types
}

Input is a struct for loading values into Table

type Option

type Option func(*Table)

A Option sets an option on a Table.

func WithEscape added in v0.0.34

func WithEscape(has bool) Option

WithEscape enables or disables HTML escaping.

func WithFormat added in v0.0.4

func WithFormat(format Format) Option

WithFormat sets the output format.

func WithHeader added in v0.0.4

func WithHeader(has bool) Option

WithHeader sets the table header.

func WithIgnoreFields

func WithIgnoreFields(indices []int) Option

WithIgnoreFields sets column indices to be ignored.

func WithMargin added in v0.0.34

func WithMargin(width int) Option

WithMargin sets the margin size around field values.

func WithMergeFields

func WithMergeFields(indices []int) Option

WithMergeFields sets column indices to be merged.

func WithPlaceholder added in v0.0.54

func WithPlaceholder(placeholder string) Option

WithPlaceholder sets the placeholder for empty fields.

func WithWordDelimiter added in v0.0.3

func WithWordDelimiter(delimiter string) Option

WithWordDelimiter sets the delimiter to split words in a field.

type Table

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

Table represents a table structure for rendering data.

func New

func New(w io.Writer, opts ...Option) *Table

New instantiates a new Table with the writer and options.

func (*Table) Load added in v0.0.4

func (t *Table) Load(v any) error

Load validates v and converts it to a struct Table. v must be passed in one of the following two ways:

1. Struct `Input`

  • The number of columns in all rows must be the same.
  • Header is allowd to be nil.

2. Any struct slices

  • If a struct is passed, it is converted to a slice with one element.
  • If the field is a slice with primitive data type or a slice of byte slice, it is converted to a string.
  • If the field is struct, an error is returned (nested structs are not supported)

func (*Table) Render added in v0.0.46

func (t *Table) Render()

Render renders the table to the writer.

Jump to

Keyboard shortcuts

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