diff options
author | Drew DeVault <sir@cmpwn.com> | 2019-03-15 10:47:09 -0400 |
---|---|---|
committer | Drew DeVault <sir@cmpwn.com> | 2019-03-15 10:47:09 -0400 |
commit | 513e8aa94b5a95698453250bc514b333b1768a6c (patch) | |
tree | 6c988566c3d247a189a537d85b47245b8b6d57bb /commands/cd.go | |
parent | 9e3b602ce7317c18a73d228fda7eb476b18a8988 (diff) | |
download | aerc-513e8aa94b5a95698453250bc514b333b1768a6c.tar.gz |
Rename :cd -> :cf, add :cd
Diffstat (limited to 'commands/cd.go')
-rw-r--r-- | commands/cd.go | 32 |
1 files changed, 21 insertions, 11 deletions
diff --git a/commands/cd.go b/commands/cd.go index e3a1953..52d5b49 100644 --- a/commands/cd.go +++ b/commands/cd.go @@ -2,34 +2,44 @@ package commands import ( "errors" + "os" "git.sr.ht/~sircmpwn/aerc2/widgets" + "github.com/mitchellh/go-homedir" ) var ( - history map[string]string + previousDir string ) func init() { - history = make(map[string]string) Register("cd", ChangeDirectory) } func ChangeDirectory(aerc *widgets.Aerc, args []string) error { if len(args) != 2 { - return errors.New("Usage: cd <directory>") + return errors.New("Usage: cf <directory>") } - acct := aerc.SelectedAccount() - previous := acct.Directories().Selected() + cwd, err := os.Getwd() + if err != nil { + return err + } + var target string if args[1] == "-" { - if dir, ok := history[acct.Name()]; ok { - acct.Directories().Select(dir) + if previousDir == "" { + return errors.New("No previous folder to return to") } else { - return errors.New("No previous directory to return to") + target = previousDir } } else { - acct.Directories().Select(args[1]) + target = args[1] + } + target, err = homedir.Expand(target) + if err != nil { + return err + } + if err := os.Chdir(target); err == nil { + previousDir = cwd } - history[acct.Name()] = previous - return nil + return err } |