forked from cli/cli
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract shared comment and reaction group code
- Loading branch information
Showing
4 changed files
with
142 additions
and
245 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
Oops, something went wrong.