Begin building a sorting feature

This commit is contained in:
Jan Philipp Timme 2019-01-01 12:41:15 +01:00
parent dd47907270
commit c199ca0bcb
2 changed files with 46 additions and 16 deletions

17
main.go
View File

@ -6,7 +6,6 @@ import (
"io" "io"
"os" "os"
"os/exec" "os/exec"
"sort"
"strconv" "strconv"
) )
@ -50,21 +49,7 @@ 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)
} }
// Experiment: Sort queue by Date queue.Sort()
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)
}
})
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

@ -7,6 +7,7 @@ import (
"fmt" "fmt"
"io" "io"
"regexp" "regexp"
"sort"
"strconv" "strconv"
"strings" "strings"
"text/tabwriter" "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, ", ")) 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) { func (queue MailQ) PrintMachineReadable(writer io.Writer) {
for _, entry := range queue.Entries { for _, entry := range queue.Entries {
fmt.Fprintf(writer, "%s\n", entry.MachineReadableString()) fmt.Fprintf(writer, "%s\n", entry.MachineReadableString())