From 7e9746304a172cffffd2acd99105bdc065aac54f Mon Sep 17 00:00:00 2001 From: Cory Bennett Date: Mon, 2 Dec 2019 14:43:07 -0800 Subject: [PATCH] add template functions to handle table output, fixes #176, replaces #296 --- _t/100basic.t | 10 ++--- _t/120custom-commands.t | 10 ++--- _t/130basic-epics.t | 38 +++++++++--------- _t/140basic-attach.t | 44 ++++++++++----------- go.mod | 1 + go.sum | 4 ++ jiracli/templates.go | 87 +++++++++++++++++++++++++++++++---------- 7 files changed, 123 insertions(+), 71 deletions(-) diff --git a/_t/100basic.t b/_t/100basic.t index 0028ca3..51de422 100755 --- a/_t/100basic.t +++ b/_t/100basic.t @@ -66,11 +66,11 @@ EOF RUNS $jira ls --project BASIC --template table DIFF < 0 || len(cells) > 0 { + table.SetHeader(headers) + table.AppendBulk(cells) + table.Render() + } + return nil } @@ -296,23 +324,42 @@ const defaultDebugTemplate = "{{ . | toJson}}\n" const defaultListTemplate = "{{ range .issues }}{{ .key | append \":\" | printf \"%-12s\"}} {{ .fields.summary }}\n{{ end }}" const defaultTableTemplate = `{{/* table template */ -}} -{{$w := sub termWidth 107 -}} -+{{ "-" | rep 16 }}+{{ "-" | rep $w }}+{{ "-" | rep 14 }}+{{ "-" | rep 14 }}+{{ "-" | rep 14 }}+{{ "-" | rep 12 }}+{{ "-" | rep 14 }}+{{ "-" | rep 14 }}+ -| {{ "Issue" | printf "%-14s" }} | {{ "Summary" | printf (printf "%%-%ds" (sub $w 2)) }} | {{ "Type" | printf "%-12s"}} | {{ "Priority" | printf "%-12s" }} | {{ "Status" | printf "%-12s" }} | {{ "Age" | printf "%-10s" }} | {{ "Reporter" | printf "%-12s" }} | {{ "Assignee" | printf "%-12s" }} | -+{{ "-" | rep 16 }}+{{ "-" | rep $w }}+{{ "-" | rep 14 }}+{{ "-" | rep 14 }}+{{ "-" | rep 14 }}+{{ "-" | rep 12 }}+{{ "-" | rep 14 }}+{{ "-" | rep 14 }}+ -{{ range .issues -}} - | {{ .key | printf "%-14s"}} | {{ .fields.summary | abbrev (sub $w 2) | printf (printf "%%-%ds" (sub $w 2)) }} | {{.fields.issuetype.name | printf "%-12s" }} | {{if .fields.priority}}{{.fields.priority.name | printf "%-12s" }}{{else}}{{end}} | {{.fields.status.name | printf "%-12s" }} | {{.fields.created | age | printf "%-10s" }} | {{if .fields.reporter}}{{ .fields.reporter.name | printf "%-12s"}}{{else}}{{end}} | {{if .fields.assignee }}{{.fields.assignee.name | printf "%-12s" }}{{else}}{{end}} | -{{ end -}} -+{{ "-" | rep 16 }}+{{ "-" | rep $w }}+{{ "-" | rep 14 }}+{{ "-" | rep 14 }}+{{ "-" | rep 14 }}+{{ "-" | rep 12 }}+{{ "-" | rep 14 }}+{{ "-" | rep 14 }}+ +{{- headers "Issue" "Summary" "Type" "Priority" "Status" "Age" "Reporter" "Assignee" -}} +{{- range .issues -}} + {{- row -}} + {{- cell .key -}} + {{- cell .fields.summary -}} + {{- cell .fields.issuetype.name -}} + {{- if .fields.priority -}} + {{- cell .fields.priority.name -}} + {{- else -}} + {{- cell "" -}} + {{- end -}} + {{- cell .fields.status.name -}} + {{- cell (.fields.created | age) -}} + {{- if .fields.reporter -}} + {{- cell .fields.reporter.name -}} + {{- else -}} + {{- cell "" -}} + {{- end -}} + {{- if .fields.assignee -}} + {{- cell .fields.assignee.name -}} + {{- else -}} + {{- cell "" -}} + {{- end -}} +{{- end -}} ` -const defaultAttachListTemplate = `{{/* table template */ -}} -+{{ "-" | rep 12 }}+{{ "-" | rep 30 }}+{{ "-" | rep 12 }}+{{ "-" | rep 14 }}+{{ "-" | rep 14 }}+ -| {{printf "%-10s" "id"}} | {{printf "%-28s" "filename"}} | {{printf "%-10s" "bytes"}} | {{printf "%-12s" "user"}} | {{printf "%-12s" "created"}} | -+{{ "-" | rep 12 }}+{{ "-" | rep 30 }}+{{ "-" | rep 12 }}+{{ "-" | rep 14 }}+{{ "-" | rep 14 }}+ -{{range . -}} -| {{.id | printf "%10d" }} | {{.filename | printf "%-28s"}} | {{.size | printf "%10d"}} | {{.author.name | printf "%-12s"}} | {{.created | age | printf "%-12s"}} | -{{end -}} -+{{ "-" | rep 12 }}+{{ "-" | rep 30 }}+{{ "-" | rep 12 }}+{{ "-" | rep 14 }}+{{ "-" | rep 14 }}+ + +const defaultAttachListTemplate = `{{/* attach list template */ -}} +{{- headers "id" "filename" "bytes" "user" "created" -}} +{{- range . -}} + {{- row -}} + {{- cell .id -}} + {{- cell .filename -}} + {{- cell .size -}} + {{- cell .author.name -}} + {{- cell (.created | age) -}} +{{- end -}} ` const defaultViewTemplate = `{{/* view template */ -}}