From 910bc95af6e8cee59ab48cb5df2e7bea85b5c94a Mon Sep 17 00:00:00 2001 From: Koni Marti Date: Fri, 14 Jun 2024 23:13:59 +0200 Subject: dirlist: store previous folder info Store the previous folder in the dirlist and retire the global 'history' map in the commands package. This ensures that the previous folder is always available when using ':cf -'. Signed-off-by: Koni Marti Acked-by: Robin Jarry --- commands/account/cf.go | 20 ++++++++++---------- commands/account/mkdir.go | 2 -- commands/account/rmdir.go | 3 ++- 3 files changed, 12 insertions(+), 13 deletions(-) (limited to 'commands') diff --git a/commands/account/cf.go b/commands/account/cf.go index 199664fa..8d7d27ae 100644 --- a/commands/account/cf.go +++ b/commands/account/cf.go @@ -13,15 +13,12 @@ import ( "git.sr.ht/~rjarry/go-opt" ) -var history map[string]string - type ChangeFolder struct { Account bool `opt:"-a"` Folder string `opt:"..." complete:"CompleteFolderAndNotmuch"` } func init() { - history = make(map[string]string) commands.Register(ChangeFolder{}) } @@ -115,16 +112,22 @@ func (c ChangeFolder) Execute([]string) error { handleDirOpenResponse(acct, msg) } + dirlist := acct.Directories() + if dirlist == nil { + return errors.New("No directory list found") + } + if target == "-" { - if dir, ok := history[acct.Name()]; ok { - acct.Directories().Open(dir, "", 0*time.Second, finalize, false) + dir := dirlist.Previous() + if dir != "" { + target = dir } else { return errors.New("No previous folder to return to") } - } else { - acct.Directories().Open(target, "", 0*time.Second, finalize, false) } + dirlist.Open(target, "", 0*time.Second, finalize, false) + return nil } @@ -135,9 +138,6 @@ func handleDirOpenResponse(acct *app.AccountView, msg types.WorkerMessage) { case *types.Error: app.PushError(msg.Error.Error()) case *types.Done: - curAccount := app.SelectedAccount() - previous := curAccount.Directories().Selected() - history[curAccount.Name()] = previous // reset store filtering if we switched folders store := acct.Store() if store != nil { diff --git a/commands/account/mkdir.go b/commands/account/mkdir.go index 5ce7bbf4..13310665 100644 --- a/commands/account/mkdir.go +++ b/commands/account/mkdir.go @@ -45,14 +45,12 @@ func (m MakeDir) Execute(args []string) error { if acct == nil { return errors.New("No account selected") } - previous := acct.SelectedDirectory() acct.Worker().PostAction(&types.CreateDirectory{ Directory: m.Folder, }, func(msg types.WorkerMessage) { switch msg := msg.(type) { case *types.Done: app.PushStatus("Directory created.", 10*time.Second) - history[acct.Name()] = previous acct.Directories().Open(m.Folder, "", 0, nil, false) case *types.Error: app.PushError(msg.Error.Error()) diff --git a/commands/account/rmdir.go b/commands/account/rmdir.go index cd70aa62..ff1463b6 100644 --- a/commands/account/rmdir.go +++ b/commands/account/rmdir.go @@ -72,7 +72,8 @@ func (r RemoveDir) Execute(args []string) error { var newDir string dirFound := false - if oldDir, ok := history[acct.Name()]; ok { + oldDir := acct.Directories().Previous() + if oldDir != "" { present := false for _, dir := range acct.Directories().List() { if dir == oldDir { -- cgit