Skip to content

Commit

Permalink
Extract shared comment and reaction group code
Browse files Browse the repository at this point in the history
  • Loading branch information
samcoe committed Dec 9, 2020
1 parent 9f101ff commit dee7077
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 245 deletions.
120 changes: 4 additions & 116 deletions pkg/cmd/issue/view/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,8 @@ func viewRun(opts *ViewOptions) error {
}

if opts.Comments {
return printRawIssueComments(opts.IO.Out, issue)
fmt.Fprint(opts.IO.Out, prShared.RawCommentList(issue.Comments))
return nil
}

return printRawIssuePreview(opts.IO.Out, issue)
Expand All @@ -149,26 +150,6 @@ func printRawIssuePreview(out io.Writer, issue *api.Issue) error {
return nil
}

func printRawIssueComments(out io.Writer, issue *api.Issue) error {
var b strings.Builder
for _, comment := range issue.Comments.Nodes {
fmt.Fprint(&b, formatRawIssueComment(comment))
}
fmt.Fprint(out, b.String())
return nil
}

func formatRawIssueComment(comment api.Comment) string {
var b strings.Builder
fmt.Fprintf(&b, "author:\t%s\n", comment.Author.Login)
fmt.Fprintf(&b, "association:\t%s\n", strings.ToLower(comment.AuthorAssociation))
fmt.Fprintf(&b, "edited:\t%t\n", comment.IncludesCreatedEdit)
fmt.Fprintln(&b, "--")
fmt.Fprintln(&b, comment.Body)
fmt.Fprintln(&b, "--")
return b.String()
}

func printHumanIssuePreview(io *iostreams.IOStreams, issue *api.Issue) error {
out := io.Out
now := time.Now()
Expand All @@ -186,7 +167,7 @@ func printHumanIssuePreview(io *iostreams.IOStreams, issue *api.Issue) error {
)

// Reactions
if reactions := reactionGroupList(issue.ReactionGroups); reactions != "" {
if reactions := prShared.ReactionGroupList(issue.ReactionGroups); reactions != "" {
fmt.Fprint(out, reactions)
fmt.Fprintln(out)
}
Expand Down Expand Up @@ -224,7 +205,7 @@ func printHumanIssuePreview(io *iostreams.IOStreams, issue *api.Issue) error {

// Comments
if issue.Comments.TotalCount > 0 {
comments, err := issueCommentList(io, issue.Comments)
comments, err := prShared.CommentList(io, issue.Comments)
if err != nil {
return err
}
Expand All @@ -237,75 +218,6 @@ func printHumanIssuePreview(io *iostreams.IOStreams, issue *api.Issue) error {
return nil
}

func issueCommentList(io *iostreams.IOStreams, comments api.Comments) (string, error) {
var b strings.Builder
cs := io.ColorScheme()
retrievedCount := len(comments.Nodes)
hiddenCount := comments.TotalCount - retrievedCount

if hiddenCount > 0 {
fmt.Fprint(&b, cs.Gray(fmt.Sprintf("———————— Not showing %s ————————", utils.Pluralize(hiddenCount, "comment"))))
fmt.Fprintf(&b, "\n\n\n")
}

for i, comment := range comments.Nodes {
last := i+1 == retrievedCount
cmt, err := formatIssueComment(io, comment, last)
if err != nil {
return "", err
}
fmt.Fprint(&b, cmt)
if last {
fmt.Fprintln(&b)
}
}

if hiddenCount > 0 {
fmt.Fprint(&b, cs.Gray("Use --comments to view the full conversation"))
fmt.Fprintln(&b)
}

return b.String(), nil
}

func formatIssueComment(io *iostreams.IOStreams, comment api.Comment, newest bool) (string, error) {
var b strings.Builder
cs := io.ColorScheme()

// Header
fmt.Fprint(&b, cs.Bold(comment.Author.Login))
if comment.AuthorAssociation != "NONE" {
fmt.Fprint(&b, cs.Bold(fmt.Sprintf(" (%s)", strings.ToLower(comment.AuthorAssociation))))
}
fmt.Fprint(&b, cs.Bold(fmt.Sprintf(" • %s", utils.FuzzyAgoAbbr(time.Now(), comment.CreatedAt))))
if comment.IncludesCreatedEdit {
fmt.Fprint(&b, cs.Bold(" • edited"))
}
if newest {
fmt.Fprint(&b, cs.Bold(" • "))
fmt.Fprint(&b, cs.CyanBold("Newest comment"))
}
fmt.Fprintln(&b)

// Reactions
if reactions := reactionGroupList(comment.ReactionGroups); reactions != "" {
fmt.Fprint(&b, reactions)
fmt.Fprintln(&b)
}

// Body
if comment.Body != "" {
style := markdown.GetStyle(io.TerminalTheme())
md, err := markdown.Render(comment.Body, style, "")
if err != nil {
return "", err
}
fmt.Fprint(&b, md)
}

return b.String(), nil
}

func issueStateTitleWithColor(cs *iostreams.ColorScheme, state string) string {
colorFunc := cs.ColorFromString(prShared.ColorForState(state))
return colorFunc(strings.Title(strings.ToLower(state)))
Expand Down Expand Up @@ -348,27 +260,3 @@ func issueProjectList(issue api.Issue) string {
}
return list
}

func reactionGroupList(rgs api.ReactionGroups) string {
var rs []string

for _, rg := range rgs {
if r := formatReactionGroup(rg); r != "" {
rs = append(rs, r)
}
}

return strings.Join(rs, " • ")
}

func formatReactionGroup(rg api.ReactionGroup) string {
c := rg.Count()
if c == 0 {
return ""
}
e := rg.Emoji()
if e == "" {
return ""
}
return fmt.Sprintf("%v %s", c, e)
}
100 changes: 100 additions & 0 deletions pkg/cmd/pr/shared/comments.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package shared

import (
"fmt"
"strings"
"time"

"github.com/cli/cli/api"
"github.com/cli/cli/pkg/iostreams"
"github.com/cli/cli/pkg/markdown"
"github.com/cli/cli/utils"
)

func RawCommentList(comments api.Comments) string {
var b strings.Builder
for _, comment := range comments.Nodes {
fmt.Fprint(&b, formatRawComment(comment))
}
return b.String()
}

func formatRawComment(comment api.Comment) string {
var b strings.Builder
fmt.Fprintf(&b, "author:\t%s\n", comment.Author.Login)
fmt.Fprintf(&b, "association:\t%s\n", strings.ToLower(comment.AuthorAssociation))
fmt.Fprintf(&b, "edited:\t%t\n", comment.IncludesCreatedEdit)
fmt.Fprintln(&b, "--")
fmt.Fprintln(&b, comment.Body)
fmt.Fprintln(&b, "--")
return b.String()
}

func CommentList(io *iostreams.IOStreams, comments api.Comments) (string, error) {
var b strings.Builder
cs := io.ColorScheme()
retrievedCount := len(comments.Nodes)
hiddenCount := comments.TotalCount - retrievedCount

if hiddenCount > 0 {
fmt.Fprint(&b, cs.Gray(fmt.Sprintf("———————— Not showing %s ————————", utils.Pluralize(hiddenCount, "comment"))))
fmt.Fprintf(&b, "\n\n\n")
}

for i, comment := range comments.Nodes {
last := i+1 == retrievedCount
cmt, err := formatComment(io, comment, last)
if err != nil {
return "", err
}
fmt.Fprint(&b, cmt)
if last {
fmt.Fprintln(&b)
}
}

if hiddenCount > 0 {
fmt.Fprint(&b, cs.Gray("Use --comments to view the full conversation"))
fmt.Fprintln(&b)
}

return b.String(), nil
}

func formatComment(io *iostreams.IOStreams, comment api.Comment, newest bool) (string, error) {
var b strings.Builder
cs := io.ColorScheme()

// Header
fmt.Fprint(&b, cs.Bold(comment.Author.Login))
if comment.AuthorAssociation != "NONE" {
fmt.Fprint(&b, cs.Bold(fmt.Sprintf(" (%s)", strings.ToLower(comment.AuthorAssociation))))
}
fmt.Fprint(&b, cs.Bold(fmt.Sprintf(" • %s", utils.FuzzyAgoAbbr(time.Now(), comment.CreatedAt))))
if comment.IncludesCreatedEdit {
fmt.Fprint(&b, cs.Bold(" • edited"))
}
if newest {
fmt.Fprint(&b, cs.Bold(" • "))
fmt.Fprint(&b, cs.CyanBold("Newest comment"))
}
fmt.Fprintln(&b)

// Reactions
if reactions := ReactionGroupList(comment.ReactionGroups); reactions != "" {
fmt.Fprint(&b, reactions)
fmt.Fprintln(&b)
}

// Body
if comment.Body != "" {
style := markdown.GetStyle(io.TerminalTheme())
md, err := markdown.Render(comment.Body, style, "")
if err != nil {
return "", err
}
fmt.Fprint(&b, md)
}

return b.String(), nil
}
32 changes: 32 additions & 0 deletions pkg/cmd/pr/shared/reaction_groups.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package shared

import (
"fmt"
"strings"

"github.com/cli/cli/api"
)

func ReactionGroupList(rgs api.ReactionGroups) string {
var rs []string

for _, rg := range rgs {
if r := formatReactionGroup(rg); r != "" {
rs = append(rs, r)
}
}

return strings.Join(rs, " • ")
}

func formatReactionGroup(rg api.ReactionGroup) string {
c := rg.Count()
if c == 0 {
return ""
}
e := rg.Emoji()
if e == "" {
return ""
}
return fmt.Sprintf("%v %s", c, e)
}
Loading

0 comments on commit dee7077

Please sign in to comment.