From 513e8aa94b5a95698453250bc514b333b1768a6c Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Fri, 15 Mar 2019 10:47:09 -0400 Subject: [PATCH] Rename :cd -> :cf, add :cd --- commands/cd.go | 32 +++++++++++++++++++++----------- commands/cf.go | 35 +++++++++++++++++++++++++++++++++++ config/aerc.conf | 2 +- go.mod | 1 + go.sum | 2 ++ 5 files changed, 60 insertions(+), 12 deletions(-) create mode 100644 commands/cf.go 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] } - history[acct.Name()] = previous - return nil + target, err = homedir.Expand(target) + if err != nil { + return err + } + if err := os.Chdir(target); err == nil { + previousDir = cwd + } + 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 +} diff --git a/config/aerc.conf b/config/aerc.conf index f9c91b8..30953fc 100644 --- a/config/aerc.conf +++ b/config/aerc.conf @@ -125,7 +125,7 @@ h = :prev-account = :view-message d = :confirm 'Really delete this message?' ':delete-message' -c = :cd +c = :cf $ = :term-exec [mbinds] diff --git a/go.mod b/go.mod index 5eae665..13a1259 100644 --- a/go.mod +++ b/go.mod @@ -12,6 +12,7 @@ require ( github.com/lucasb-eyer/go-colorful v0.0.0-20180531031333-d9cec903b20c github.com/mattn/go-isatty v0.0.3 github.com/mattn/go-runewidth v0.0.2 + github.com/mitchellh/go-homedir v1.1.0 github.com/nsf/termbox-go v0.0.0-20180129072728-88b7b944be8b github.com/stretchr/testify v1.3.0 golang.org/x/text v0.3.0 diff --git a/go.sum b/go.sum index 1278e7a..99e88d9 100644 --- a/go.sum +++ b/go.sum @@ -24,6 +24,8 @@ github.com/mattn/go-isatty v0.0.3 h1:ns/ykhmWi7G9O+8a448SecJU3nSMBXJfqQkl0upE1jI github.com/mattn/go-isatty v0.0.3/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4= github.com/mattn/go-runewidth v0.0.2 h1:UnlwIPBGaTZfPQ6T1IGzPI0EkYAQmT9fAEJ/poFC63o= github.com/mattn/go-runewidth v0.0.2/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU= +github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y= +github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0= github.com/nsf/termbox-go v0.0.0-20180129072728-88b7b944be8b/go.mod h1:IuKpRQcYE1Tfu+oAQqaLisqDeXgjyyltCfsaoYN18NQ= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=