Skip to content

Commit 5d77247

Browse files
babakksCopilot
andcommitted
fix(discussion view): print only requested items in non-tty output
Make the non-interactive output of each mode print only what was asked for. Without --comments, only the discussion metadata and body are printed. With --comments, only the comments are printed, each as a metadata block (author, created, url, and answer when applicable) followed by its body. In replies mode, only the replies are printed in the same block format, without the parent comment. Comments and replies follow the same chronological ordering as the interactive output, and pagination cursors are no longer emitted in non-tty mode. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 06d2e34 commit 5d77247

2 files changed

Lines changed: 76 additions & 86 deletions

File tree

pkg/cmd/discussion/view/view.go

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,11 @@ func viewRun(opts *ViewOptions) error {
326326
return printHumanView(opts, discussion)
327327
}
328328

329-
return printRawView(opts.IO.Out, discussion, opts.Comments)
329+
if opts.Comments {
330+
return printRawComments(opts.IO.Out, discussion.Comments)
331+
}
332+
333+
return printRawView(opts.IO.Out, discussion)
330334
}
331335

332336
func printHumanView(opts *ViewOptions, d *client.Discussion) error {
@@ -446,7 +450,7 @@ func printHumanView(opts *ViewOptions, d *client.Discussion) error {
446450
return nil
447451
}
448452

449-
func printRawView(out io.Writer, d *client.Discussion, showComments bool) error {
453+
func printRawView(out io.Writer, d *client.Discussion) error {
450454
fmt.Fprintf(out, "title:\t%s\n", d.Title)
451455
state := "OPEN"
452456
if d.Closed {
@@ -457,18 +461,25 @@ func printRawView(out io.Writer, d *client.Discussion, showComments bool) error
457461
fmt.Fprintf(out, "author:\t%s\n", d.Author.Login)
458462
fmt.Fprintf(out, "labels:\t%s\n", labelList(d.Labels, nil))
459463
fmt.Fprintf(out, "comments:\t%d\n", d.Comments.TotalCount)
460-
if showComments && d.Comments.NextCursor != "" {
461-
fmt.Fprintf(out, "next:\t%s\n", d.Comments.NextCursor)
462-
}
463464
fmt.Fprintf(out, "number:\t%d\n", d.Number)
464465
fmt.Fprintf(out, "url:\t%s\n", d.URL)
465466
fmt.Fprintln(out, "--")
466467
fmt.Fprintln(out, d.Body)
467468

468-
if showComments {
469-
for _, c := range d.Comments.Comments {
470-
printRawComment(out, c, "")
471-
}
469+
return nil
470+
}
471+
472+
// printRawComments writes the comments as a sequence of metadata blocks,
473+
// without any discussion-level fields or nested replies. Comments are
474+
// printed in chronological order regardless of how they were fetched.
475+
func printRawComments(out io.Writer, list client.DiscussionCommentList) error {
476+
comments := slices.Clone(list.Comments)
477+
if list.Direction == client.DiscussionCommentListDirectionBackward {
478+
slices.Reverse(comments)
479+
}
480+
481+
for _, c := range comments {
482+
printRawComment(out, c)
472483
}
473484

474485
return nil
@@ -576,23 +587,16 @@ func printHumanComment(opts *ViewOptions, out io.Writer, c client.DiscussionComm
576587
return nil
577588
}
578589

579-
func printRawComment(out io.Writer, c client.DiscussionComment, indent string) {
580-
answer := ""
590+
func printRawComment(out io.Writer, c client.DiscussionComment) {
591+
fmt.Fprintf(out, "author:\t%s\n", c.Author.Login)
592+
fmt.Fprintf(out, "created:\t%s\n", c.CreatedAt.Format(time.RFC3339))
593+
fmt.Fprintf(out, "url:\t%s\n", c.URL)
581594
if c.IsAnswer {
582-
answer = "\tanswer"
583-
}
584-
fmt.Fprintf(out, "%scomment:\t%s\t%s\t%s%s\n", indent, c.Author.Login, c.CreatedAt.Format(time.RFC3339), c.URL, answer)
585-
fmt.Fprintf(out, "%s--\n", indent)
586-
if indent != "" {
587-
fmt.Fprint(out, text.Indent(c.Body, indent))
588-
} else {
589-
fmt.Fprint(out, c.Body)
590-
}
591-
fmt.Fprintln(out)
592-
593-
for _, reply := range c.Replies.Comments {
594-
printRawComment(out, reply, indent+" ")
595+
fmt.Fprintln(out, "answer:\ttrue")
595596
}
597+
fmt.Fprintln(out, "--")
598+
fmt.Fprintln(out, c.Body)
599+
fmt.Fprintln(out, "--")
596600
}
597601

598602
func labelList(labels []client.DiscussionLabel, cs *iostreams.ColorScheme) string {
@@ -632,21 +636,17 @@ func printHumanCommentAndReplies(opts *ViewOptions, c *client.DiscussionComment)
632636
return nil
633637
}
634638

639+
// printRawReplies writes the replies of a comment as a sequence of metadata
640+
// blocks, without any fields of the parent comment. Replies are printed in
641+
// chronological order regardless of how they were fetched.
635642
func printRawReplies(out io.Writer, c *client.DiscussionComment) error {
636-
answer := ""
637-
if c.IsAnswer {
638-
answer = "\tanswer"
639-
}
640-
fmt.Fprintf(out, "comment:\t%s\t%s\t%s%s\n", c.Author.Login, c.CreatedAt.Format(time.RFC3339), c.URL, answer)
641-
fmt.Fprintf(out, "replies:\t%d\n", c.Replies.TotalCount)
642-
if c.Replies.NextCursor != "" {
643-
fmt.Fprintf(out, "next:\t%s\n", c.Replies.NextCursor)
643+
replies := slices.Clone(c.Replies.Comments)
644+
if c.Replies.Direction == client.DiscussionCommentListDirectionBackward {
645+
slices.Reverse(replies)
644646
}
645-
fmt.Fprintln(out, "--")
646-
fmt.Fprintln(out, c.Body)
647647

648-
for _, reply := range c.Replies.Comments {
649-
printRawComment(out, reply, " ")
648+
for _, reply := range replies {
649+
printRawComment(out, reply)
650650
}
651651

652652
return nil

pkg/cmd/discussion/view/view_test.go

Lines changed: 40 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -484,25 +484,19 @@ func TestViewRun(t *testing.T) {
484484
Order: "oldest",
485485
},
486486
wantStdout: heredoc.Doc(`
487-
title: an interesting question
488-
state: OPEN
489-
category: Q&A
490-
author: monalisa
491-
labels: help-wanted
492-
comments: 2
493-
number: 123
494-
url: https://github.com/OWNER/REPO/discussions/123
495-
--
496-
about my interesting question
497-
comment: octocat 2025-03-01T00:00:00Z https://github.com/OWNER/REPO/discussions/123#discussioncomment-1 answer
487+
author: octocat
488+
created: 2025-03-01T00:00:00Z
489+
url: https://github.com/OWNER/REPO/discussions/123#discussioncomment-1
490+
answer: true
498491
--
499492
This is a comment
500-
comment: hubot 2025-03-01T00:30:00Z https://github.com/OWNER/REPO/discussions/123#discussioncomment-2
501-
--
502-
Thanks!
503-
comment: monalisa 2025-03-01T00:45:00Z https://github.com/OWNER/REPO/discussions/123#discussioncomment-3
493+
--
494+
author: monalisa
495+
created: 2025-03-01T00:45:00Z
496+
url: https://github.com/OWNER/REPO/discussions/123#discussioncomment-3
504497
--
505498
Another comment
499+
--
506500
`),
507501
},
508502
{
@@ -581,26 +575,19 @@ func TestViewRun(t *testing.T) {
581575
Order: "oldest",
582576
},
583577
wantStdout: heredoc.Doc(`
584-
title: an interesting question
585-
state: OPEN
586-
category: Q&A
587-
author: monalisa
588-
labels: help-wanted
589-
comments: 2
590-
next: NEXT_CURSOR_456
591-
number: 123
592-
url: https://github.com/OWNER/REPO/discussions/123
593-
--
594-
about my interesting question
595-
comment: octocat 2025-03-01T00:00:00Z https://github.com/OWNER/REPO/discussions/123#discussioncomment-1 answer
578+
author: octocat
579+
created: 2025-03-01T00:00:00Z
580+
url: https://github.com/OWNER/REPO/discussions/123#discussioncomment-1
581+
answer: true
596582
--
597583
This is a comment
598-
comment: hubot 2025-03-01T00:30:00Z https://github.com/OWNER/REPO/discussions/123#discussioncomment-2
599-
--
600-
Thanks!
601-
comment: monalisa 2025-03-01T00:45:00Z https://github.com/OWNER/REPO/discussions/123#discussioncomment-3
584+
--
585+
author: monalisa
586+
created: 2025-03-01T00:45:00Z
587+
url: https://github.com/OWNER/REPO/discussions/123#discussioncomment-3
602588
--
603589
Another comment
590+
--
604591
`),
605592
},
606593
{
@@ -884,16 +871,18 @@ func TestViewRun(t *testing.T) {
884871
Order: "oldest",
885872
},
886873
wantStdout: heredoc.Doc(`
887-
comment: octocat 2025-03-01T00:00:00Z https://github.com/OWNER/REPO/discussions/123#discussioncomment-1 answer
888-
replies: 2
874+
author: hubot
875+
created: 2025-03-01T00:20:00Z
876+
url: https://github.com/OWNER/REPO/discussions/123#discussioncomment-2
877+
--
878+
First reply
879+
--
880+
author: monalisa
881+
created: 2025-03-01T00:40:00Z
882+
url: https://github.com/OWNER/REPO/discussions/123#discussioncomment-3
883+
--
884+
Second reply
889885
--
890-
This is the parent comment
891-
comment: hubot 2025-03-01T00:20:00Z https://github.com/OWNER/REPO/discussions/123#discussioncomment-2
892-
--
893-
First reply
894-
comment: monalisa 2025-03-01T00:40:00Z https://github.com/OWNER/REPO/discussions/123#discussioncomment-3
895-
--
896-
Second reply
897886
`),
898887
},
899888
{
@@ -913,17 +902,18 @@ func TestViewRun(t *testing.T) {
913902
Order: "oldest",
914903
},
915904
wantStdout: heredoc.Doc(`
916-
comment: octocat 2025-03-01T00:00:00Z https://github.com/OWNER/REPO/discussions/123#discussioncomment-1 answer
917-
replies: 2
918-
next: NEXT_CUR_456
905+
author: hubot
906+
created: 2025-03-01T00:20:00Z
907+
url: https://github.com/OWNER/REPO/discussions/123#discussioncomment-2
908+
--
909+
First reply
910+
--
911+
author: monalisa
912+
created: 2025-03-01T00:40:00Z
913+
url: https://github.com/OWNER/REPO/discussions/123#discussioncomment-3
914+
--
915+
Second reply
919916
--
920-
This is the parent comment
921-
comment: hubot 2025-03-01T00:20:00Z https://github.com/OWNER/REPO/discussions/123#discussioncomment-2
922-
--
923-
First reply
924-
comment: monalisa 2025-03-01T00:40:00Z https://github.com/OWNER/REPO/discussions/123#discussioncomment-3
925-
--
926-
Second reply
927917
`),
928918
},
929919
{

0 commit comments

Comments
 (0)