commands: git bug comment now show the comments of a bug

Michael Muré created

Change summary

bug/comment.go           |  9 +++++-
bug/person.go            |  4 ++
commands/comment.go      | 58 ++++++++++++++---------------------------
commands/show.go         |  2 
termui/bug_table.go      | 24 ++++++++--------
util/text/left_padded.go | 30 ++++++++++++++++-----
6 files changed, 67 insertions(+), 60 deletions(-)

Detailed changes

bug/comment.go 🔗

@@ -17,8 +17,13 @@ type Comment struct {
 	UnixTime int64
 }
 
-// FormatTime format the UnixTime of the comment for human consumption
-func (c Comment) FormatTime() string {
+// FormatTimeRel format the UnixTime of the comment for human consumption
+func (c Comment) FormatTimeRel() string {
 	t := time.Unix(c.UnixTime, 0)
 	return humanize.Time(t)
 }
+
+func (c Comment) FormatTime() string {
+	t := time.Unix(c.UnixTime, 0)
+	return t.Format("Mon Jan 2 15:04:05 2006 +0200")
+}

bug/person.go 🔗

@@ -63,3 +63,7 @@ func (p Person) Validate() error {
 
 	return nil
 }
+
+func (p Person) String() string {
+	return fmt.Sprintf("%s <%s>", p.Name, p.Email)
+}

commands/comment.go 🔗

@@ -4,16 +4,13 @@ import (
 	"errors"
 	"fmt"
 
+	"github.com/MichaelMure/git-bug/bug"
 	"github.com/MichaelMure/git-bug/cache"
-	"github.com/MichaelMure/git-bug/input"
+	"github.com/MichaelMure/git-bug/util/colors"
+	"github.com/MichaelMure/git-bug/util/text"
 	"github.com/spf13/cobra"
 )
 
-var (
-	commentMessageFile string
-	commentMessage     string
-)
-
 func runComment(cmd *cobra.Command, args []string) error {
 	var err error
 
@@ -33,40 +30,33 @@ func runComment(cmd *cobra.Command, args []string) error {
 
 	prefix := args[0]
 
-	if commentMessageFile != "" && commentMessage == "" {
-		commentMessage, err = input.FromFile(commentMessageFile)
-		if err != nil {
-			return err
-		}
-	}
-
-	if commentMessage == "" {
-		commentMessage, err = input.BugCommentEditorInput(backend.Repository())
-		if err == input.ErrEmptyMessage {
-			fmt.Println("Empty message, aborting.")
-			return nil
-		}
-		if err != nil {
-			return err
-		}
-	}
-
 	b, err := backend.ResolveBugPrefix(prefix)
 	if err != nil {
 		return err
 	}
 
-	err = b.AddComment(commentMessage)
-	if err != nil {
-		return err
-	}
+	snap := b.Snapshot()
+
+	commentsTextOutput(snap.Comments)
+
+	return nil
+}
 
-	return b.Commit()
+func commentsTextOutput(comments []bug.Comment) {
+	for i, comment := range comments {
+		if i != 0 {
+			fmt.Println()
+		}
+
+		fmt.Printf("Author: %s\n", colors.Magenta(comment.Author))
+		fmt.Printf("Date: %s\n\n", comment.FormatTime())
+		fmt.Println(text.LeftPad(comment.Message, 4))
+	}
 }
 
 var commentCmd = &cobra.Command{
 	Use:   "comment <id>",
-	Short: "Add a new comment to a bug",
+	Short: "Show a bug's comments",
 	RunE:  runComment,
 }
 
@@ -74,12 +64,4 @@ func init() {
 	RootCmd.AddCommand(commentCmd)
 
 	commentCmd.Flags().SortFlags = false
-
-	commentCmd.Flags().StringVarP(&commentMessageFile, "file", "F", "",
-		"Take the message from the given file. Use - to read the message from the standard input",
-	)
-
-	commentCmd.Flags().StringVarP(&commentMessage, "message", "m", "",
-		"Provide the new message from the command line",
-	)
 }

commands/show.go 🔗

@@ -49,7 +49,7 @@ func runShowBug(cmd *cobra.Command, args []string) error {
 
 	fmt.Printf("%s opened this issue %s\n\n",
 		colors.Magenta(firstComment.Author.Name),
-		firstComment.FormatTime(),
+		firstComment.FormatTimeRel(),
 	)
 
 	var labels = make([]string, len(snapshot.Labels))

termui/bug_table.go 🔗

@@ -297,12 +297,12 @@ func (bt *bugTable) render(v *gocui.View, maxX int) {
 			person = create.Author
 		}
 
-		id := text.LeftPaddedString(snap.HumanId(), columnWidths["id"], 2)
-		status := text.LeftPaddedString(snap.Status.String(), columnWidths["status"], 2)
-		title := text.LeftPaddedString(snap.Title, columnWidths["title"], 2)
-		author := text.LeftPaddedString(person.Name, columnWidths["author"], 2)
-		summary := text.LeftPaddedString(snap.Summary(), columnWidths["summary"], 2)
-		lastEdit := text.LeftPaddedString(humanize.Time(snap.LastEditTime()), columnWidths["lastEdit"], 2)
+		id := text.LeftPadMaxLine(snap.HumanId(), columnWidths["id"], 2)
+		status := text.LeftPadMaxLine(snap.Status.String(), columnWidths["status"], 2)
+		title := text.LeftPadMaxLine(snap.Title, columnWidths["title"], 2)
+		author := text.LeftPadMaxLine(person.Name, columnWidths["author"], 2)
+		summary := text.LeftPadMaxLine(snap.Summary(), columnWidths["summary"], 2)
+		lastEdit := text.LeftPadMaxLine(humanize.Time(snap.LastEditTime()), columnWidths["lastEdit"], 2)
 
 		fmt.Fprintf(v, "%s %s %s %s %s %s\n",
 			colors.Cyan(id),
@@ -318,12 +318,12 @@ func (bt *bugTable) render(v *gocui.View, maxX int) {
 func (bt *bugTable) renderHeader(v *gocui.View, maxX int) {
 	columnWidths := bt.getColumnWidths(maxX)
 
-	id := text.LeftPaddedString("ID", columnWidths["id"], 2)
-	status := text.LeftPaddedString("STATUS", columnWidths["status"], 2)
-	title := text.LeftPaddedString("TITLE", columnWidths["title"], 2)
-	author := text.LeftPaddedString("AUTHOR", columnWidths["author"], 2)
-	summary := text.LeftPaddedString("SUMMARY", columnWidths["summary"], 2)
-	lastEdit := text.LeftPaddedString("LAST EDIT", columnWidths["lastEdit"], 2)
+	id := text.LeftPadMaxLine("ID", columnWidths["id"], 2)
+	status := text.LeftPadMaxLine("STATUS", columnWidths["status"], 2)
+	title := text.LeftPadMaxLine("TITLE", columnWidths["title"], 2)
+	author := text.LeftPadMaxLine("AUTHOR", columnWidths["author"], 2)
+	summary := text.LeftPadMaxLine("SUMMARY", columnWidths["summary"], 2)
+	lastEdit := text.LeftPadMaxLine("LAST EDIT", columnWidths["lastEdit"], 2)
 
 	fmt.Fprintf(v, "\n")
 	fmt.Fprintf(v, "%s %s %s %s %s %s\n", id, status, title, author, summary, lastEdit)

util/text/left_padded.go 🔗

@@ -1,20 +1,36 @@
 package text
 
 import (
+	"bytes"
 	"strings"
 	"unicode/utf8"
 )
 
-// LeftPaddedString pads a string on the left by a specified amount and pads the string on the right to fill the maxLength
-func LeftPaddedString(value string, maxValueLength, padAmount int) string {
+// LeftPadMaxLine pads a string on the left by a specified amount and pads the string on the right to fill the maxLength
+func LeftPadMaxLine(value string, maxValueLength, leftPad int) string {
 	valueLength := utf8.RuneCountInString(value)
-	if maxValueLength-padAmount >= valueLength {
-		return strings.Repeat(" ", padAmount) + value + strings.Repeat(" ", maxValueLength-valueLength-padAmount)
-	} else if maxValueLength-padAmount < valueLength {
-		tmp := strings.Trim(value[0:maxValueLength-padAmount-3], " ") + "..."
+	if maxValueLength-leftPad >= valueLength {
+		return strings.Repeat(" ", leftPad) + value + strings.Repeat(" ", maxValueLength-valueLength-leftPad)
+	} else if maxValueLength-leftPad < valueLength {
+		tmp := strings.Trim(value[0:maxValueLength-leftPad-3], " ") + "..."
 		tmpLength := utf8.RuneCountInString(tmp)
-		return strings.Repeat(" ", padAmount) + tmp + strings.Repeat(" ", maxValueLength-tmpLength-padAmount)
+		return strings.Repeat(" ", leftPad) + tmp + strings.Repeat(" ", maxValueLength-tmpLength-leftPad)
 	}
 
 	return value
 }
+
+// LeftPad left pad each line of the given text
+func LeftPad(text string, leftPad int) string {
+	var result bytes.Buffer
+
+	pad := strings.Repeat(" ", leftPad)
+
+	for _, line := range strings.Split(text, "\n") {
+		result.WriteString(pad)
+		result.WriteString(line)
+		result.WriteString("\n")
+	}
+
+	return result.String()
+}