2019-03-15 03:34:34 +01:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2019-03-15 15:47:09 +01:00
|
|
|
"os"
|
2019-03-15 03:34:34 +01:00
|
|
|
|
2019-05-18 02:57:10 +02:00
|
|
|
"git.sr.ht/~sircmpwn/aerc/widgets"
|
2019-03-15 15:47:09 +01:00
|
|
|
"github.com/mitchellh/go-homedir"
|
2019-03-15 03:34:34 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2019-03-15 15:47:09 +01:00
|
|
|
previousDir string
|
2019-03-15 03:34:34 +01:00
|
|
|
)
|
|
|
|
|
2019-06-27 19:33:11 +02:00
|
|
|
type ChangeDirectory struct{}
|
|
|
|
|
2019-03-15 03:34:34 +01:00
|
|
|
func init() {
|
2019-06-27 19:33:11 +02:00
|
|
|
register(ChangeDirectory{})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (_ ChangeDirectory) Aliases() []string {
|
|
|
|
return []string{"cd"}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (_ ChangeDirectory) Complete(aerc *widgets.Aerc, args []string) []string {
|
|
|
|
return nil
|
2019-03-15 03:34:34 +01:00
|
|
|
}
|
|
|
|
|
2019-06-27 19:33:11 +02:00
|
|
|
func (_ ChangeDirectory) Execute(aerc *widgets.Aerc, args []string) error {
|
2019-06-05 08:58:40 +02:00
|
|
|
if len(args) < 1 || len(args) > 2 {
|
|
|
|
return errors.New("Usage: cd [directory]")
|
2019-03-15 03:34:34 +01:00
|
|
|
}
|
2019-03-15 15:47:09 +01:00
|
|
|
cwd, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
var target string
|
2019-06-05 08:58:40 +02:00
|
|
|
if len(args) == 1 {
|
|
|
|
target = "~"
|
|
|
|
} else if args[1] == "-" {
|
2019-03-15 15:47:09 +01:00
|
|
|
if previousDir == "" {
|
|
|
|
return errors.New("No previous folder to return to")
|
2019-03-15 03:34:34 +01:00
|
|
|
} else {
|
2019-03-15 15:47:09 +01:00
|
|
|
target = previousDir
|
2019-03-15 03:34:34 +01:00
|
|
|
}
|
|
|
|
} else {
|
2019-03-15 15:47:09 +01:00
|
|
|
target = args[1]
|
|
|
|
}
|
|
|
|
target, err = homedir.Expand(target)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := os.Chdir(target); err == nil {
|
|
|
|
previousDir = cwd
|
2019-03-15 03:34:34 +01:00
|
|
|
}
|
2019-03-15 15:47:09 +01:00
|
|
|
return err
|
2019-03-15 03:34:34 +01:00
|
|
|
}
|