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 ability to put a table anywhere #497

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
54 changes: 40 additions & 14 deletions jiracli/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,28 +258,52 @@ func RunTemplate(templateName string, data interface{}, out io.Writer) error {
return err
}

table := tablewriter.NewWriter(out)
table.SetAutoFormatHeaders(false)
headers := []string{}
cells := [][]string{}
footerTable := tablewriter.NewWriter(out)
footerTable.SetAutoFormatHeaders(false)
footerHeaders := []string{}
footerCells := [][]string{}

var table *tablewriter.Table
var table_str *strings.Builder
tmpl, err := TemplateProcessor().Funcs(map[string]interface{}{
"newTable": func(mw int) string {
table_str = new(strings.Builder)
table = tablewriter.NewWriter(table_str)
table.SetAutoFormatHeaders(false)
table.SetBorders(tablewriter.Border{Left: true, Top: false, Right: true, Bottom: false})
table.SetCenterSeparator("|")
table.SetColWidth(mw)
return ""
},
"tableHeaders": func(args ...string) string {
table.SetHeader(args)
return ""
},
"tableRow": func(args ...string) string {
table.Append(args)
return ""
},
"tableRender": func() string {
table.Render()
return table_str.String()
},
"defaultColWidth": func(cw int) string {
table.SetColWidth(cw)
footerTable.SetColWidth(cw)
return ""
},
"headers": func(titles ...string) string {
headers = append(headers, titles...)
footerHeaders = append(footerHeaders, titles...)
return ""
},
"row": func() string {
cells = append(cells, []string{})
footerCells = append(footerCells, []string{})
return ""
},
"cell": func(value interface{}) (string, error) {
if len(cells) == 0 {
if len(footerCells) == 0 {
return "", fmt.Errorf(`"cell" template function called before "row" template function`)
}
cells[len(cells)-1] = append(cells[len(cells)-1], fmt.Sprintf("%v", value))
footerCells[len(footerCells)-1] = append(footerCells[len(footerCells)-1], fmt.Sprintf("%v", value))
return "", nil
},
}).Parse(templateContent)
Expand All @@ -290,10 +314,12 @@ func RunTemplate(templateName string, data interface{}, out io.Writer) error {
if err := tmpl.Execute(out, rawData); err != nil {
return err
}
if len(headers) > 0 || len(cells) > 0 {
table.SetHeader(headers)
table.AppendBulk(cells)
table.Render()
if len(footerHeaders) > 0 || len(footerCells) > 0 {
footerTable.SetHeader(footerHeaders)
footerTable.AppendBulk(footerCells)
footerTable.SetBorders(tablewriter.Border{Left: true, Top: false, Right: true, Bottom: false})
footerTable.SetCenterSeparator("|")
footerTable.Render()
}

return nil
Expand Down Expand Up @@ -333,7 +359,7 @@ const defaultListTemplate = "{{ range .issues }}{{ .key | append \":\" | printf

const defaultTableTemplate = `{{/* table template */ -}}
{{- headers "Issue" "Summary" "Type" "Priority" "Status" "Age" "Reporter" "Assignee" -}}
{{- range .issues -}}
{{- range .issues -}}
{{- row -}}
{{- cell .key -}}
{{- cell .fields.summary -}}
Expand Down