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 template functions to handle table output, fixes #176, replaces #296 #302

Merged
merged 1 commit into from
Dec 3, 2019
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
10 changes: 5 additions & 5 deletions _t/100basic.t
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,11 @@ EOF

RUNS $jira ls --project BASIC --template table
DIFF <<EOF
+----------------+------------------------------------------+--------------+--------------+--------------+------------+--------------+--------------+
| Issue | Summary | Type | Priority | Status | Age | Reporter | Assignee |
+----------------+------------------------------------------+--------------+--------------+--------------+------------+--------------+--------------+
| $(printf %-14s $issue) | summary | Bug | Medium | To Do | a minute | gojira | gojira |
+----------------+------------------------------------------+--------------+--------------+--------------+------------+--------------+--------------+
+------------+---------+------+----------+--------+----------+----------+----------+
| Issue | Summary | Type | Priority | Status | Age | Reporter | Assignee |
+------------+---------+------+----------+--------+----------+----------+----------+
| $issue | summary | Bug | Medium | To Do | a minute | gojira | gojira |
+------------+---------+------+----------+--------+----------+----------+----------+
EOF

###############################################################################
Expand Down
10 changes: 5 additions & 5 deletions _t/120custom-commands.t
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ EOF

RUNS $jira mine
DIFF <<EOF
+----------------+------------------------------------------+--------------+--------------+--------------+------------+--------------+--------------+
| Issue | Summary | Type | Priority | Status | Age | Reporter | Assignee |
+----------------+------------------------------------------+--------------+--------------+--------------+------------+--------------+--------------+
| $(printf %-14s $issue) | summary | Bug | Medium | To Do | a minute | gojira | gojira |
+----------------+------------------------------------------+--------------+--------------+--------------+------------+--------------+--------------+
+------------+---------+------+----------+--------+----------+----------+----------+
| Issue | Summary | Type | Priority | Status | Age | Reporter | Assignee |
+------------+---------+------+----------+--------+----------+----------+----------+
| $issue | summary | Bug | Medium | To Do | a minute | gojira | gojira |
+------------+---------+------+----------+--------+----------+----------+----------+
EOF
38 changes: 19 additions & 19 deletions _t/130basic-epics.t
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ EOF
RUNS $jira epic list $epic

DIFF<<EOF
+----------------+------------------------------------------+--------------+--------------+--------------+------------+--------------+--------------+
| Issue | Summary | Type | Priority | Status | Age | Reporter | Assignee |
+----------------+------------------------------------------+--------------+--------------+--------------+------------+--------------+--------------+
+----------------+------------------------------------------+--------------+--------------+--------------+------------+--------------+--------------+
+-------+---------+------+----------+--------+-----+----------+----------+
| Issue | Summary | Type | Priority | Status | Age | Reporter | Assignee |
+-------+---------+------+----------+--------+-----+----------+----------+
+-------+---------+------+----------+--------+-----+----------+----------+
EOF

###############################################################################
Expand All @@ -69,12 +69,12 @@ EOF
RUNS $jira epic list $epic

DIFF<<EOF
+----------------+------------------------------------------+--------------+--------------+--------------+------------+--------------+--------------+
| Issue | Summary | Type | Priority | Status | Age | Reporter | Assignee |
+----------------+------------------------------------------+--------------+--------------+--------------+------------+--------------+--------------+
| $(printf %-14s $issue1) | summary | Bug | Medium | To Do | a minute | gojira | gojira |
| $(printf %-14s $issue2) | summary | Bug | Medium | To Do | a minute | gojira | gojira |
+----------------+------------------------------------------+--------------+--------------+--------------+------------+--------------+--------------+
+------------+---------+------+----------+--------+----------+----------+----------+
| Issue | Summary | Type | Priority | Status | Age | Reporter | Assignee |
+------------+---------+------+----------+--------+----------+----------+----------+
| $issue1 | summary | Bug | Medium | To Do | a minute | gojira | gojira |
| $issue2 | summary | Bug | Medium | To Do | a minute | gojira | gojira |
+------------+---------+------+----------+--------+----------+----------+----------+
EOF

###############################################################################
Expand All @@ -92,11 +92,11 @@ EOF
RUNS $jira epic list $epic

DIFF<<EOF
+----------------+------------------------------------------+--------------+--------------+--------------+------------+--------------+--------------+
| Issue | Summary | Type | Priority | Status | Age | Reporter | Assignee |
+----------------+------------------------------------------+--------------+--------------+--------------+------------+--------------+--------------+
| $(printf %-14s $issue2) | summary | Bug | Medium | To Do | a minute | gojira | gojira |
+----------------+------------------------------------------+--------------+--------------+--------------+------------+--------------+--------------+
+------------+---------+------+----------+--------+----------+----------+----------+
| Issue | Summary | Type | Priority | Status | Age | Reporter | Assignee |
+------------+---------+------+----------+--------+----------+----------+----------+
| $issue2 | summary | Bug | Medium | To Do | a minute | gojira | gojira |
+------------+---------+------+----------+--------+----------+----------+----------+
EOF

###############################################################################
Expand All @@ -114,8 +114,8 @@ EOF
RUNS $jira epic list $epic

DIFF<<EOF
+----------------+------------------------------------------+--------------+--------------+--------------+------------+--------------+--------------+
| Issue | Summary | Type | Priority | Status | Age | Reporter | Assignee |
+----------------+------------------------------------------+--------------+--------------+--------------+------------+--------------+--------------+
+----------------+------------------------------------------+--------------+--------------+--------------+------------+--------------+--------------+
+-------+---------+------+----------+--------+-----+----------+----------+
| Issue | Summary | Type | Priority | Status | Age | Reporter | Assignee |
+-------+---------+------+----------+--------+-----+----------+----------+
+-------+---------+------+----------+--------+-----+----------+----------+
EOF
44 changes: 22 additions & 22 deletions _t/140basic-attach.t
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,13 @@ EOF
###############################################################################
RUNS $jira attach list $issue
DIFF <<EOF
+------------+------------------------------+------------+--------------+--------------+
| id | filename | bytes | user | created |
+------------+------------------------------+------------+--------------+--------------+
| $(printf %10s $attach1) | README.md | 1239 | gojira | a minute |
| $(printf %10s $attach2) | garbage.bin | 1048576 | gojira | a minute |
| $(printf %10s $attach3) | foobar.bin | 1048576 | gojira | a minute |
+------------+------------------------------+------------+--------------+--------------+
+-------+-------------+---------+--------+----------+
| id | filename | bytes | user | created |
+-------+-------------+---------+--------+----------+
| $attach1 | README.md | 1239 | gojira | a minute |
| $attach2 | garbage.bin | 1048576 | gojira | a minute |
| $attach3 | foobar.bin | 1048576 | gojira | a minute |
+-------+-------------+---------+--------+----------+
EOF

###############################################################################
Expand Down Expand Up @@ -146,12 +146,12 @@ EOF

RUNS $jira attach list $issue
DIFF <<EOF
+------------+------------------------------+------------+--------------+--------------+
| id | filename | bytes | user | created |
+------------+------------------------------+------------+--------------+--------------+
| $(printf %10s $attach2) | garbage.bin | 1048576 | gojira | a minute |
| $(printf %10s $attach3) | foobar.bin | 1048576 | gojira | a minute |
+------------+------------------------------+------------+--------------+--------------+
+-------+-------------+---------+--------+----------+
| id | filename | bytes | user | created |
+-------+-------------+---------+--------+----------+
| $attach2 | garbage.bin | 1048576 | gojira | a minute |
| $attach3 | foobar.bin | 1048576 | gojira | a minute |
+-------+-------------+---------+--------+----------+
EOF


Expand All @@ -165,11 +165,11 @@ EOF

RUNS $jira attach list $issue
DIFF <<EOF
+------------+------------------------------+------------+--------------+--------------+
| id | filename | bytes | user | created |
+------------+------------------------------+------------+--------------+--------------+
| $(printf %10s $attach3) | foobar.bin | 1048576 | gojira | a minute |
+------------+------------------------------+------------+--------------+--------------+
+-------+------------+---------+--------+----------+
| id | filename | bytes | user | created |
+-------+------------+---------+--------+----------+
| $attach3 | foobar.bin | 1048576 | gojira | a minute |
+-------+------------+---------+--------+----------+
EOF

###############################################################################
Expand All @@ -182,8 +182,8 @@ EOF

RUNS $jira attach list $issue
DIFF <<EOF
+------------+------------------------------+------------+--------------+--------------+
| id | filename | bytes | user | created |
+------------+------------------------------+------------+--------------+--------------+
+------------+------------------------------+------------+--------------+--------------+
+----+----------+-------+------+---------+
| id | filename | bytes | user | created |
+----+----------+-------+------+---------+
+----+----------+-------+------+---------+
EOF
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ require (
github.com/mattn/go-colorable v0.0.9 // indirect
github.com/mattn/go-isatty v0.0.3 // indirect
github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b
github.com/olekukonko/tablewriter v0.0.3
github.com/pkg/browser v0.0.0-20170505125900-c90ca0c84f15
github.com/pkg/errors v0.8.0
github.com/pmezard/go-difflib v1.0.0 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,12 @@ github.com/mattn/go-colorable v0.0.9 h1:UVL0vNpWh04HeJXV0KLcaT7r06gOH2l4OW6ddYRU
github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
github.com/mattn/go-isatty v0.0.3 h1:ns/ykhmWi7G9O+8a448SecJU3nSMBXJfqQkl0upE1jI=
github.com/mattn/go-isatty v0.0.3/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
github.com/mattn/go-runewidth v0.0.6 h1:V2iyH+aX9C5fsYCpK60U8BYIvmhqxuOL3JZcqc1NB7k=
github.com/mattn/go-runewidth v0.0.6/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b h1:j7+1HpAFS1zy5+Q4qx1fWh90gTKwiN4QCGoY9TWyyO4=
github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b/go.mod h1:01TrycV0kFyexm33Z7vhZRXopbI8J3TDReVlkTgMUxE=
github.com/olekukonko/tablewriter v0.0.3 h1:i0LBnzgiChAWHJYTQAZJDOgf8MNxAVYZJ2m63SIDimI=
github.com/olekukonko/tablewriter v0.0.3/go.mod h1:YZeBtGzYYEsCHp2LST/u/0NDwGkRoBtmn1cIWCJiS6M=
github.com/pkg/browser v0.0.0-20170505125900-c90ca0c84f15 h1:mrI+6Ae64Wjt+uahGe5we/sPS1sXjvfT3YjtawAVgps=
github.com/pkg/browser v0.0.0-20170505125900-c90ca0c84f15/go.mod h1:4OwLy04Bl9Ef3GJJCoec+30X3LQs/0/m4HFRt/2LUSA=
github.com/pkg/errors v0.8.0 h1:WdK/asTD0HN+q6hsWO3/vpuAkAr+tw6aNJNDFFf0+qw=
Expand Down
87 changes: 67 additions & 20 deletions jiracli/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/coryb/figtree"
shellquote "github.com/kballard/go-shellquote"
"github.com/mgutz/ansi"
"github.com/olekukonko/tablewriter"
"golang.org/x/crypto/ssh/terminal"
)

Expand All @@ -35,8 +36,8 @@ func findTemplate(name string) ([]byte, error) {
}

func getTemplate(name string) (string, error) {
if _, err := os.Stat(".jira.d/"+name); err == nil {
b, err := ioutil.ReadFile(".jira.d/"+name)
if _, err := os.Stat(".jira.d/" + name); err == nil {
b, err := ioutil.ReadFile(".jira.d/" + name)
if err != nil {
return "", err
}
Expand Down Expand Up @@ -152,7 +153,7 @@ func TemplateProcessor() *template.Template {
return ansi.ColorCode(color)
},
"remLineBreak": func(content string) string {
return strings.Replace(strings.Replace(content,string('\r'),string(' '),-1),string('\n'),string(' '),-1)
return strings.Replace(strings.Replace(content, string('\r'), string(' '), -1), string('\n'), string(' '), -1)
},
"regReplace": func(search string, replace string, content string) string {
re := regexp.MustCompile(search)
Expand Down Expand Up @@ -253,13 +254,40 @@ func RunTemplate(templateName string, data interface{}, out io.Writer) error {
return err
}

tmpl, err := TemplateProcessor().Parse(templateContent)
table := tablewriter.NewWriter(out)
table.SetAutoFormatHeaders(false)
headers := []string{}
cells := [][]string{}
tmpl, err := TemplateProcessor().Funcs(map[string]interface{}{
"headers": func(titles ...string) string {
headers = append(headers, titles...)
return ""
},
"row": func() string {
cells = append(cells, []string{})
return ""
},
"cell": func(value interface{}) (string, error) {
if len(cells) == 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))
return "", nil
},
}).Parse(templateContent)
if err != nil {
return err
}

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()
}

return nil
}

Expand Down Expand Up @@ -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}}<unassigned>{{end}} | {{.fields.status.name | printf "%-12s" }} | {{.fields.created | age | printf "%-10s" }} | {{if .fields.reporter}}{{ .fields.reporter.name | printf "%-12s"}}{{else}}<unassigned>{{end}} | {{if .fields.assignee }}{{.fields.assignee.name | printf "%-12s" }}{{else}}<unassigned>{{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 "<none>" -}}
{{- end -}}
{{- cell .fields.status.name -}}
{{- cell (.fields.created | age) -}}
{{- if .fields.reporter -}}
{{- cell .fields.reporter.name -}}
{{- else -}}
{{- cell "<unknown>" -}}
{{- end -}}
{{- if .fields.assignee -}}
{{- cell .fields.assignee.name -}}
{{- else -}}
{{- cell "<unassigned>" -}}
{{- 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 */ -}}
Expand Down