2016-02-21 19:58:20 +08:00
|
|
|
package eval
|
|
|
|
|
2017-06-25 00:08:47 +08:00
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/elves/elvish/daemon/api"
|
|
|
|
)
|
2016-02-21 19:58:20 +08:00
|
|
|
|
|
|
|
// PwdVariable is a variable whose value always reflects the current working
|
|
|
|
// directory. Setting it changes the current working directory.
|
2017-06-25 00:08:47 +08:00
|
|
|
type PwdVariable struct {
|
|
|
|
daemon *api.Client
|
|
|
|
}
|
2016-02-21 19:58:20 +08:00
|
|
|
|
|
|
|
var _ Variable = PwdVariable{}
|
|
|
|
|
|
|
|
func (PwdVariable) Get() Value {
|
|
|
|
pwd, err := os.Getwd()
|
|
|
|
maybeThrow(err)
|
|
|
|
return String(pwd)
|
|
|
|
}
|
|
|
|
|
2017-06-25 00:08:47 +08:00
|
|
|
func (pwd PwdVariable) Set(v Value) {
|
2016-02-21 19:58:20 +08:00
|
|
|
path, ok := v.(String)
|
|
|
|
if !ok {
|
|
|
|
throw(ErrPathMustBeString)
|
|
|
|
}
|
2017-06-25 00:08:47 +08:00
|
|
|
err := Chdir(string(path), pwd.daemon)
|
2016-02-21 19:58:20 +08:00
|
|
|
maybeThrow(err)
|
|
|
|
}
|