README
¶
mintab
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
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
License
Documentation
¶
Index ¶
- Constants
- type Format
- type Input
- type Option
- func WithEscape(has bool) Option
- func WithFormat(format Format) Option
- func WithHeader(has bool) Option
- func WithIgnoreFields(indices []int) Option
- func WithMargin(width int) Option
- func WithMergeFields(indices []int) Option
- func WithPlaceholder(placeholder string) Option
- func WithWordDelimiter(delimiter string) Option
- type Table
Constants ¶
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 )
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.
func ParseFormat ¶ added in v0.0.54
ParseFormat parses a string into a Format.
func (Format) MarshalJSON ¶ added in v0.0.54
MarshalJSON marshals a Format into JSON.
type Option ¶
type Option func(*Table)
A Option sets an option on a Table.
func WithEscape ¶ added in v0.0.34
WithEscape enables or disables HTML escaping.
func WithFormat ¶ added in v0.0.4
WithFormat sets the output format.
func WithHeader ¶ added in v0.0.4
WithHeader sets the table header.
func WithIgnoreFields ¶
WithIgnoreFields sets column indices to be ignored.
func WithMargin ¶ added in v0.0.34
WithMargin sets the margin size around field values.
func WithMergeFields ¶
WithMergeFields sets column indices to be merged.
func WithPlaceholder ¶ added in v0.0.54
WithPlaceholder sets the placeholder for empty fields.
func WithWordDelimiter ¶ added in v0.0.3
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 (*Table) Load ¶ added in v0.0.4
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)