Skip to content

Commit

Permalink
feat(list,trash): add --sort
Browse files Browse the repository at this point in the history
  • Loading branch information
ShuheiKubota committed Oct 23, 2021
1 parent 79ec555 commit c2fa112
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 10 deletions.
94 changes: 92 additions & 2 deletions cmd_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,19 @@ import (
"encoding/base64"
"fmt"
"os"
"sort"
"strings"
"time"

"github.com/shu-go/gli"
"golang.org/x/xerrors"
gmail "google.golang.org/api/gmail/v1"
"google.golang.org/api/option"
)

type listCmd struct {
Format string `cli:"format,f" default:"{id} {subject} ({date})" help:"{id}, {subject}, {date}, {snippet}, {body}"`
Format string `cli:"format,f" default:"{id} {subject} ({date})" help:"{id}, {subject}, {date}, {snippet}, {body}"`
Sort gli.StrList `cli:"sort" default:"-date,subject,id" help:"sort criteria that is a list of [id, subject, date, snippet] (- means descending order)"`
}

func (c listCmd) Run(g globalCmd, args []string) error {
Expand Down Expand Up @@ -51,6 +55,8 @@ func (c listCmd) Run(g globalCmd, args []string) error {
}
}

list := make([]listItem, 0, 4)

// list messages
{
msgService := gmail.NewUsersMessagesService(gmailService)
Expand Down Expand Up @@ -96,9 +102,93 @@ func (c listCmd) Run(g globalCmd, args []string) error {
content = strings.Replace(content, "{body}", string(decoded), -1)
}

fmt.Println(content)
dt, err := time.Parse(time.RFC822Z, getHeader(m.Payload.Headers, "Date"))
if err != nil {
dt = time.Now()
}
list = append(list, listItem{
Content: content + dt.String(),
ID: m.Id,
Subject: getHeader(m.Payload.Headers, "Subject"),
Date: dt,
Snippet: m.Snippet,
})
}
}

sortListItems(list, c.Sort)
for _, item := range list {
fmt.Println(item.Content)
}

return nil
}

// used to collect displaying items
// list, trash
type listItem struct {
Content string

ID, Subject, Snippet string
Date time.Time
}

func sortListItems(list []listItem, criteria []string) {
sort.Slice(list, func(i, j int) bool {
for _, c := range criteria {
switch strings.ToLower(c) {
case "id":
if list[i].ID < list[j].ID {
return true
} else if list[i].ID > list[j].ID {
return false
}
case "-id":
if list[j].ID < list[i].ID {
return true
} else if list[j].ID > list[i].ID {
return false
}
case "subject":
if list[i].Subject < list[j].Subject {
return true
} else if list[i].Subject > list[j].Subject {
return false
}
case "-subject":
if list[j].Subject < list[i].Subject {
return true
} else if list[j].Subject > list[i].Subject {
return false
}
case "date":
if list[i].Date.Before(list[j].Date) {
return true
} else if list[i].Date.After(list[j].Date) {
return false
}
case "-date":
if list[j].Date.Before(list[i].Date) {
return true
} else if list[j].Date.After(list[i].Date) {
return false
}
case "snippet":
if list[i].Snippet < list[j].Snippet {
return true
} else if list[i].Snippet > list[j].Snippet {
return false
}
case "-snippet":
if list[j].Snippet < list[i].Snippet {
return true
} else if list[j].Snippet > list[i].Snippet {
return false
}
//default:
//nop
}
}
return false
})
}
31 changes: 23 additions & 8 deletions cmd_trash.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"os"
"strings"
"time"

"github.com/shu-go/gli"
"golang.org/x/xerrors"
Expand All @@ -19,7 +20,8 @@ type trashCmd struct {

Confirm bool `cli:"confirm,i" help:"confirm each deletion" default:"true"`

Format string `cli:"format,f" default:"{id} {subject} ({date})" help:"{id}, {subject}, {date}, {snippet}, {body}"`
Format string `cli:"format,f" default:"{id} {subject} ({date})" help:"{id}, {subject}, {date}, {snippet}, {body}"`
Sort gli.StrList `cli:"sort" default:"-date,subject,id" help:"sort criteria that is a list of [id, subject, date, snippet] (- means descending order)"`
}

func (c trashCmd) Run(g globalCmd, args []string) error {
Expand Down Expand Up @@ -59,11 +61,13 @@ func (c trashCmd) Run(g globalCmd, args []string) error {
}
}

list := make([]listItem, 0, 4)

// list messages
{
idset := make(map[string]string)
idset := make(map[string]struct{})
for _, id := range c.IDs {
idset[id] = ""
idset[id] = struct{}{}
}

msgService := gmail.NewUsersMessagesService(gmailService)
Expand All @@ -75,7 +79,7 @@ func (c trashCmd) Run(g globalCmd, args []string) error {
return err
}
for _, msg := range resp.Messages {
idset[msg.Id] = ""
idset[msg.Id] = struct{}{}
}
}

Expand Down Expand Up @@ -117,11 +121,22 @@ func (c trashCmd) Run(g globalCmd, args []string) error {
content = strings.Replace(content, "{body}", string(decoded), -1)
}

idset[id] = content
dt, err := time.Parse(time.RFC822Z, getHeader(m.Payload.Headers, "Date"))
if err != nil {
dt = time.Now()
}
list = append(list, listItem{
Content: content + dt.String(),
ID: m.Id,
Subject: getHeader(m.Payload.Headers, "Subject"),
Date: dt,
Snippet: m.Snippet,
})
}

for id, content := range idset {
fmt.Println(content)
sortListItems(list, c.Sort)
for _, item := range list {
fmt.Println(item.Content)

if c.Confirm {
var yesno string
Expand All @@ -133,7 +148,7 @@ func (c trashCmd) Run(g globalCmd, args []string) error {
}
}

_, err = msgService.Trash(g.UserID, id).Do()
_, err = msgService.Trash(g.UserID, item.ID).Do()
if err != nil {
return err
}
Expand Down

0 comments on commit c2fa112

Please sign in to comment.