Browse Source

Make the code for printing a list of comment threads consistent with the code for printing a list of reviews

Omar Jarjur 8 months ago
parent
commit
cb27471505
2 changed files with 26 additions and 16 deletions
  1. 2 9
      commands/list.go
  2. 24 7
      commands/output/output.go

+ 2 - 9
commands/list.go

@@ -20,6 +20,7 @@ import (
 	"encoding/json"
 	"flag"
 	"fmt"
+
 	"github.com/google/git-appraise/commands/output"
 	"github.com/google/git-appraise/repository"
 	"github.com/google/git-appraise/review"
@@ -39,14 +40,8 @@ func listReviews(repo repository.Repo, args []string) error {
 	var reviews []review.Summary
 	if *listAll {
 		reviews = review.ListAll(repo)
-		if !*listJSONOutput {
-			fmt.Printf("Loaded %d reviews:\n", len(reviews))
-		}
 	} else {
 		reviews = review.ListOpen(repo)
-		if !*listJSONOutput {
-			fmt.Printf("Loaded %d open reviews:\n", len(reviews))
-		}
 	}
 	if *listJSONOutput {
 		b, err := json.MarshalIndent(reviews, "", "  ")
@@ -56,9 +51,7 @@ func listReviews(repo repository.Repo, args []string) error {
 		fmt.Println(string(b))
 		return nil
 	}
-	for _, r := range reviews {
-		output.PrintSummary(&r)
-	}
+	output.PrintSummaries(reviews, *listAll)
 	return nil
 }
 

+ 24 - 7
commands/output/output.go

@@ -28,6 +28,15 @@ import (
 )
 
 const (
+	// Template for printing the summary of a list of reviews.
+	reviewListTemplate = `Loaded %d reviews:
+`
+	// Template for printing the summary of a list of open reviews.
+	openReviewListTemplate = `Loaded %d open reviews:
+`
+	// Template for printing the summary of a list of comment threads.
+	commentListTemplate = `Loaded %d comment threads:
+`
 	// Template for printing the summary of a code review.
 	reviewSummaryTemplate = `[%s] %.12s
   %s
@@ -49,9 +58,6 @@ status: %s
 %s`
 	// Template for displaying the summary of the comment threads for a review
 	commentSummaryTemplate = `  comments (%d threads):
-`
-	// Template for displaying the summary of the detached comment threads for a path
-	detachedCommentSummaryTemplate = `Comments (%d threads):
 `
 	// Number of lines of context to print for inline comments
 	contextLineCount = 5
@@ -81,6 +87,18 @@ func getStatusString(r *review.Summary) string {
 	return "rejected"
 }
 
+// PrintSummaries prints single-line summaries of a slice of reviews.
+func PrintSummaries(reviews []review.Summary, listAll bool) {
+	if listAll {
+		fmt.Printf(reviewListTemplate, len(reviews))
+	} else {
+		fmt.Printf(openReviewListTemplate, len(reviews))
+	}
+	for _, r := range reviews {
+		PrintSummary(&r)
+	}
+}
+
 // PrintSummary prints a single-line summary of a review.
 func PrintSummary(r *review.Summary) {
 	statusString := getStatusString(r)
@@ -186,15 +204,14 @@ func printCommentsWithIndent(repo repository.Repo, c []review.CommentThread, ind
 
 // PrintComments prints all of the given comment threads.
 func PrintComments(repo repository.Repo, c []review.CommentThread) error {
-	fmt.Printf(detachedCommentSummaryTemplate, len(c))
-	return printCommentsWithIndent(repo, c, "")
+	fmt.Printf(commentListTemplate, len(c))
+	return printCommentsWithIndent(repo, c, "  ")
 }
 
 // printComments prints all of the comments for the review, with snippets of the preceding source code.
 func printComments(r *review.Review) error {
 	fmt.Printf(commentSummaryTemplate, len(r.Comments))
-	indent := "    "
-	return printCommentsWithIndent(r.Repo, r.Comments, indent)
+	return printCommentsWithIndent(r.Repo, r.Comments, "    ")
 }
 
 // PrintDetails prints a multi-line overview of a review, including all comments.