~sircmpwn/aerc

Add archive command v1 PROPOSED

Robert Günzler: 1
 Add archive command

 3 files changed, 62 insertions(+), 0 deletions(-)
Export patchset (mbox)
How do I use this?

Copy & paste the following snippet into your terminal to import this patchset into git:

curl -s https://lists.sr.ht/~sircmpwn/aerc/patches/5961/mbox | git am -3
Learn more about email & git
View this thread in the archives

[PATCH] Add archive command Export this patch

Adds an archive command that moves the current message into the folder
specified in the account config entry.

Supports three layouts at this point:

- flat: puts all messages next to each other
- year: creates a folder per year
- month: same as above, plus folders per month
---
 commands/account/archive.go | 58 +++++++++++++++++++++++++++++++++++++
 config/binds.conf           |  1 +
 config/config.go            |  3 ++
 3 files changed, 62 insertions(+)
 create mode 100644 commands/account/archive.go

diff --git a/commands/account/archive.go b/commands/account/archive.go
new file mode 100644
index 0000000..7aa6384
--- /dev/null
+++ b/commands/account/archive.go
@@ -0,0 +1,58 @@
package account

import (
	"errors"
	"fmt"
	"path"
	"time"

	"github.com/gdamore/tcell"

	"git.sr.ht/~sircmpwn/aerc/widgets"
	"git.sr.ht/~sircmpwn/aerc/worker/types"
)

const (
	ARCHIVE_FLAT  = "flat"
	ARCHIVE_YEAR  = "year"
	ARCHIVE_MONTH = "month"
)

func init() {
	register("archive", Archive)
}

func Archive(aerc *widgets.Aerc, args []string) error {
	if len(args) != 2 {
		return errors.New("Usage: archive <flat|year|month>")
	}
	acct := aerc.SelectedAccount()
	if acct == nil {
		return errors.New("No account selected")
	}
	msg := acct.Messages().Selected()
	store := acct.Messages().Store()
	archiveDir := acct.AccountConfig().Archive
	acct.Messages().Next()

	switch args[1] {
	case ARCHIVE_MONTH:
		archiveDir = path.Join(archiveDir,
			fmt.Sprintf("%d", msg.Envelope.Date.Year()),
			fmt.Sprintf("%02d", msg.Envelope.Date.Month()))
	case ARCHIVE_YEAR:
		archiveDir = path.Join(archiveDir, fmt.Sprintf("%v", msg.Envelope.Date.Year()))
	case ARCHIVE_FLAT:
	}

	store.Move([]uint32{msg.Uid}, archiveDir, func(msg types.WorkerMessage) {
		switch msg := msg.(type) {
		case *types.Done:
			aerc.PushStatus("Messages archived.", 10*time.Second)
		case *types.Error:
			aerc.PushStatus(" "+msg.Error.Error(), 10*time.Second).
				Color(tcell.ColorDefault, tcell.ColorRed)
		}
	})
	return nil
}
diff --git a/config/binds.conf b/config/binds.conf
index f758b17..2b27454 100644
--- a/config/binds.conf
+++ b/config/binds.conf
@@ -28,6 +28,7 @@ K = :prev-folder<Enter>
<Enter> = :view<Enter>
d = :confirm 'Really delete this message?' ':delete-message<Enter>'<Enter>
D = :delete<Enter>
a = :archive year<Enter>

C = :compose<Enter>

diff --git a/config/config.go b/config/config.go
index 1fb764b..1c419c2 100644
--- a/config/config.go
+++ b/config/config.go
@@ -33,6 +33,7 @@ const (
)

type AccountConfig struct {
	Archive         string
	CopyTo          string
	Default         string
	From            string
@@ -133,6 +134,8 @@ func loadAccountConfig(path string) ([]AccountConfig, error) {
				account.From = val
			} else if key == "copy-to" {
				account.CopyTo = val
			} else if key == "archive" {
				account.Archive = val
			} else if key != "name" {
				account.Params[key] = val
			}
-- 
2.21.0
Nice! I think we're going to need to make sure the target folder exists
before we try to move the message into it, though.