diff --git a/main.go b/main.go index 150f2c4..37cd8e6 100644 --- a/main.go +++ b/main.go @@ -6,7 +6,6 @@ import ( "io" "os" "os/exec" - "sort" "strconv" ) @@ -50,21 +49,7 @@ func showQueue() { fmt.Printf("Could not fetch queue entries: %s\n", err.Error()) os.Exit(1) } - // Experiment: Sort queue by Date - sort.Slice(queue.Entries, func(a int, b int) bool { - //var sortBy []string = []string{"Status", "Sender", "Date"} - //for _, sortAttribute := range sortBy { - //if sort attributes are equal continue - //else return a < b - - //} - if queue.Entries[a].Date.Equal(queue.Entries[b].Date) { - return queue.Entries[a].Sender > queue.Entries[b].Sender - } else { - return queue.Entries[a].Date.Before(queue.Entries[b].Date) - } - - }) + queue.Sort() if settings["usePager"] == "true" { pagerReader, pagerWriter := io.Pipe() pagerDone := make(chan bool) diff --git a/parser/parser.go b/parser/parser.go index 8f576b8..015ef7c 100644 --- a/parser/parser.go +++ b/parser/parser.go @@ -7,6 +7,7 @@ import ( "fmt" "io" "regexp" + "sort" "strconv" "strings" "text/tabwriter" @@ -53,6 +54,50 @@ 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, ", ")) } +func (queue MailQ) Sort() { + var sortByAttributes []string = []string{"Status", "Recipients", "Sender", "Date"} + if len(sortByAttributes) == 0 { + return + } + sort.Slice(queue.Entries, func(a int, b int) bool { + for _, sortBy := range sortByAttributes { + switch sortBy { + case "Id": + if queue.Entries[a].Id != queue.Entries[b].Id { + return queue.Entries[a].Id < queue.Entries[b].Id + } + case "Status": + if queue.Entries[a].Status != queue.Entries[b].Status { + return queue.Entries[a].Status < queue.Entries[b].Status + } + case "Date": + if !queue.Entries[a].Date.Equal(queue.Entries[b].Date) { + return queue.Entries[a].Date.Before(queue.Entries[b].Date) + } + case "Size": + if queue.Entries[a].Size != queue.Entries[b].Size { + return queue.Entries[a].Size < queue.Entries[b].Size + } + case "Sender": + if queue.Entries[a].Sender != queue.Entries[b].Sender { + return queue.Entries[a].Sender < queue.Entries[b].Sender + } + case "Recipients": + if len(queue.Entries[a].Recipients) != len(queue.Entries[b].Recipients) { + return len(queue.Entries[a].Recipients) < len(queue.Entries[b].Recipients) + } + case "Reason": + if queue.Entries[a].Reason != queue.Entries[b].Reason { + return queue.Entries[a].Reason < queue.Entries[b].Reason + } + default: + // TODO: Handle this error case? + } + } + return false + }) +} + func (queue MailQ) PrintMachineReadable(writer io.Writer) { for _, entry := range queue.Entries { fmt.Fprintf(writer, "%s\n", entry.MachineReadableString())