-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9e8b547
commit 43bf009
Showing
5 changed files
with
203 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
package printer | ||
|
||
import ( | ||
"github.com/jedib0t/go-pretty/v6/table" | ||
"github.com/jedib0t/go-pretty/v6/text" | ||
"github.com/siyul-park/uniflow/pkg/primitive" | ||
"github.com/xiatechs/jsonata-go" | ||
) | ||
|
||
type ( | ||
TableColumnDefinition struct { | ||
Name string | ||
Format string | ||
} | ||
|
||
TablePrinter struct { | ||
names []string | ||
formats []*jsonata.Expr | ||
} | ||
) | ||
|
||
var ( | ||
style = table.Style{ | ||
Name: "StyleDefault", | ||
Box: table.BoxStyle{ | ||
BottomLeft: "", | ||
BottomRight: "", | ||
BottomSeparator: "", | ||
EmptySeparator: text.RepeatAndTrim(" ", text.RuneWidthWithoutEscSequences(" ")), | ||
Left: "", | ||
LeftSeparator: "", | ||
MiddleHorizontal: "", | ||
MiddleSeparator: "", | ||
MiddleVertical: "", | ||
PaddingLeft: " ", | ||
PaddingRight: " ", | ||
PageSeparator: "\n", | ||
Right: "", | ||
RightSeparator: "", | ||
TopLeft: "", | ||
TopRight: "", | ||
TopSeparator: "", | ||
UnfinishedRow: " ~", | ||
}, | ||
Color: table.ColorOptionsDefault, | ||
Format: table.FormatOptionsDefault, | ||
HTML: table.DefaultHTMLOptions, | ||
Options: table.Options{ | ||
DrawBorder: false, | ||
SeparateColumns: true, | ||
SeparateFooter: false, | ||
SeparateHeader: false, | ||
SeparateRows: false, | ||
}, | ||
Title: table.TitleOptionsDefault, | ||
} | ||
) | ||
|
||
func NewTable(columns []TableColumnDefinition) (*TablePrinter, error) { | ||
names := make([]string, len(columns)) | ||
formats := make([]*jsonata.Expr, len(columns)) | ||
|
||
for i, column := range columns { | ||
name := column.Name | ||
format, err := jsonata.Compile(column.Format) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
names[i] = name | ||
formats[i] = format | ||
} | ||
|
||
return &TablePrinter{ | ||
names: names, | ||
formats: formats, | ||
}, nil | ||
} | ||
|
||
func (p *TablePrinter) Print(data any) (string, error) { | ||
value, err := primitive.MarshalText(data) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
var elements []any | ||
if v, ok := value.(*primitive.Slice); ok { | ||
elements = v.Slice() | ||
} else if v, ok := value.(*primitive.Map); ok { | ||
elements = append(elements, v.Interface()) | ||
} | ||
|
||
header := make(table.Row, len(p.names)) | ||
for i, name := range p.names { | ||
header[i] = name | ||
} | ||
|
||
rows := make([]table.Row, len(elements)) | ||
for i, element := range elements { | ||
row := make(table.Row, len(p.formats)) | ||
for j, format := range p.formats { | ||
data, _ := format.Eval(element) | ||
row[j] = data | ||
} | ||
rows[i] = row | ||
} | ||
|
||
tb := table.NewWriter() | ||
tb.SetStyle(style) | ||
tb.AppendHeader(header) | ||
tb.AppendRows(rows) | ||
|
||
return tb.Render(), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package printer | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestTablePrinter_Print(t *testing.T) { | ||
data := []map[string]any{ | ||
{ | ||
"foo": "foo", | ||
"bar": "bar", | ||
}, | ||
} | ||
|
||
p, err := NewTable([]TableColumnDefinition{ | ||
{ | ||
Name: "foo", | ||
Format: "$.foo", | ||
}, | ||
{ | ||
Name: "bar", | ||
Format: "$.bar", | ||
}, | ||
}) | ||
assert.NoError(t, err) | ||
|
||
expect := " FOO BAR \n foo bar " | ||
|
||
table, err := p.Print(data) | ||
assert.NoError(t, err) | ||
assert.Equal(t, expect, table) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters