Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add control over rendering Table's column separator and header #19

Merged
merged 1 commit into from
Jun 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion cli/workflowCommands.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,8 @@ func RunWorkflow(c *cli.Context) {
&row{Field: "Args", Value: truncate(payloads.ToString(input))},
}
fmt.Println(colorMagenta("Running execution:"))
terminal.PrintItems(c, executionData, []string{"Field", "Value"})
opts := &terminal.PrintOptions{Fields: []string{"Field", "Value"}, Header: false}
terminal.PrintItems(c, executionData, opts)

printWorkflowProgress(c, wid, resp.GetRunId())
}
Expand Down
13 changes: 10 additions & 3 deletions terminal/paginate.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ func Paginate(c *cli.Context, iter collection.Iterator, opts *PaginateOptions) e
all := opts.All

var pageItems []interface{}
var printOpts = &PrintOptions{
Fields: opts.Fields,
Header: true,
Separator: opts.Separator,
}
for iter.HasNext() {
item, err := iter.Next()
if err != nil {
Expand All @@ -57,8 +62,9 @@ func Paginate(c *cli.Context, iter collection.Iterator, opts *PaginateOptions) e
shouldPrintPage := len(pageItems) == pageSize || !iter.HasNext()

if all || shouldPrintPage {
PrintItems(c, pageItems, opts.Fields)
PrintItems(c, pageItems, printOpts)
pageItems = pageItems[:0]
printOpts.Header = false
if detach {
break
} else if all {
Expand All @@ -73,8 +79,9 @@ func Paginate(c *cli.Context, iter collection.Iterator, opts *PaginateOptions) e
}

type PaginateOptions struct {
Fields []string
All bool
Fields []string
All bool
Separator string
}

func promtNextPage() bool {
Expand Down
22 changes: 17 additions & 5 deletions terminal/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,26 @@ import (
"github.com/urfave/cli/v2"
)

func PrintItems(c *cli.Context, items []interface{}, fields []string) {
type PrintOptions struct {
Fields []string
Header bool
Separator string
}

func PrintItems(c *cli.Context, items []interface{}, opts *PrintOptions) {
if opts == nil {
opts = &PrintOptions{}
}
isJSONView := c.Bool(FlagJSON)
if isJSONView {
printJSON(items)
} else {
printTable(items, fields)
printTable(items, opts)
}
}

func printTable(items []interface{}, fields []string) {
func printTable(items []interface{}, opts *PrintOptions) {
fields := opts.Fields
if len(fields) == 0 {
// dynamically examine fields
if len(items) == 0 {
Expand All @@ -64,8 +74,10 @@ func printTable(items []interface{}, fields []string) {

table := tablewriter.NewWriter(os.Stdout)
table.SetBorder(false)
table.SetColumnSeparator("|")
table.SetHeader(fields)
table.SetColumnSeparator(opts.Separator)
if opts.Header {
table.SetHeader(fields)
}
table.SetHeaderLine(false)

for _, item := range items {
Expand Down