| 1 |
package account |
| 2 |
|
| 3 |
import ( |
| 4 |
"errors" |
| 5 |
"strings" |
| 6 |
"time" |
| 7 |
|
| 8 |
"git.sr.ht/~rjarry/aerc/app" |
| 9 |
"git.sr.ht/~rjarry/aerc/commands" |
| 10 |
"git.sr.ht/~rjarry/aerc/lib/state" |
| 11 |
"git.sr.ht/~rjarry/aerc/models" |
| 12 |
"git.sr.ht/~rjarry/aerc/worker/types" |
| 13 |
"git.sr.ht/~rjarry/go-opt/v2" |
| 14 |
) |
| 15 |
|
| 16 |
type ChangeFolder struct { |
| 17 |
Account string `opt:"-a" complete:"CompleteAccount" desc:"Change to specified account."` |
| 18 |
Folder string `opt:"..." complete:"CompleteFolderAndNotmuch" desc:"Folder name."` |
| 19 |
} |
| 20 |
|
| 21 |
func init() { |
| 22 |
commands.Register(ChangeFolder{}) |
| 23 |
} |
| 24 |
|
| 25 |
func (ChangeFolder) Description() string { |
| 26 |
return "Change the folder shown in the message list." |
| 27 |
} |
| 28 |
|
| 29 |
func (ChangeFolder) Context() commands.CommandContext { |
| 30 |
return commands.MESSAGE_LIST |
| 31 |
} |
| 32 |
|
| 33 |
func (ChangeFolder) Aliases() []string { |
| 34 |
return []string{"cf"} |
| 35 |
} |
| 36 |
|
| 37 |
func (c *ChangeFolder) CompleteAccount(arg string) []string { |
| 38 |
return commands.FilterList(app.AccountNames(), arg, nil) |
| 39 |
} |
| 40 |
|
| 41 |
func (c *ChangeFolder) CompleteFolderAndNotmuch(arg string) []string { |
| 42 |
acct := app.SelectedAccount() |
| 43 |
if acct == nil { |
| 44 |
return nil |
| 45 |
} |
| 46 |
retval := commands.FilterList( |
| 47 |
acct.Directories().List(), arg, |
| 48 |
func(s string) string { |
| 49 |
dir := acct.Directories().Directory(s) |
| 50 |
if dir != nil && dir.Role != models.QueryRole { |
| 51 |
s = commands.QuoteSpace(s) |
| 52 |
} else { |
| 53 |
s += " " |
| 54 |
} |
| 55 |
return s |
| 56 |
}, |
| 57 |
) |
| 58 |
if acct.AccountConfig().Backend == "notmuch" { |
| 59 |
notmuchcomps := handleNotmuchComplete(arg) |
| 60 |
for _, prefix := range notmuch_search_terms { |
| 61 |
if strings.HasPrefix(arg, prefix) { |
| 62 |
return notmuchcomps |
| 63 |
} |
| 64 |
} |
| 65 |
retval = append(retval, notmuchcomps...) |
| 66 |
|
| 67 |
} |
| 68 |
return retval |
| 69 |
} |
| 70 |
|
| 71 |
func (c ChangeFolder) Execute([]string) error { |
| 72 |
var target string |
| 73 |
var acct *app.AccountView |
| 74 |
var err error |
| 75 |
|
| 76 |
args := opt.LexArgs(c.Folder) |
| 77 |
|
| 78 |
if c.Account != "" { |
| 79 |
acct, err = app.Account(c.Account) |
| 80 |
if err != nil { |
| 81 |
return err |
| 82 |
} |
| 83 |
} else { |
| 84 |
acct = app.SelectedAccount() |
| 85 |
if acct == nil { |
| 86 |
return errors.New("No account selected") |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
if args.Count() == 0 { |
| 91 |
return errors.New("<folder> is required. Usage: cf [-a <account>] <folder>") |
| 92 |
} |
| 93 |
|
| 94 |
if acct.AccountConfig().Backend == "notmuch" { |
| 95 |
// With notmuch, :cf can change to a "dynamic folder" that |
| 96 |
// contains the result of a query. Preserve the entered |
| 97 |
// arguments verbatim. |
| 98 |
target = args.String() |
| 99 |
// Strip trailing space |
| 100 |
target = strings.TrimSuffix(target, " ") |
| 101 |
} else { |
| 102 |
if args.Count() != 1 { |
| 103 |
return errors.New("Unexpected argument(s). Usage: cf [-a <account>] <folder>") |
| 104 |
} |
| 105 |
target = args.Arg(0) |
| 106 |
} |
| 107 |
|
| 108 |
finalize := func(msg types.WorkerMessage) { |
| 109 |
handleDirOpenResponse(acct, msg) |
| 110 |
} |
| 111 |
|
| 112 |
dirlist := acct.Directories() |
| 113 |
if dirlist == nil { |
| 114 |
return errors.New("No directory list found") |
| 115 |
} |
| 116 |
|
| 117 |
if target == "-" { |
| 118 |
dir := dirlist.Previous() |
| 119 |
if dir != "" { |
| 120 |
target = dir |
| 121 |
} else { |
| 122 |
return errors.New("No previous folder to return to") |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
dirlist.Open(target, "", 0*time.Second, finalize, false) |
| 127 |
|
| 128 |
return nil |
| 129 |
} |
| 130 |
|
| 131 |
func handleDirOpenResponse(acct *app.AccountView, msg types.WorkerMessage) { |
| 132 |
// As we're waiting for the worker to report status we must run |
| 133 |
// the rest of the actions in this callback. |
| 134 |
switch msg := msg.(type) { |
| 135 |
case *types.Error: |
| 136 |
app.PushError(msg.Error.Error()) |
| 137 |
case *types.Done: |
| 138 |
// reset store filtering if we switched folders |
| 139 |
store := acct.Store() |
| 140 |
if store != nil { |
| 141 |
store.ApplyClear() |
| 142 |
acct.SetStatus(state.SearchFilterClear()) |
| 143 |
} |
| 144 |
// focus account tab |
| 145 |
acct.Select() |
| 146 |
} |
| 147 |
} |