Skip to content
This repository has been archived by the owner on Nov 22, 2022. It is now read-only.

enhance list table display #261

Merged
merged 6 commits into from
Oct 12, 2020
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
59 changes: 26 additions & 33 deletions commands/issue/issueutils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,50 +5,43 @@ import (
"strings"

"github.com/profclems/glab/internal/utils"
"github.com/profclems/glab/pkg/tableprinter"

"github.com/gosuri/uitable"
"github.com/xanzy/go-gitlab"
)

func DisplayAllIssues(m []*gitlab.Issue, projectID string) *uitable.Table {
return utils.DisplayList(utils.ListInfo{
Name: "issues",
Columns: []string{"IssueID", "Title", "Labels", "CreatedAt"},
Total: len(m),
GetCellValue: func(ri int, ci int) interface{} {
issue := m[ri]
switch ci {
case 0:
if issue.State == "opened" {
return utils.Green(fmt.Sprintf("#%d", issue.IID))
} else {
return utils.Red(fmt.Sprintf("#%d", issue.IID))
}
case 1:
return issue.Title
case 2:
if len(issue.Labels) > 0 {
return fmt.Sprintf("(%s)", utils.Cyan(strings.Trim(strings.Join(issue.Labels, ", "), ",")))
}
return ""
case 3:
return utils.Gray(utils.TimeToPrettyTimeAgo(*issue.CreatedAt))
default:
return ""
}
},
}, projectID)
func DisplayIssueList(issues []*gitlab.Issue, projectID string) string {
table := tableprinter.NewTablePrinter()
for _, issue := range issues {
table.AddCell(IssueState(issue))
table.AddCell(issue.Title)

if len(issue.Labels) > 0 {
table.AddCellf("(%s)", utils.Cyan(strings.Trim(strings.Join(issue.Labels, ", "), ",")))
} else {
table.AddCell("")
}

table.AddCell(utils.Gray(utils.TimeToPrettyTimeAgo(*issue.CreatedAt)))
table.EndRow()
}

return table.Render()
}

func DisplayIssue(i *gitlab.Issue) string {
duration := utils.TimeToPrettyTimeAgo(*i.CreatedAt)
var issueID string
issueID := IssueState(i)

return fmt.Sprintf("%s %s (%s)\n %s\n",
issueID, i.Title, duration, i.WebURL)
}

func IssueState(i *gitlab.Issue) (issueID string) {
if i.State == "opened" {
issueID = utils.Green(fmt.Sprintf("#%d", i.IID))
} else {
issueID = utils.Red(fmt.Sprintf("#%d", i.IID))
}

return fmt.Sprintf("%s %s (%s)\n %s\n",
issueID, i.Title, duration, i.WebURL)
return
}
29 changes: 25 additions & 4 deletions commands/issue/list/issue_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ func NewCmdList(f *cmdutils.Factory) *cobra.Command {
Args: cobra.ExactArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
var (
state string
err error
state string
err error
listType string
titleQualifier string
)

apiClient, err := f.HttpClient()
Expand All @@ -39,13 +41,18 @@ func NewCmdList(f *cmdutils.Factory) *cobra.Command {
state = "all"
} else if lb, _ := cmd.Flags().GetBool("closed"); lb {
state = "closed"
titleQualifier = "closed"
} else {
state = "opened"
titleQualifier = "open"
}

opts := &gitlab.ListProjectIssuesOptions{
State: gitlab.String(state),
}
opts.Page = 1
opts.PerPage = 30

if lb, _ := cmd.Flags().GetString("assignee"); lb != "" {
opts.AssigneeUsername = gitlab.String(lb)
}
Expand All @@ -54,29 +61,43 @@ func NewCmdList(f *cmdutils.Factory) *cobra.Command {
lb,
}
opts.Labels = label
listType = "search"
}
if lb, _ := cmd.Flags().GetString("milestone"); lb != "" {
opts.Milestone = gitlab.String(lb)
listType = "search"
}
if lb, _ := cmd.Flags().GetBool("confidential"); lb {
opts.Confidential = gitlab.Bool(lb)
listType = "search"
}
if p, _ := cmd.Flags().GetInt("page"); p != 0 {
opts.Page = p
listType = "search"
}
if p, _ := cmd.Flags().GetInt("per-page"); p != 0 {
opts.PerPage = p
listType = "search"
}

if lb, _ := cmd.Flags().GetBool("mine"); lb {
u, _ := api.CurrentUser(nil)
opts.AssigneeUsername = gitlab.String(u.Username)
listType = "search"
}
issues, err := api.ListIssues(apiClient, repo.FullName(), opts)
if err != nil {
return err
}
fmt.Fprintln(utils.ColorableOut(cmd), issueutils.DisplayAllIssues(issues, repo.FullName()))

title := utils.NewListTitle(titleQualifier + " issue")
title.RepoName = repo.FullName()
title.Page = opts.Page
title.ListActionType = listType
title.CurrentPageTotal = len(issues)

fmt.Fprintf(utils.ColorableOut(cmd), "%s\n%s\n", title.Describe(), issueutils.DisplayIssueList(issues, repo.FullName()))

return nil

},
Expand All @@ -90,7 +111,7 @@ func NewCmdList(f *cmdutils.Factory) *cobra.Command {
issueListCmd.Flags().BoolP("opened", "o", false, "Get only opened issues")
issueListCmd.Flags().BoolP("confidential", "", false, "Filter by confidential issues")
issueListCmd.Flags().IntP("page", "p", 1, "Page number")
issueListCmd.Flags().IntP("per-page", "P", 20, "Number of items to list per page")
issueListCmd.Flags().IntP("per-page", "P", 30, "Number of items to list per page. (default 30)")

return issueListCmd
}
24 changes: 12 additions & 12 deletions commands/label/list/label_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package list

import (
"fmt"
"strings"

"github.com/profclems/glab/pkg/api"

Expand Down Expand Up @@ -34,9 +33,10 @@ func NewCmdList(f *cmdutils.Factory) *cobra.Command {
return err
}

cfg, _ := f.Config()

l := &gitlab.ListLabelsOptions{}

l.WithCounts = gitlab.Bool(true)

if p, _ := cmd.Flags().GetInt("page"); p != 0 {
l.Page = p
}
Expand All @@ -61,22 +61,22 @@ func NewCmdList(f *cmdutils.Factory) *cobra.Command {
fmt.Fprintln(out, utils.Indent(labelPrintInfo, " "))

// Cache labels for host
labelNames := make([]string, 0, len(labels))
for _, label := range labels {
labelNames = append(labelNames, label.Name)
}
labelsEntry := strings.Join(labelNames, ",")
if err := cfg.Set(repo.RepoHost(), "project_labels", labelsEntry); err != nil {
_ = cfg.Write()
}
//labelNames := make([]string, 0, len(labels))
//for _, label := range labels {
// labelNames = append(labelNames, label.Name)
//}
//labelsEntry := strings.Join(labelNames, ",")
//if err := cfg.Set(repo.RepoHost(), "project_labels", labelsEntry); err != nil {
// _ = cfg.Write()
//}

return nil

},
}

labelListCmd.Flags().IntP("page", "p", 1, "Page number")
labelListCmd.Flags().IntP("per-page", "P", 20, "Number of items to list per page")
labelListCmd.Flags().IntP("per-page", "P", 30, "Number of items to list per page")

return labelListCmd
}
9 changes: 8 additions & 1 deletion commands/mr/issues/mr_issues.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,14 @@ func NewCmdIssues(f *cmdutils.Factory) *cobra.Command {
if err != nil {
return err
}
fmt.Fprintln(out, issueutils.DisplayAllIssues(mrIssues, repo.FullName()))

title := utils.NewListTitle("issue")
title.RepoName = repo.FullName()
title.Page = 0
title.ListActionType = "search"
title.CurrentPageTotal = len(mrIssues)

fmt.Fprintf(out, "%s\n%s\n", title.Describe(), issueutils.DisplayIssueList(mrIssues, repo.FullName()))
return nil
},
}
Expand Down
24 changes: 21 additions & 3 deletions commands/mr/list/mr_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ func NewCmdList(f *cmdutils.Factory) *cobra.Command {
RunE: func(cmd *cobra.Command, args []string) error {
var state string
var err error
var listType string
var titleQualifier string

out := utils.ColorableOut(cmd)

apiClient, err := f.HttpClient()
Expand All @@ -38,23 +41,31 @@ func NewCmdList(f *cmdutils.Factory) *cobra.Command {
state = "all"
} else if lb, _ := cmd.Flags().GetBool("closed"); lb {
state = "closed"
titleQualifier = state
} else if lb, _ := cmd.Flags().GetBool("merged"); lb {
state = "merged"
titleQualifier = state
} else {
state = "opened"
titleQualifier = "open"
}

l := &gitlab.ListProjectMergeRequestsOptions{
State: gitlab.String(state),
}
l.Page = 1
l.PerPage = 30

if lb, _ := cmd.Flags().GetString("label"); lb != "" {
label := gitlab.Labels{
lb,
}
l.Labels = label
listType = "search"
}
if lb, _ := cmd.Flags().GetString("milestone"); lb != "" {
l.Milestone = gitlab.String(lb)
listType = "search"
}
if p, _ := cmd.Flags().GetInt("page"); p != 0 {
l.Page = p
Expand All @@ -65,6 +76,7 @@ func NewCmdList(f *cmdutils.Factory) *cobra.Command {

if mine, _ := cmd.Flags().GetBool("mine"); mine {
l.Scope = gitlab.String("assigned_to_me")
listType = "search"
}

assigneeIds := make([]int, 0)
Expand Down Expand Up @@ -92,7 +104,13 @@ func NewCmdList(f *cmdutils.Factory) *cobra.Command {
return err
}

fmt.Fprintln(out, mrutils.DisplayAllMRs(mergeRequests, repo.FullName()))
title := utils.NewListTitle(titleQualifier + " merge request")
title.RepoName = repo.FullName()
title.Page = l.Page
title.ListActionType = listType
title.CurrentPageTotal = len(mergeRequests)

fmt.Fprintf(out, "%s\n%s\n", title.Describe(), mrutils.DisplayAllMRs(mergeRequests, repo.FullName()))
return nil
},
}
Expand All @@ -101,10 +119,10 @@ func NewCmdList(f *cmdutils.Factory) *cobra.Command {
mrListCmd.Flags().StringP("milestone", "", "", "Filter merge request by milestone <id>")
mrListCmd.Flags().BoolP("all", "a", false, "Get all merge requests")
mrListCmd.Flags().BoolP("closed", "c", false, "Get only closed merge requests")
mrListCmd.Flags().BoolP("opened", "o", false, "Get only opened merge requests")
mrListCmd.Flags().BoolP("opened", "o", false, "Get only open merge requests")
mrListCmd.Flags().BoolP("merged", "m", false, "Get only merged merge requests")
mrListCmd.Flags().IntP("page", "p", 1, "Page number")
mrListCmd.Flags().IntP("per-page", "P", 20, "Number of items to list per page")
mrListCmd.Flags().IntP("per-page", "P", 30, "Number of items to list per page. (default 30)")
mrListCmd.Flags().BoolP("mine", "", false, "Get only merge requests assigned to me")
mrListCmd.Flags().StringSliceP("assignee", "", []string{}, "Get only merge requests assigned to users")

Expand Down
52 changes: 21 additions & 31 deletions commands/mr/mrutils/mrutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,45 +3,35 @@ package mrutils
import (
"fmt"

"github.com/gosuri/uitable"
"github.com/profclems/glab/internal/utils"
"github.com/profclems/glab/pkg/tableprinter"
"github.com/xanzy/go-gitlab"
)

func DisplayMR(mr *gitlab.MergeRequest) string {
var mrID string
mrID := MRState(mr)
return fmt.Sprintf("%s %s (%s)\n %s\n",
mrID, mr.Title, mr.SourceBranch, mr.WebURL)
}

if mr.State == "opened" {
mrID = utils.Green(fmt.Sprintf("!%d", mr.IID))
func MRState(m *gitlab.MergeRequest) string {
if m.State == "opened" {
return utils.Green(fmt.Sprintf("!%d", m.IID))
} else if m.State == "merged" {
return utils.Blue(fmt.Sprintf("!%d", m.IID))
} else {
mrID = utils.Red(fmt.Sprintf("!%d", mr.IID))
return utils.Red(fmt.Sprintf("!%d", m.IID))
}

return fmt.Sprintf("%s %s (%s)\n %s\n",
mrID, mr.Title, mr.SourceBranch, mr.WebURL)
}

func DisplayAllMRs(m []*gitlab.MergeRequest, projectID string) *uitable.Table {
return utils.DisplayList(utils.ListInfo{
Name: "Merge Requests",
Columns: []string{"ID", "Title", "Branch"},
Total: len(m),
GetCellValue: func(ri int, ci int) interface{} {
mr := m[ri]
switch ci {
case 0:
if mr.State == "opened" {
return utils.Green(fmt.Sprintf("!%d", mr.IID))
} else {
return utils.Red(fmt.Sprintf("!%d", mr.IID))
}
case 1:
return mr.Title
case 2:
return utils.Cyan(fmt.Sprintf("(%s) ← (%s)", mr.TargetBranch, mr.SourceBranch))
default:
return ""
}
},
}, projectID)
func DisplayAllMRs(mrs []*gitlab.MergeRequest, projectID string) string {
table := tableprinter.NewTablePrinter()
for _, m := range mrs {
table.AddCell(MRState(m))
table.AddCell(m.Title)
table.AddCell(utils.Cyan(fmt.Sprintf("(%s) ← (%s)", m.TargetBranch, m.SourceBranch)))
table.EndRow()
}

return table.Render()
}
Loading