Introduce configurable sorting

This commit is contained in:
Jan Philipp Timme 2019-01-01 14:36:50 +01:00
parent c199ca0bcb
commit f22be8fccf
2 changed files with 25 additions and 6 deletions

View File

@ -49,7 +49,8 @@ func showQueue() {
fmt.Printf("Could not fetch queue entries: %s\n", err.Error()) fmt.Printf("Could not fetch queue entries: %s\n", err.Error())
os.Exit(1) os.Exit(1)
} }
queue.Sort() sortConfig := parser.NewSortConfig().By("Status", "ASC").By("Sender", "ASC").By("Recipients", "DESC")
queue.Sort(sortConfig)
if settings["usePager"] == "true" { if settings["usePager"] == "true" {
pagerReader, pagerWriter := io.Pipe() pagerReader, pagerWriter := io.Pipe()
pagerDone := make(chan bool) pagerDone := make(chan bool)

View File

@ -54,14 +54,32 @@ func (m QEntry) DetailedString() string {
return fmt.Sprintf("Id: %s\nDate: %s\nStatus: %s\nReason: %s\nSize: %d\nSender: %s\nRecipients: %s", m.Id, m.Date.Format(SortableDateFormat), m.Status, reasonStr, m.Size, m.Sender, strings.Join(m.Recipients, ", ")) return fmt.Sprintf("Id: %s\nDate: %s\nStatus: %s\nReason: %s\nSize: %d\nSender: %s\nRecipients: %s", m.Id, m.Date.Format(SortableDateFormat), m.Status, reasonStr, m.Size, m.Sender, strings.Join(m.Recipients, ", "))
} }
func (queue MailQ) Sort() { type qEntrySortConfig struct {
var sortByAttributes []string = []string{"Status", "Recipients", "Sender", "Date"} attributes []qEntryAttributeSortConfig
if len(sortByAttributes) == 0 { }
type qEntryAttributeSortConfig struct {
attribute string
order string
}
func NewSortConfig() qEntrySortConfig {
return qEntrySortConfig{}
}
func (c qEntrySortConfig) By(field string, order string) qEntrySortConfig {
newAttributeConfig := qEntryAttributeSortConfig{attribute: field, order: order}
c.attributes = append(c.attributes, newAttributeConfig)
return c
}
func (queue MailQ) Sort(config qEntrySortConfig) {
if len(config.attributes) == 0 {
return return
} }
sort.Slice(queue.Entries, func(a int, b int) bool { sort.Slice(queue.Entries, func(a int, b int) bool {
for _, sortBy := range sortByAttributes { for _, sortBy := range config.attributes {
switch sortBy { switch sortBy.attribute {
case "Id": case "Id":
if queue.Entries[a].Id != queue.Entries[b].Id { if queue.Entries[a].Id != queue.Entries[b].Id {
return queue.Entries[a].Id < queue.Entries[b].Id return queue.Entries[a].Id < queue.Entries[b].Id