Update non-tty comment output
This commit is contained in:
parent
8c5e5a3820
commit
bad5a59427
3 changed files with 28 additions and 27 deletions
|
|
@ -85,7 +85,7 @@
|
|||
"authorAssociation": "NONE",
|
||||
"body": "Comment 1",
|
||||
"createdAt": "2020-01-01T12:00:00Z",
|
||||
"includesCreatedEdit": false,
|
||||
"includesCreatedEdit": true,
|
||||
"reactionGroups": [
|
||||
{
|
||||
"content": "CONFUSED",
|
||||
|
|
|
|||
|
|
@ -27,9 +27,10 @@ type ViewOptions struct {
|
|||
IO *iostreams.IOStreams
|
||||
BaseRepo func() (ghrepo.Interface, error)
|
||||
|
||||
SelectorArg string
|
||||
WebMode bool
|
||||
Comments int
|
||||
SelectorArg string
|
||||
WebMode bool
|
||||
Comments int
|
||||
CommentsProvided bool
|
||||
}
|
||||
|
||||
func NewCmdView(f *cmdutil.Factory, runF func(*ViewOptions) error) *cobra.Command {
|
||||
|
|
@ -54,6 +55,8 @@ func NewCmdView(f *cmdutil.Factory, runF func(*ViewOptions) error) *cobra.Comman
|
|||
// support `-R, --repo` override
|
||||
opts.BaseRepo = f.BaseRepo
|
||||
|
||||
opts.CommentsProvided = cmd.Flags().Changed("comments")
|
||||
|
||||
if len(args) > 0 {
|
||||
opts.SelectorArg = args[0]
|
||||
}
|
||||
|
|
@ -66,7 +69,7 @@ func NewCmdView(f *cmdutil.Factory, runF func(*ViewOptions) error) *cobra.Comman
|
|||
}
|
||||
|
||||
cmd.Flags().BoolVarP(&opts.WebMode, "web", "w", false, "Open an issue in the browser")
|
||||
cmd.Flags().IntVarP(&opts.Comments, "comments", "c", 1, "View issue comments")
|
||||
cmd.Flags().IntVarP(&opts.Comments, "comments", "c", 1, "View issue comments sorted by most recent")
|
||||
cmd.Flags().Lookup("comments").NoOptDefVal = "30"
|
||||
|
||||
return cmd
|
||||
|
|
@ -104,6 +107,11 @@ func viewRun(opts *ViewOptions) error {
|
|||
if opts.IO.IsStdoutTTY() {
|
||||
return printHumanIssuePreview(opts.IO, issue)
|
||||
}
|
||||
|
||||
if opts.CommentsProvided {
|
||||
return printRawIssueComments(opts.IO.Out, issue)
|
||||
}
|
||||
|
||||
return printRawIssuePreview(opts.IO.Out, issue)
|
||||
}
|
||||
|
||||
|
|
@ -122,30 +130,25 @@ func printRawIssuePreview(out io.Writer, issue *api.Issue) error {
|
|||
fmt.Fprintf(out, "assignees:\t%s\n", assignees)
|
||||
fmt.Fprintf(out, "projects:\t%s\n", projects)
|
||||
fmt.Fprintf(out, "milestone:\t%s\n", issue.Milestone.Title)
|
||||
|
||||
fmt.Fprintln(out, "--")
|
||||
fmt.Fprintln(out, issue.Body)
|
||||
fmt.Fprintln(out, "--")
|
||||
|
||||
if len(issue.Comments.Nodes) > 0 {
|
||||
fmt.Fprint(out, rawIssueComments(issue.Comments))
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func rawIssueComments(comments api.IssueComments) string {
|
||||
func printRawIssueComments(out io.Writer, issue *api.Issue) error {
|
||||
var b strings.Builder
|
||||
for _, comment := range comments.Nodes {
|
||||
for _, comment := range issue.Comments.Nodes {
|
||||
fmt.Fprint(&b, rawIssueComment(comment))
|
||||
}
|
||||
return b.String()
|
||||
fmt.Fprint(out, b.String())
|
||||
return nil
|
||||
}
|
||||
|
||||
func rawIssueComment(comment api.IssueComment) 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, "--")
|
||||
|
|
|
|||
|
|
@ -360,7 +360,7 @@ func TestIssueView_tty_Comments(t *testing.T) {
|
|||
expectedOutputs: []string{
|
||||
`some title`,
|
||||
`some body`,
|
||||
`monalisa • Jan 1, 2020`,
|
||||
`monalisa • Jan 1, 2020 • edited`,
|
||||
`1 \x{1f615} • 2 \x{1f440} • 3 \x{2764}\x{fe0f} • 4 \x{1f389} • 5 \x{1f604} • 6 \x{1f680} • 7 \x{1f44e} • 8 \x{1f44d}`,
|
||||
`Comment 1`,
|
||||
`johnnytest \(contributor\) • Jan 1, 2020`,
|
||||
|
|
@ -428,36 +428,35 @@ func TestIssueView_nontty_Comments(t *testing.T) {
|
|||
fixture: "./fixtures/issueView_previewSingleComment.json",
|
||||
expectedOutputs: []string{
|
||||
`title:\tsome title`,
|
||||
`state:\tOPEN`,
|
||||
`author:\tmarseilles`,
|
||||
`comments:\t5`,
|
||||
`some body`,
|
||||
`author:\tmarseilles`,
|
||||
`association:\tcollaborator`,
|
||||
`Comment 5`,
|
||||
},
|
||||
},
|
||||
"with default comments flag": {
|
||||
cli: "123 --comments",
|
||||
fixture: "./fixtures/issueView_previewFullComments.json",
|
||||
expectedOutputs: []string{
|
||||
`title:\tsome title`,
|
||||
`author:\tmarseilles`,
|
||||
`comments:\t5`,
|
||||
`some body`,
|
||||
`author:\tmonalisa`,
|
||||
`association:\t`,
|
||||
`edited:\ttrue`,
|
||||
`Comment 1`,
|
||||
`author:\tjohnnytest`,
|
||||
`association:\tcontributor`,
|
||||
`edited:\tfalse`,
|
||||
`Comment 2`,
|
||||
`author:\telvisp`,
|
||||
`association:\tmember`,
|
||||
`edited:\tfalse`,
|
||||
`Comment 3`,
|
||||
`author:\tloislane`,
|
||||
`association:\towner`,
|
||||
`edited:\tfalse`,
|
||||
`Comment 4`,
|
||||
`author:\tmarseilles`,
|
||||
`association:\tcollaborator`,
|
||||
`edited:\tfalse`,
|
||||
`Comment 5`,
|
||||
},
|
||||
},
|
||||
|
|
@ -465,18 +464,17 @@ func TestIssueView_nontty_Comments(t *testing.T) {
|
|||
cli: "123 --comments=3",
|
||||
fixture: "./fixtures/issueView_previewThreeComments.json",
|
||||
expectedOutputs: []string{
|
||||
`title:\tsome title`,
|
||||
`author:\tmarseilles`,
|
||||
`comments:\t5`,
|
||||
`some body`,
|
||||
`author:\telvisp`,
|
||||
`association:\tmember`,
|
||||
`edited:\tfalse`,
|
||||
`Comment 3`,
|
||||
`author:\tloislane`,
|
||||
`association:\towner`,
|
||||
`edited:\tfalse`,
|
||||
`Comment 4`,
|
||||
`author:\tmarseilles`,
|
||||
`association:\tcollaborator`,
|
||||
`edited:\tfalse`,
|
||||
`Comment 5`,
|
||||
},
|
||||
},
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue