From 513e8aa94b5a95698453250bc514b333b1768a6c Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Fri, 15 Mar 2019 10:47:09 -0400 Subject: Rename :cd -> :cf, add :cd --- commands/cd.go | 32 +++++++++++++++++++++----------- commands/cf.go | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 11 deletions(-) create mode 100644 commands/cf.go (limited to 'commands') 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 ") + return errors.New("Usage: cf ") } - 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 } diff --git a/commands/cf.go b/commands/cf.go new file mode 100644 index 0000000..83b757c --- /dev/null +++ b/commands/cf.go @@ -0,0 +1,35 @@ +package commands + +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() + 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 +} -- cgit v1.2.3