mirror of
https://github.com/go-sylixos/elvish.git
synced 2024-12-15 03:37:52 +08:00
07fa612759
All the initialization steps that were related to the args are not optional, and can be done separately by calling the relevant methods of the Evaler.
37 lines
825 B
Go
37 lines
825 B
Go
package eval
|
|
|
|
import (
|
|
"os"
|
|
)
|
|
|
|
// AddDirer wraps the AddDir function.
|
|
type AddDirer interface {
|
|
// AddDir adds a directory with the given weight to some storage.
|
|
AddDir(dir string, weight float64) error
|
|
}
|
|
|
|
// Chdir changes the current directory. On success it also updates the PWD
|
|
// environment variable and records the new directory in the directory history.
|
|
// It returns nil as long as the directory changing part succeeds.
|
|
func Chdir(path string, store AddDirer) error {
|
|
err := os.Chdir(path)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
pwd, err := os.Getwd()
|
|
if err != nil {
|
|
logger.Println("getwd after cd:", err)
|
|
return nil
|
|
}
|
|
os.Setenv("PWD", pwd)
|
|
if store != nil {
|
|
go func() {
|
|
err := store.AddDir(pwd, 1)
|
|
if err != nil {
|
|
logger.Println("Failed to save dir to history:", err)
|
|
}
|
|
}()
|
|
}
|
|
return nil
|
|
}
|