From 8126d82956636a2525263e2d0d985d721fdb8074 Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Thu, 21 Mar 2019 16:30:23 -0400 Subject: Add context-specific commands --- commands/account/account.go | 16 +++++++++++ commands/account/cf.go | 38 ++++++++++++++++++++++++++ commands/account/delete-message.go | 25 +++++++++++++++++ commands/account/next-folder.go | 46 +++++++++++++++++++++++++++++++ commands/account/next-message.go | 55 ++++++++++++++++++++++++++++++++++++++ commands/account/select-message.go | 34 +++++++++++++++++++++++ 6 files changed, 214 insertions(+) create mode 100644 commands/account/account.go create mode 100644 commands/account/cf.go create mode 100644 commands/account/delete-message.go create mode 100644 commands/account/next-folder.go create mode 100644 commands/account/next-message.go create mode 100644 commands/account/select-message.go (limited to 'commands/account') diff --git a/commands/account/account.go b/commands/account/account.go new file mode 100644 index 0000000..918d962 --- /dev/null +++ b/commands/account/account.go @@ -0,0 +1,16 @@ +package account + +import ( + "git.sr.ht/~sircmpwn/aerc2/commands" +) + +var ( + AccountCommands *commands.Commands +) + +func register(name string, cmd commands.AercCommand) { + if AccountCommands == nil { + AccountCommands = commands.NewCommands() + } + AccountCommands.Register(name, cmd) +} diff --git a/commands/account/cf.go b/commands/account/cf.go new file mode 100644 index 0000000..3d12b5c --- /dev/null +++ b/commands/account/cf.go @@ -0,0 +1,38 @@ +package account + +import ( + "errors" + + "git.sr.ht/~sircmpwn/aerc2/widgets" +) + +var ( + history map[string]string +) + +func init() { + history = make(map[string]string) + register("cf", ChangeFolder) +} + +func ChangeFolder(aerc *widgets.Aerc, args []string) error { + if len(args) != 2 { + return errors.New("Usage: cf ") + } + acct := aerc.SelectedAccount() + if acct == nil { + return errors.New("No account selected") + } + previous := acct.Directories().Selected() + if args[1] == "-" { + if dir, ok := history[acct.Name()]; ok { + acct.Directories().Select(dir) + } else { + return errors.New("No previous folder to return to") + } + } else { + acct.Directories().Select(args[1]) + } + history[acct.Name()] = previous + return nil +} diff --git a/commands/account/delete-message.go b/commands/account/delete-message.go new file mode 100644 index 0000000..40e3bba --- /dev/null +++ b/commands/account/delete-message.go @@ -0,0 +1,25 @@ +package account + +import ( + "errors" + + "git.sr.ht/~sircmpwn/aerc2/widgets" +) + +func init() { + register("delete-message", DeleteMessage) +} + +func DeleteMessage(aerc *widgets.Aerc, args []string) error { + if len(args) != 1 { + return errors.New("Usage: :delete-message") + } + acct := aerc.SelectedAccount() + if acct == nil { + return errors.New("No account selected") + } + store := acct.Messages().Store() + msg := acct.Messages().Selected() + store.Delete([]uint32{msg.Uid}) + return nil +} diff --git a/commands/account/next-folder.go b/commands/account/next-folder.go new file mode 100644 index 0000000..1b59af1 --- /dev/null +++ b/commands/account/next-folder.go @@ -0,0 +1,46 @@ +package account + +import ( + "errors" + "fmt" + "strconv" + + "git.sr.ht/~sircmpwn/aerc2/widgets" +) + +func init() { + register("next-folder", NextPrevFolder) + register("prev-folder", NextPrevFolder) +} + +func nextPrevFolderUsage(cmd string) error { + return errors.New(fmt.Sprintf("Usage: %s [n]", cmd)) +} + +func NextPrevFolder(aerc *widgets.Aerc, args []string) error { + if len(args) > 2 { + return nextPrevFolderUsage(args[0]) + } + var ( + n int = 1 + err error + ) + if len(args) > 1 { + n, err = strconv.Atoi(args[1]) + if err != nil { + return nextPrevFolderUsage(args[0]) + } + } + acct := aerc.SelectedAccount() + if acct == nil { + return errors.New("No account selected") + } + for ; n > 0; n-- { + if args[0] == "prev-folder" { + acct.Directories().Prev() + } else { + acct.Directories().Next() + } + } + return nil +} diff --git a/commands/account/next-message.go b/commands/account/next-message.go new file mode 100644 index 0000000..d2c006f --- /dev/null +++ b/commands/account/next-message.go @@ -0,0 +1,55 @@ +package account + +import ( + "errors" + "fmt" + "strconv" + "strings" + + "git.sr.ht/~sircmpwn/aerc2/widgets" +) + +func init() { + register("next-message", NextPrevMessage) + register("prev-message", NextPrevMessage) +} + +func nextPrevMessageUsage(cmd string) error { + return errors.New(fmt.Sprintf("Usage: %s [[%%]]", cmd)) +} + +func NextPrevMessage(aerc *widgets.Aerc, args []string) error { + if len(args) > 2 { + return nextPrevMessageUsage(args[0]) + } + var ( + n int = 1 + err error + pct bool + ) + if len(args) > 1 { + if strings.HasSuffix(args[1], "%") { + pct = true + args[1] = args[1][:len(args[1])-1] + } + n, err = strconv.Atoi(args[1]) + if err != nil { + return nextPrevMessageUsage(args[0]) + } + } + acct := aerc.SelectedAccount() + if acct == nil { + return errors.New("No account selected") + } + if pct { + n = int(float64(acct.Messages().Height()) * (float64(n) / 100.0)) + } + for ; n > 0; n-- { + if args[0] == "prev-message" { + acct.Messages().Prev() + } else { + acct.Messages().Next() + } + } + return nil +} diff --git a/commands/account/select-message.go b/commands/account/select-message.go new file mode 100644 index 0000000..54f48f2 --- /dev/null +++ b/commands/account/select-message.go @@ -0,0 +1,34 @@ +package account + +import ( + "errors" + "strconv" + + "git.sr.ht/~sircmpwn/aerc2/widgets" +) + +func init() { + register("select-message", SelectMessage) +} + +func SelectMessage(aerc *widgets.Aerc, args []string) error { + if len(args) != 2 { + return errors.New("Usage: :select-message ") + } + var ( + n int = 1 + err error + ) + if len(args) > 1 { + n, err = strconv.Atoi(args[1]) + if err != nil { + return errors.New("Usage: :select-message ") + } + } + acct := aerc.SelectedAccount() + if acct == nil { + return errors.New("No account selected") + } + acct.Messages().Select(n) + return nil +} -- cgit v1.2.3