Rename :cd -> :cf, add :cd

This commit is contained in:
Drew DeVault 2019-03-15 10:47:09 -04:00
parent 9e3b602ce7
commit 513e8aa94b
5 changed files with 60 additions and 12 deletions

View file

@ -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]
}
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
}

35
commands/cf.go Normal file
View file

@ -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 <folder>")
}
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
}

View file

@ -125,7 +125,7 @@ h = :prev-account<Enter>
<Enter> = :view-message<Enter>
d = :confirm 'Really delete this message?' ':delete-message<Enter>'<Enter>
c = :cd<space>
c = :cf<space>
$ = :term-exec
[mbinds]

1
go.mod
View file

@ -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

2
go.sum
View file

@ -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=