2020-12-09 22:04:36 +00:00
|
|
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
2018-09-03 06:43:00 +00:00
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2020-09-30 05:11:33 +00:00
|
|
|
package print
|
2018-09-03 06:43:00 +00:00
|
|
|
|
2019-09-15 08:38:18 +00:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2020-12-09 22:04:36 +00:00
|
|
|
"sort"
|
2019-09-15 08:38:18 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/olekukonko/tablewriter"
|
|
|
|
)
|
2018-09-03 06:43:00 +00:00
|
|
|
|
2020-12-09 22:04:36 +00:00
|
|
|
// table provides infrastructure to easily print (sorted) lists in different formats
|
|
|
|
type table struct {
|
|
|
|
headers []string
|
|
|
|
values [][]string
|
|
|
|
sortDesc bool // used internally by sortable interface
|
|
|
|
sortColumn uint // ↑
|
|
|
|
}
|
|
|
|
|
2020-12-21 15:41:07 +00:00
|
|
|
// printable can be implemented for structs to put fields dynamically into a table
|
|
|
|
type printable interface {
|
PR listing: add --fields & expose additional fields (#415)
This PR adds the `--fields` flag to `tea pr ls` (#342), and exposes more fields specific to the `PullRequest` type:
```
--fields value, -f value Comma-separated list of fields to print.
Available values:
index,state,author,author-id,url,title,body,mergeable,base,base-commit,head,diff,patch,created,updated,deadline,assignees,milestone,labels,comments
(default: "index,title,state,author,milestone,updated,labels")
```
Co-authored-by: justusbunsi <61625851+justusbunsi@users.noreply.github.com>
Co-authored-by: Norwin <git@nroo.de>
Reviewed-on: https://gitea.com/gitea/tea/pulls/415
Reviewed-by: Norwin <noerw@noreply.gitea.io>
Reviewed-by: techknowlogick <techknowlogick@gitea.io>
Reviewed-by: 6543 <6543@obermui.de>
Co-authored-by: justusbunsi <justusbunsi@noreply.gitea.io>
Co-committed-by: justusbunsi <justusbunsi@noreply.gitea.io>
2021-09-28 20:36:33 +00:00
|
|
|
FormatField(field string, machineReadable bool) string
|
2020-12-21 15:41:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// high level api to print a table of items with dynamic fields
|
PR listing: add --fields & expose additional fields (#415)
This PR adds the `--fields` flag to `tea pr ls` (#342), and exposes more fields specific to the `PullRequest` type:
```
--fields value, -f value Comma-separated list of fields to print.
Available values:
index,state,author,author-id,url,title,body,mergeable,base,base-commit,head,diff,patch,created,updated,deadline,assignees,milestone,labels,comments
(default: "index,title,state,author,milestone,updated,labels")
```
Co-authored-by: justusbunsi <61625851+justusbunsi@users.noreply.github.com>
Co-authored-by: Norwin <git@nroo.de>
Reviewed-on: https://gitea.com/gitea/tea/pulls/415
Reviewed-by: Norwin <noerw@noreply.gitea.io>
Reviewed-by: techknowlogick <techknowlogick@gitea.io>
Reviewed-by: 6543 <6543@obermui.de>
Co-authored-by: justusbunsi <justusbunsi@noreply.gitea.io>
Co-committed-by: justusbunsi <justusbunsi@noreply.gitea.io>
2021-09-28 20:36:33 +00:00
|
|
|
func tableFromItems(fields []string, values []printable, machineReadable bool) table {
|
2020-12-21 15:41:07 +00:00
|
|
|
t := table{headers: fields}
|
|
|
|
for _, v := range values {
|
|
|
|
row := make([]string, len(fields))
|
|
|
|
for i, f := range fields {
|
PR listing: add --fields & expose additional fields (#415)
This PR adds the `--fields` flag to `tea pr ls` (#342), and exposes more fields specific to the `PullRequest` type:
```
--fields value, -f value Comma-separated list of fields to print.
Available values:
index,state,author,author-id,url,title,body,mergeable,base,base-commit,head,diff,patch,created,updated,deadline,assignees,milestone,labels,comments
(default: "index,title,state,author,milestone,updated,labels")
```
Co-authored-by: justusbunsi <61625851+justusbunsi@users.noreply.github.com>
Co-authored-by: Norwin <git@nroo.de>
Reviewed-on: https://gitea.com/gitea/tea/pulls/415
Reviewed-by: Norwin <noerw@noreply.gitea.io>
Reviewed-by: techknowlogick <techknowlogick@gitea.io>
Reviewed-by: 6543 <6543@obermui.de>
Co-authored-by: justusbunsi <justusbunsi@noreply.gitea.io>
Co-committed-by: justusbunsi <justusbunsi@noreply.gitea.io>
2021-09-28 20:36:33 +00:00
|
|
|
row[i] = v.FormatField(f, machineReadable)
|
2020-12-21 15:41:07 +00:00
|
|
|
}
|
|
|
|
t.addRowSlice(row)
|
|
|
|
}
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
2020-12-09 22:04:36 +00:00
|
|
|
func tableWithHeader(header ...string) table {
|
|
|
|
return table{headers: header}
|
|
|
|
}
|
|
|
|
|
|
|
|
// it's the callers responsibility to ensure row length is equal to header length!
|
|
|
|
func (t *table) addRow(row ...string) {
|
|
|
|
t.addRowSlice(row)
|
|
|
|
}
|
|
|
|
|
|
|
|
// it's the callers responsibility to ensure row length is equal to header length!
|
|
|
|
func (t *table) addRowSlice(row []string) {
|
|
|
|
t.values = append(t.values, row)
|
|
|
|
}
|
2018-09-03 06:43:00 +00:00
|
|
|
|
2020-12-09 22:04:36 +00:00
|
|
|
func (t *table) sort(column uint, desc bool) {
|
|
|
|
t.sortColumn = column
|
|
|
|
t.sortDesc = desc
|
|
|
|
sort.Stable(t) // stable to allow multiple calls to sort
|
|
|
|
}
|
|
|
|
|
|
|
|
// sortable interface
|
|
|
|
func (t table) Len() int { return len(t.values) }
|
|
|
|
func (t table) Swap(i, j int) { t.values[i], t.values[j] = t.values[j], t.values[i] }
|
|
|
|
func (t table) Less(i, j int) bool {
|
|
|
|
const column = 0
|
|
|
|
if t.sortDesc {
|
|
|
|
i, j = j, i
|
|
|
|
}
|
|
|
|
return t.values[i][t.sortColumn] < t.values[j][t.sortColumn]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *table) print(output string) {
|
2020-12-21 15:41:07 +00:00
|
|
|
switch output {
|
|
|
|
case "", "table":
|
2020-12-09 22:04:36 +00:00
|
|
|
outputtable(t.headers, t.values)
|
2020-12-21 15:41:07 +00:00
|
|
|
case "csv":
|
2020-12-09 22:04:36 +00:00
|
|
|
outputdsv(t.headers, t.values, ",")
|
2020-12-21 15:41:07 +00:00
|
|
|
case "simple":
|
2020-12-09 22:04:36 +00:00
|
|
|
outputsimple(t.headers, t.values)
|
2020-12-21 15:41:07 +00:00
|
|
|
case "tsv":
|
2020-12-09 22:04:36 +00:00
|
|
|
outputdsv(t.headers, t.values, "\t")
|
2020-12-21 15:41:07 +00:00
|
|
|
case "yml", "yaml":
|
2020-12-09 22:04:36 +00:00
|
|
|
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")
|
|
|
|
}
|
2018-09-03 06:43:00 +00:00
|
|
|
}
|
2019-09-15 08:38:18 +00:00
|
|
|
|
|
|
|
// outputtable prints structured data as table
|
|
|
|
func outputtable(headers []string, values [][]string) {
|
|
|
|
table := tablewriter.NewWriter(os.Stdout)
|
|
|
|
if len(headers) > 0 {
|
|
|
|
table.SetHeader(headers)
|
|
|
|
}
|
|
|
|
for _, value := range values {
|
|
|
|
table.Append(value)
|
|
|
|
}
|
|
|
|
table.Render()
|
|
|
|
}
|
|
|
|
|
|
|
|
// outputsimple prints structured data as space delimited value
|
|
|
|
func outputsimple(headers []string, values [][]string) {
|
|
|
|
for _, value := range values {
|
|
|
|
fmt.Printf(strings.Join(value, " "))
|
|
|
|
fmt.Printf("\n")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// outputdsv prints structured data as delimiter separated value format
|
|
|
|
func outputdsv(headers []string, values [][]string, delimiterOpt ...string) {
|
|
|
|
delimiter := ","
|
|
|
|
if len(delimiterOpt) > 0 {
|
|
|
|
delimiter = delimiterOpt[0]
|
|
|
|
}
|
|
|
|
fmt.Println("\"" + strings.Join(headers, "\""+delimiter+"\"") + "\"")
|
|
|
|
for _, value := range values {
|
|
|
|
fmt.Printf("\"")
|
|
|
|
fmt.Printf(strings.Join(value, "\""+delimiter+"\""))
|
|
|
|
fmt.Printf("\"")
|
|
|
|
fmt.Printf("\n")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// outputyaml prints structured data as yaml
|
|
|
|
func outputyaml(headers []string, values [][]string) {
|
|
|
|
for _, value := range values {
|
|
|
|
fmt.Println("-")
|
|
|
|
for j, val := range value {
|
|
|
|
intVal, _ := strconv.Atoi(val)
|
|
|
|
if strconv.Itoa(intVal) == val {
|
|
|
|
fmt.Printf(" %s: %s\n", headers[j], val)
|
|
|
|
} else {
|
|
|
|
fmt.Printf(" %s: '%s'\n", headers[j], val)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-12-21 15:41:07 +00:00
|
|
|
|
|
|
|
func isMachineReadable(outputFormat string) bool {
|
|
|
|
switch outputFormat {
|
2022-03-13 04:00:21 +00:00
|
|
|
case "yml", "yaml", "csv", "tsv":
|
2020-12-21 15:41:07 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|