Skip to content

Commit

Permalink
feat: add FormatCompressedText instead of compress flag
Browse files Browse the repository at this point in the history
  • Loading branch information
nekrassov01 committed Feb 20, 2024
1 parent 00343c7 commit d3b9029
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 49 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Text merged

![text_merged](_assets/text_merged.png)

Text compressed (only when text)
Compressed-text merged

![text_compressed](_assets/text_compressed.png)

Expand Down
Binary file modified _assets/backlog_merged.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified _assets/markdown_merged.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
24 changes: 14 additions & 10 deletions _examples/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -348,18 +348,10 @@ func main() {
+------------+--------------+-------+-----------------+---------------+------------+----------+--------+---------------+---------------+
*/

/*
Compress only when text format
*/

fmt.Println("/* Compress only when text format */")
fmt.Println()

fmt.Println("// format: text")
fmt.Println("// format: compressed")
fmt.Println("// mergeFields: []int{0, 1, 2, 3}")
fmt.Println("// compress: true")
fmt.Println()
table = mintab.New(os.Stdout, mintab.WithMergeFields([]int{0, 1, 2, 3}), mintab.WithCompress(true))
table = mintab.New(os.Stdout, mintab.WithFormat(mintab.FormatCompressedText), mintab.WithMergeFields([]int{0, 1, 2, 3}))
if err := table.Load(s2); err != nil {
log.Fatal(err)
}
Expand Down Expand Up @@ -565,4 +557,16 @@ func main() {
| i-2 | server-2 | vpc-1 | sg-3 | Egress | -1 | 0 | 0 | Ipv4 | 0.0.0.0/0 |
+------------+--------------+-------+-----------------+---------------+------------+----------+--------+---------------+---------------+
*/

table = mintab.New(os.Stdout, mintab.WithFormat(mintab.FormatMarkdown), mintab.WithMergeFields([]int{0, 1, 2, 3}))
if err := table.Load(s2); err != nil {
log.Fatal(err)
}
table.Out()

table = mintab.New(os.Stdout, mintab.WithFormat(mintab.FormatBacklog), mintab.WithMergeFields([]int{0, 1, 2, 3}))
if err := table.Load(s2); err != nil {
log.Fatal(err)
}
table.Out()
}
39 changes: 19 additions & 20 deletions mintab.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,16 @@ type Format int

// Enumeration of supported output formats.
const (
FormatText Format = iota // Plain text format.
FormatMarkdown // Markdown format.
FormatBacklog // Backlog-specific format.
FormatText Format = iota // Plain text format.
FormatCompressedText // Compressed plain text format.
FormatMarkdown // Markdown format.
FormatBacklog // Backlog-specific format.
)

// Formats holds the string representations of each format constant.
var Formats = []string{
"text",
"compressed",
"markdown",
"backlog",
}
Expand Down Expand Up @@ -63,7 +65,6 @@ type Table struct {
columnWidths []int // Calculated max width of each column.
hasHeader bool // Indicates if the header should be rendered.
hasEscape bool // Indicates if escaping should be performed.
compress bool
}

// New instantiates a new Table with the specified writer and options.
Expand Down Expand Up @@ -141,12 +142,6 @@ func WithEscape(has bool) Option {
}
}

func WithCompress(has bool) Option {
return func(t *Table) {
t.compress = has
}
}

// Load validates the input and converts it into table data.
// Returns an error if the input is not a slice or if it's empty.
func (t *Table) Load(input any) error {
Expand Down Expand Up @@ -188,7 +183,8 @@ func (t *Table) Load(input any) error {
// It supports markdown and backlog formats for easy copying and pasting.
func (t *Table) Out() {
if t.hasHeader {
if t.format == FormatText {
switch t.format {
case FormatText, FormatCompressedText:
t.printBorder()
}
t.printHeader()
Expand All @@ -197,7 +193,8 @@ func (t *Table) Out() {
t.printBorder()
}
t.printData()
if t.format == FormatText {
switch t.format {
case FormatText, FormatCompressedText:
t.printBorder()
}
fmt.Fprintf(t.writer, "\n")
Expand All @@ -223,14 +220,13 @@ func (t *Table) printHeader() {
// printData renders the table data with dynamic conditional borders.
func (t *Table) printData() {
for ri, row := range t.data {
if ri > 0 && t.format == FormatText {
if t.compress {
if row[0] != "" {
t.printBorder()
}
} else {
if ri > 0 {
if t.format == FormatText {
t.printDataBorder(row)
}
if t.format == FormatCompressedText && row[0] != "" {
t.printBorder()
}
}
lines := 1
splitFields := make([][]string, len(row))
Expand Down Expand Up @@ -362,9 +358,12 @@ func (t *Table) setData(v reflect.Value) error {

// setBorder computes the table border string based on the calculated column widths.
func (t *Table) setBorder() {
sep := "+"
if t.format != FormatText {
var sep string
switch t.format {
case FormatMarkdown, FormatBacklog:
sep = "|"
default:
sep = "+"
}
t.builder.Reset()
for _, width := range t.columnWidths {
Expand Down
26 changes: 8 additions & 18 deletions mintab_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,11 @@ func TestFormat_String(t *testing.T) {
o: FormatText,
want: "text",
},
{
name: "compressed",
o: FormatCompressedText,
want: "compressed",
},
{
name: "markdown",
o: FormatMarkdown,
Expand All @@ -150,7 +155,7 @@ func TestFormat_String(t *testing.T) {
},
{
name: "other",
o: 3,
o: 9,
want: "",
},
}
Expand Down Expand Up @@ -192,7 +197,6 @@ func TestNew(t *testing.T) {
columnWidths: nil,
hasHeader: true,
hasEscape: false,
compress: false,
},
},
{
Expand All @@ -207,7 +211,6 @@ func TestNew(t *testing.T) {
WithMergeFields([]int{0}),
WithIgnoreFields([]int{0}),
WithEscape(true),
WithCompress(true),
},
},
want: &Table{
Expand All @@ -224,7 +227,6 @@ func TestNew(t *testing.T) {
columnWidths: nil,
hasHeader: false,
hasEscape: true,
compress: true,
},
},
}
Expand Down Expand Up @@ -352,7 +354,6 @@ func TestTable_Out(t *testing.T) {
header []string
data [][]string
columnWidths []int
compress bool
}
tests := []struct {
name string
Expand All @@ -373,7 +374,6 @@ func TestTable_Out(t *testing.T) {
{"i-6", "server-6", "-", "tg-5\ntg-6\ntg-7\ntg-8"},
},
columnWidths: []int{10, 12, 10, 10},
compress: false,
},
want: `+------------+--------------+------------+------------+
| InstanceID | InstanceName | AttachedLB | AttachedTG |
Expand Down Expand Up @@ -401,7 +401,7 @@ func TestTable_Out(t *testing.T) {
{
name: "text_with_compressed",
fields: fields{
format: FormatText,
format: FormatCompressedText,
header: []string{"InstanceID", "InstanceName", "VPCID", "SecurityGroupID", "FlowDirection", "IPProtocol", "FromPort", "ToPort", "AddressType", "CidrBlock"},
data: [][]string{
{"i-1", "server-1", "vpc-1", "sg-1", "Ingress", "tcp", "22", "22", "SecurityGroup", "sg-10"},
Expand All @@ -414,7 +414,6 @@ func TestTable_Out(t *testing.T) {
{"", "", "", "", "Egress", "-1", "0", "0", "Ipv4", "0.0.0.0/0"},
},
columnWidths: []int{10, 12, 5, 15, 13, 10, 8, 6, 13, 13},
compress: true,
},
want: `+------------+--------------+-------+-----------------+---------------+------------+----------+--------+---------------+---------------+
| InstanceID | InstanceName | VPCID | SecurityGroupID | FlowDirection | IPProtocol | FromPort | ToPort | AddressType | CidrBlock |
Expand Down Expand Up @@ -446,7 +445,6 @@ func TestTable_Out(t *testing.T) {
{"i-6", "server-6", "\\-", "tg-5<br>tg-6<br>tg-7<br>tg-8"},
},
columnWidths: []int{10, 12, 12, 28},
compress: false,
},
want: `| InstanceID | InstanceName | AttachedLB | AttachedTG |
|------------|--------------|--------------|------------------------------|
Expand All @@ -473,7 +471,6 @@ func TestTable_Out(t *testing.T) {
{"i-6", "server-6", "-", "tg-5&br;tg-6&br;tg-7&br;tg-8"},
},
columnWidths: []int{10, 12, 12, 28},
compress: false,
},
want: `| InstanceID | InstanceName | AttachedLB | AttachedTG |h
| i-1 | server-1 | lb-1 | tg-1 |
Expand All @@ -494,7 +491,6 @@ func TestTable_Out(t *testing.T) {
tr.header = tt.fields.header
tr.data = tt.fields.data
tr.columnWidths = tt.fields.columnWidths
tr.compress = tt.fields.compress
tr.setBorder()
tr.Out()
if !reflect.DeepEqual(buf.String(), tt.want) {
Expand Down Expand Up @@ -598,7 +594,6 @@ func TestTable_printData(t *testing.T) {
data [][]string
format Format
columnWidths []int
compress bool
}
tests := []struct {
name string
Expand All @@ -618,7 +613,6 @@ func TestTable_printData(t *testing.T) {
},
format: FormatText,
columnWidths: []int{10, 12, 10, 10},
compress: false,
},
want: `| i-1 | server-1 | lb-1 | tg-1 |
+------------+--------------+------------+------------+
Expand Down Expand Up @@ -651,7 +645,6 @@ func TestTable_printData(t *testing.T) {
},
format: FormatMarkdown,
columnWidths: []int{10, 12, 12, 28},
compress: false,
},
want: `| i-1 | server-1 | lb-1 | tg-1 |
| i-2 | server-2 | lb-2<br>lb-3 | tg-2 |
Expand All @@ -674,7 +667,6 @@ func TestTable_printData(t *testing.T) {
},
format: FormatBacklog,
columnWidths: []int{10, 12, 12, 28},
compress: false,
},
want: `| i-1 | server-1 | lb-1 | tg-1 |
| i-2 | server-2 | lb-2&br;lb-3 | tg-2 |
Expand All @@ -697,9 +689,8 @@ func TestTable_printData(t *testing.T) {
{"", "", "", "", "Ingress", "tcp", "0", "65535", "PrefixList", "pl-id/pl-name"},
{"", "", "", "", "Egress", "-1", "0", "0", "Ipv4", "0.0.0.0/0"},
},
format: FormatText,
format: FormatCompressedText,
columnWidths: []int{10, 12, 5, 15, 13, 10, 8, 6, 13, 13},
compress: true,
},
want: `| i-1 | server-1 | vpc-1 | sg-1 | Ingress | tcp | 22 | 22 | SecurityGroup | sg-10 |
| | | | | Egress | -1 | 0 | 0 | Ipv4 | 0.0.0.0/0 |
Expand All @@ -720,7 +711,6 @@ func TestTable_printData(t *testing.T) {
tr.data = tt.fields.data
tr.format = tt.fields.format
tr.columnWidths = tt.fields.columnWidths
tr.compress = tt.fields.compress
tr.setBorder()
tr.printData()
if !reflect.DeepEqual(buf.String(), tt.want) {
Expand Down

0 comments on commit d3b9029

Please sign in to comment.