Add tea issues --fields, allow printing labels (#312)

generalize list printing with dynamic fields

refactor print.IssuesList to use tableFromItems()

preparatory refactor

print.IssuesList: allow printing labels

move formatters to formatters.go

expose more printable fields on issue

add generic flags.FieldsFlag

add fields flag to tea issues, tea ms issues

validate provided fields

add strict username, or formatted user fields

change default fields

tea issues -> replace updated with labels
tea ms issues -> replace author with labels, reorder

Validate provided fields

Co-authored-by: Norwin Roosen <git@nroo.de>
Co-authored-by: 6543 <6543@obermui.de>
Reviewed-on: https://gitea.com/gitea/tea/pulls/312
Reviewed-by: Lunny Xiao <xiaolunwen@gmail.com>
Reviewed-by: 6543 <6543@obermui.de>
Co-Authored-By: Norwin <noerw@noreply.gitea.io>
Co-Committed-By: Norwin <noerw@noreply.gitea.io>
This commit is contained in:
Norwin
2020-12-21 23:41:07 +08:00
committed by 6543
parent 8bb5c15745
commit 9efee7bf99
16 changed files with 343 additions and 222 deletions

View File

@ -22,6 +22,24 @@ type table struct {
sortColumn uint // ↑
}
// printable can be implemented for structs to put fields dynamically into a table
type printable interface {
FormatField(field string) string
}
// high level api to print a table of items with dynamic fields
func tableFromItems(fields []string, values []printable) table {
t := table{headers: fields}
for _, v := range values {
row := make([]string, len(fields))
for i, f := range fields {
row[i] = v.FormatField(f)
}
t.addRowSlice(row)
}
return t
}
func tableWithHeader(header ...string) table {
return table{headers: header}
}
@ -54,16 +72,16 @@ func (t table) Less(i, j int) bool {
}
func (t *table) print(output string) {
switch {
case output == "" || output == "table":
switch output {
case "", "table":
outputtable(t.headers, t.values)
case output == "csv":
case "csv":
outputdsv(t.headers, t.values, ",")
case output == "simple":
case "simple":
outputsimple(t.headers, t.values)
case output == "tsv":
case "tsv":
outputdsv(t.headers, t.values, "\t")
case output == "yaml":
case "yml", "yaml":
outputyaml(t.headers, t.values)
default:
fmt.Printf("unknown output type '" + output + "', available types are:\n- csv: comma-separated values\n- simple: space-separated values\n- table: auto-aligned table format (default)\n- tsv: tab-separated values\n- yaml: YAML format\n")
@ -119,3 +137,11 @@ func outputyaml(headers []string, values [][]string) {
}
}
}
func isMachineReadable(outputFormat string) bool {
switch outputFormat {
case "yml", "yaml", "csv":
return true
}
return false
}