Add --fields to notification & milestone listings #422

Merged
6543 merged 22 commits from noerw/tea:fields-flag into main 2022-09-13 19:08:19 +00:00
2 changed files with 68 additions and 40 deletions
Showing only changes of commit fcbfdaeb97 - Show all commits

View File

@ -15,6 +15,10 @@ import (
"github.com/urfave/cli/v2"
)
var notifFieldsFlag = flags.FieldsFlag(print.NotificationFields, []string{
noerw marked this conversation as resolved Outdated
Outdated
Review

typo?

typo?
"id", "status", "index", "type", "state", "title",
})
var notifTypeFlag = flags.NewCsvFlag("types", "subject types to filter by", []string{"t"},
[]string{"issue", "pull", "repository", "commit"}, nil)
@ -25,7 +29,10 @@ var CmdNotificationsList = cli.Command{
Usage: "List notifications",
Description: `List notifications`,
Action: RunNotificationsList,
Flags: append([]cli.Flag{notifTypeFlag}, flags.NotificationFlags...),
Flags: append([]cli.Flag{
notifFieldsFlag,
notifTypeFlag,
}, flags.NotificationFlags...),
}
// RunNotificationsList list notifications
@ -66,7 +73,17 @@ func listNotifications(cmd *cli.Context, status []gitea.NotifyStatus, subjects [
listOpts.Page = 1
}
fields, err := notifFieldsFlag.GetValues(cmd)
if err != nil {
return err
}
if all {
// add repository to the default fields
if !notifFieldsFlag.IsSet() {
fields = append(fields, "repository")
}
news, _, err = client.ListNotifications(gitea.ListNotificationOptions{
ListOptions: listOpts,
Status: status,
@ -84,6 +101,6 @@ func listNotifications(cmd *cli.Context, status []gitea.NotifyStatus, subjects [
log.Fatal(err)
}
print.NotificationsList(news, ctx.Output, all)
print.NotificationsList(news, ctx.Output, fields)
return nil
}

View File

@ -12,26 +12,47 @@ import (
)
// NotificationsList prints a listing of notification threads
func NotificationsList(news []*gitea.NotificationThread, output string, showRepository bool) {
headers := []string{
"ID",
"Status",
"Type",
"State",
"Index",
"Title",
}
if showRepository {
headers = append(headers, "Repository")
func NotificationsList(news []*gitea.NotificationThread, output string, fields []string) {
var printables = make([]printable, len(news))
for i, x := range news {
printables[i] = &printableNotification{x}
}
t := tableFromItems(fields, printables)
t.print(output)
}
t := table{headers: headers}
// NotificationFields are all available fields to print with NotificationsList
var NotificationFields = []string{
"id",
"status",
for _, n := range news {
if n.Subject == nil {
continue
// these are about the notification subject
"index",
"type",
"state",
"title",
"repository",
}
type printableNotification struct {
*gitea.NotificationThread
noerw marked this conversation as resolved Outdated
}
func (n printableNotification) FormatField(field string) string {
switch field {
case "id":
return fmt.Sprintf("%d", n.ID)
case "status":
status := "read"
if n.Pinned {
status = "pinned"
} else if n.Unread {
status = "unread"
}
// if pull or Issue get Index
return status
case "index":
var index string
if n.Subject.Type == "Issue" || n.Subject.Type == "Pull" {
index = n.Subject.URL
@ -39,31 +60,21 @@ func NotificationsList(news []*gitea.NotificationThread, output string, showRepo
if len(urlParts) != 0 {
index = urlParts[len(urlParts)-1]
}
index = "#" + index
}
return index
status := "read"
if n.Pinned {
status = "pinned"
} else if n.Unread {
status = "unread"
}
case "type":
return string(n.Subject.Type)
item := []string{
fmt.Sprint(n.ID),
status,
string(n.Subject.Type),
string(n.Subject.State),
index,
n.Subject.Title,
}
if showRepository {
item = append(item, n.Repository.FullName)
}
t.addRowSlice(item)
case "state":
return string(n.Subject.State)
case "title":
return n.Subject.Title
case "repo", "repository":
return n.Repository.FullName
}
if t.Len() != 0 {
t.print(output)
}
return ""
}