Skip to content

Commit

Permalink
dirlist: store previous folder info
Browse files Browse the repository at this point in the history
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 <[email protected]>
Acked-by: Robin Jarry <[email protected]>
  • Loading branch information
konimarti authored and rjarry committed Jun 23, 2024
1 parent 2c06df8 commit 910bc95
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 13 deletions.
8 changes: 8 additions & 0 deletions app/dirlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ type DirectoryLister interface {
ui.Drawable

Selected() string
Previous() string

Select(string)
Open(string, string, time.Duration, func(types.WorkerMessage), bool)

Expand Down Expand Up @@ -56,6 +58,7 @@ type DirectoryList struct {
dirs []string
selecting string
selected string
previous string
spinner *Spinner
worker *types.Worker
ctx context.Context
Expand Down Expand Up @@ -112,6 +115,7 @@ func (dirlist *DirectoryList) Update(msg types.WorkerMessage) {
case *types.Done:
switch msg := msg.InResponseTo().(type) {
case *types.OpenDirectory:
dirlist.previous = dirlist.selected
dirlist.selected = msg.Directory
dirlist.filterDirsByFoldersConfig()
hasSelected := false
Expand Down Expand Up @@ -222,6 +226,10 @@ func (dirlist *DirectoryList) Selected() string {
return dirlist.selected
}

func (dirlist *DirectoryList) Previous() string {
return dirlist.previous
}

func (dirlist *DirectoryList) Invalidate() {
ui.Invalidate()
}
Expand Down
20 changes: 10 additions & 10 deletions commands/account/cf.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{})
}

Expand Down Expand Up @@ -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
}

Expand All @@ -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 {
Expand Down
2 changes: 0 additions & 2 deletions commands/account/mkdir.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
3 changes: 2 additions & 1 deletion commands/account/rmdir.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 910bc95

Please sign in to comment.