From fc3f6540b8fc941be76d01da49fcd43890ee7376 Mon Sep 17 00:00:00 2001 From: vince Date: Tue, 23 Jun 2020 17:51:42 +0800 Subject: [PATCH] Add org-mode formatting option This adds an option to the formatting flag on the ls, show and user ls commands that allows the user to specify the format of the output in org-mode. This will be useful for emacs users to read it in the editor. --- commands/ls.go | 83 +++++++++++++++++++++++++++++++++++++++- commands/show.go | 92 ++++++++++++++++++++++++++++++++++++++++++--- commands/user_ls.go | 20 +++++++++- 3 files changed, 187 insertions(+), 8 deletions(-) diff --git a/commands/ls.go b/commands/ls.go index 41f48f911d213aa7efa105832a476038f322fc9c..99bdbe820300efddaf08a40a76a55bcb3bdb51de 100644 --- a/commands/ls.go +++ b/commands/ls.go @@ -61,6 +61,8 @@ func runLsBug(_ *cobra.Command, args []string) error { } switch lsOutputFormat { + case "org-mode": + return lsOrgmodeFormatter(backend, bugExcerpt) case "plain": return lsPlainFormatter(backend, bugExcerpt) case "json": @@ -200,11 +202,88 @@ func lsDefaultFormatter(backend *cache.RepoCache, bugExcerpts []*cache.BugExcerp func lsPlainFormatter(_ *cache.RepoCache, bugExcerpts []*cache.BugExcerpt) error { for _, b := range bugExcerpts { - fmt.Printf("[%s] %s\n", b.Status, b.Title) + fmt.Printf("%s [%s] %s\n", b.Id.Human(), b.Status, b.Title) } return nil } +func lsOrgmodeFormatter(backend *cache.RepoCache, bugExcerpts []*cache.BugExcerpt) error { + fmt.Println("+TODO: OPEN | CLOSED") + + for _, b := range bugExcerpts { + status := strings.Title(b.Status.String()) + + var title string + if link, ok := b.CreateMetadata["github-url"]; ok { + title = fmt.Sprintf("[%s][%s]", link, b.Title) + } else { + title = b.Title + } + + var name string + if b.AuthorId != "" { + author, err := backend.ResolveIdentityExcerpt(b.AuthorId) + if err != nil { + return err + } + name = author.DisplayName() + } else { + name = b.LegacyAuthor.DisplayName() + } + + labels := b.Labels + labelsString := ":" + if len(labels) > 0 { + for _, label := range labels { + labelsString += label.String() + ":" + } + } else { + labelsString = "" + } + + fmt.Printf("* %s %s [%s] %s: %s %s\n", + b.Id.Human(), + status, + time.Unix(b.CreateUnixTime, 0), + name, + title, + labelsString, + ) + + fmt.Printf("** Last Edited: %s\n", + time.Unix(b.EditUnixTime, 0).String(), + ) + + fmt.Printf("** Actors:\n") + for _, element := range b.Actors { + actor, err := backend.ResolveIdentityExcerpt(element) + if err != nil { + return err + } + + fmt.Printf(": %s %s\n", + actor.Id.Human(), + actor.DisplayName(), + ) + } + + fmt.Printf("** Participants:\n") + for _, element := range b.Participants { + participant, err := backend.ResolveIdentityExcerpt(element) + if err != nil { + return err + } + + fmt.Printf(": %s %s\n", + participant.Id.Human(), + participant.DisplayName(), + ) + } + } + + return nil +} + // Finish the command flags transformation into the query.Query func completeQuery() error { for _, str := range lsStatusQuery { @@ -287,5 +366,5 @@ func init() { lsCmd.Flags().StringVarP(&lsSortDirection, "direction", "d", "asc", "Select the sorting direction. Valid values are [asc,desc]") lsCmd.Flags().StringVarP(&lsOutputFormat, "format", "f", "default", - "Select the output formatting style. Valid values are [default,plain,json]") + "Select the output formatting style. Valid values are [default,plain,json,org-mode]") } diff --git a/commands/show.go b/commands/show.go index c484c97b8d469387bce1ca40ff13f66c139477ea..101ed57d14227dd10c48ad23b8cd675dd47919da 100644 --- a/commands/show.go +++ b/commands/show.go @@ -78,6 +78,8 @@ func runShowBug(_ *cobra.Command, args []string) error { } switch showOutputFormat { + case "org-mode": + return showOrgmodeFormatter(snapshot) case "json": return showJsonFormatter(snapshot) case "plain": @@ -91,9 +93,9 @@ func runShowBug(_ *cobra.Command, args []string) error { func showDefaultFormatter(snapshot *bug.Snapshot) error { // Header - fmt.Printf("[%s] %s %s\n\n", - colors.Yellow(snapshot.Status), + fmt.Printf("%s [%s] %s\n\n", colors.Cyan(snapshot.Id().Human()), + colors.Yellow(snapshot.Status), snapshot.Title, ) @@ -165,9 +167,9 @@ func showDefaultFormatter(snapshot *bug.Snapshot) error { func showPlainFormatter(snapshot *bug.Snapshot) error { // Header - fmt.Printf("[%s] %s %s\n", - snapshot.Status, + fmt.Printf("%s [%s] %s\n", snapshot.Id().Human(), + snapshot.Status, snapshot.Title, ) @@ -322,6 +324,86 @@ func showJsonFormatter(snapshot *bug.Snapshot) error { return nil } +func showOrgmodeFormatter(snapshot *bug.Snapshot) error { + // Header + fmt.Printf("%s [%s] %s\n", + snapshot.Id().Human(), + snapshot.Status, + snapshot.Title, + ) + + fmt.Printf("* Author: %s\n", + snapshot.Author.DisplayName(), + ) + + fmt.Printf("* Creation Time: %s\n", + snapshot.CreatedAt.String(), + ) + + fmt.Printf("* Last Edit: %s\n", + snapshot.LastEditTime().String(), + ) + + // Labels + var labels = make([]string, len(snapshot.Labels)) + for i, label := range snapshot.Labels { + labels[i] = string(label) + } + + fmt.Printf("* Labels:\n") + if len(labels) > 0 { + fmt.Printf("** %s", strings.TrimSuffix(strings.Join(labels, "\n **"), "\n **")) + } + + // Actors + var actors = make([]string, len(snapshot.Actors)) + for i, actor := range snapshot.Actors { + actors[i] = fmt.Sprintf("%s %s", + actor.Id().Human(), + actor.DisplayName(), + ) + } + + fmt.Printf("* Actors: %s\n", + strings.TrimSuffix(strings.Join(actors, "\n **"), "\n **"), + ) + + // Participants + var participants = make([]string, len(snapshot.Participants)) + for i, participant := range snapshot.Participants { + actors[i] = fmt.Sprintf("%s %s", + participant.Id().Human(), + participant.DisplayName(), + ) + } + + fmt.Printf("* Participants: %s\n", + strings.TrimSuffix(strings.Join(participants, "\n **"), "\n **"), + ) + + fmt.Printf("* Comments:\n") + + for i, comment := range snapshot.Comments { + var message string + fmt.Printf("** #%d %s [%s]\n", + i, + comment.Author.DisplayName(), + ) + + if comment.Message == "" { + message = "No description provided." + } else { + message = strings.Replace(comment.Message, "\n", "\n: ", -1) + } + + fmt.Printf(": %s\n", + message, + ) + } + + return nil +} + var showCmd = &cobra.Command{ Use: "show []", Short: "Display the details of a bug.", @@ -334,5 +416,5 @@ func init() { showCmd.Flags().StringVarP(&showFieldsQuery, "field", "", "", "Select field to display. Valid values are [author,authorEmail,createTime,lastEdit,humanId,id,labels,shortId,status,title,actors,participants]") showCmd.Flags().StringVarP(&showOutputFormat, "format", "f", "default", - "Select the output formatting style. Valid values are [default,plain,json]") + "Select the output formatting style. Valid values are [default,plain,json,org-mode]") } diff --git a/commands/user_ls.go b/commands/user_ls.go index 9d0eebf128d8e370cbdb72b43a46a346f8cf40a9..353bc101154f1369bc3589989f143b95d2c923fb 100644 --- a/commands/user_ls.go +++ b/commands/user_ls.go @@ -22,6 +22,8 @@ func runUserLs(_ *cobra.Command, _ []string) error { interrupt.RegisterCleaner(backend.Close) switch userLsOutputFormat { + case "org-mode": + return userLsOrgmodeFormatter(backend) case "json": return userLsJsonFormatter(backend) case "plain": @@ -93,6 +95,22 @@ func userLsJsonFormatter(backend *cache.RepoCache) error { return nil } +func userLsOrgmodeFormatter(backend *cache.RepoCache) error { + for _, id := range backend.AllIdentityIds() { + i, err := backend.ResolveIdentityExcerpt(id) + if err != nil { + return err + } + + fmt.Printf("* %s %s\n", + i.Id.Human(), + i.DisplayName(), + ) + } + + return nil +} + var userLsCmd = &cobra.Command{ Use: "ls", Short: "List identities.", @@ -104,5 +122,5 @@ func init() { userCmd.AddCommand(userLsCmd) userLsCmd.Flags().SortFlags = false userLsCmd.Flags().StringVarP(&userLsOutputFormat, "format", "f", "default", - "Select the output formatting style. Valid values are [default,plain,json]") + "Select the output formatting style. Valid values are [default,plain,json,org-mode]") }