Clean up mail listing Still need to fix column width thing.
Anirudh Oppiliappan x@icyphox.sh
Sun, 04 Jul 2021 21:47:55 +0530
6 files changed,
109 insertions(+),
42 deletions(-)
M
commands/list.go
→
commands/list.go
@@ -1,15 +1,13 @@
package commands import ( - "fmt" "net/mail" - "os" "path/filepath" "sort" - "text/tabwriter" "time" gomaildir "github.com/emersion/go-maildir" + "github.com/jedib0t/go-pretty/v6/table" "git.icyphox.sh/mx/config" "git.icyphox.sh/mx/maildir"@@ -38,6 +36,10 @@ }
return r } +func formatDate(date time.Time) string { + return date.Format("2006-02-01 15:04") +} + type ListLine struct { from string date time.Time@@ -45,31 +47,7 @@ flags string
subject string } -func printListLine(lines []ListLine) error { - // w, h, err := term.GetSize() - // if err != nil { - // return err - // } - - writer := tabwriter.NewWriter(os.Stdout, 0, 0, 5, '\t', 0) - layout := "2006-01-02 15:04" - for index, ll := range lines { - dt := time.Time.Format(ll.date, layout) - fmt.Fprintln(writer, index, ll.flags, dt, ll.from, ll.subject) - } - // fmt.Printf("%s%d %s %-20s %-s\n", ll.flags, ll.index, ll.date, ll.from, ll.subject) - return nil -} - -func (List) Execute(cmd string) error { - basePath := config.Config.Maildir.Base - mailbox := config.Config.Maildir.Inbox - dir := gomaildir.Dir(filepath.Join(basePath, mailbox)) - keys, err := dir.Keys() - if err != nil { - return err - } - +func (ll *ListLine) makeListLines(keys []string, dir gomaildir.Dir) ([]ListLine, error) { lines := []ListLine{} for _, k := range keys { m := maildir.Message{}@@ -77,9 +55,9 @@ flags := []gomaildir.Flag{}
m.Key = k m.Dir = dir - flags, err = m.Flags() + flags, err := m.Flags() if err != nil { - return err + return nil, err } strflags := string(flagToRune(flags))@@ -89,23 +67,46 @@ subject, err := m.GetHeader("Subject")
from, err := m.GetHeader("From") addr, err := mail.ParseAddress(from) if err != nil { - return err + return nil, err } - ll := ListLine{ - from: addr.Name, - date: date, - flags: strflags, - subject: subject, + if len(addr.Name) > 0 { + ll.from = addr.Name + } else { + ll.from = addr.Address } + ll.date = date + ll.flags = strflags + ll.subject = subject - lines = append(lines, ll) + lines = append(lines, *ll) } // sort lines by date sort.Slice(lines, func(i, j int) bool { return lines[i].date.Before(lines[j].date) }) + return lines, nil +} - printListLine(lines) - return nil +func (List) Execute(cmd string) error { + basePath := config.Config.Maildir.Base + mailbox := config.Config.Maildir.Inbox + dir := gomaildir.Dir(filepath.Join(basePath, mailbox)) + keys, err := dir.Keys() + if err != nil { + return err + } + + tbl := GetTable() + ll := ListLine{} + lines, err := ll.makeListLines(keys, dir) + + for index, line := range lines { + tbl.AppendRow(table.Row{ + index, line.flags, line.from, formatDate(line.date), line.subject, + }) + } + tbl.Render() + + return err }
A
commands/table.go
@@ -0,0 +1,46 @@
+package commands + +import ( + "os" + + "github.com/jedib0t/go-pretty/v6/table" + "github.com/jedib0t/go-pretty/v6/text" + + "git.icyphox.sh/mx/term" +) + +func GetTable() table.Writer { + w, _, _ := term.GetSize() + t := table.NewWriter() + t.SetOutputMirror(os.Stdout) + + t.SetStyle(table.Style{ + Box: table.BoxStyle{ + BottomLeft: " ", + BottomRight: " ", + BottomSeparator: " ", + EmptySeparator: text.RepeatAndTrim(" ", text.RuneCount(" ")), + Left: "", + LeftSeparator: " ", + MiddleHorizontal: " ", + MiddleSeparator: " ", + MiddleVertical: " ", + PaddingLeft: " ", + PaddingRight: " ", + PageSeparator: "\n", + Right: " ", + RightSeparator: " ", + TopLeft: " ", + TopRight: " ", + TopSeparator: " ", + UnfinishedRow: "", + }, + }) + t.SetColumnConfigs([]table.ColumnConfig{ + {Number: 1}, + {Number: 2}, + {Number: 3, WidthMax: 25, WidthMaxEnforcer: text.Trim}, + }) + t.SetAllowedRowLength(w) + return t +}
M
go.sum
→
go.sum
@@ -1,9 +1,21 @@
+github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/emersion/go-maildir v0.2.0 h1:fC4+UVGl8GcQGbFF7AWab2JMf4VbKz+bMNv07xxhzs8= github.com/emersion/go-maildir v0.2.0/go.mod h1:I2j27lND/SRLgxROe50Vam81MSaqPFvJ0OHNnDZ7n84= -github.com/mattn/go-runewidth v0.0.3 h1:a+kO+98RDGEfo6asOGMmpodZq4FNtnGP54yps8BzLR4= +github.com/fzipp/gocyclo v0.3.1/go.mod h1:DJHO6AUmbdqj2ET4Z9iArSuwWgYDRryYt2wASxc7x3E= +github.com/jedib0t/go-pretty/v6 v6.2.2 h1:o3McN0rQ4X+IU+HduppSp9TwRdGLRW2rhJXy9CJaCRw= +github.com/jedib0t/go-pretty/v6 v6.2.2/go.mod h1:+nE9fyyHGil+PuISTCrp7avEdo6bqoMwqZnuiK2r2a0= github.com/mattn/go-runewidth v0.0.3/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU= +github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0= +github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI= github.com/peterh/liner v1.2.1 h1:O4BlKaq/LWu6VRWmol4ByWfzx6MfXc5Op5HETyIy5yg= github.com/peterh/liner v1.2.1/go.mod h1:CRroGNssyjTd/qIG2FyxByd2S8JEAZXBl4qUrZf8GS0= +github.com/pkg/profile v1.2.1/go.mod h1:hJw3o1OdXxsrSjjVksARp5W95eeEaEfptyVZyv6JUPA= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w= +github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= +golang.org/x/sys v0.0.0-20180816055513-1c9583448a9c/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c h1:F1jZWGFhYfh0Ci55sIpILtKKK8p3i2/krTr0H1rg74I= golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
M
maildir/message.go
→
maildir/message.go
@@ -4,6 +4,7 @@ import (
"bufio" "fmt" "io" + "mime" "net/mail" "github.com/emersion/go-maildir"@@ -64,6 +65,11 @@ func (m Message) Remove() error {
return m.Dir.Remove(m.Key) } +func decodeMime(str string) (string, error) { + dec := new(mime.WordDecoder) + return dec.DecodeHeader(str) +} + func (m Message) GetHeader(h string) (string, error) { r, err := m.NewReader() msg, err := mail.ReadMessage(r)@@ -71,5 +77,6 @@ if err != nil {
return "", err } - return msg.Header.Get(h), nil + decodedHeader, err := decodeMime(msg.Header.Get(h)) + return decodedHeader, err }
M
term/utils.go
→
term/utils.go
@@ -6,5 +6,5 @@ )
func GetSize() (width, height int, err error) { size, err := unix.IoctlGetWinsize(0, unix.TIOCGWINSZ) - return int(size.Col), int(size.Row), nil + return int(size.Col), int(size.Row), err }