pkg/eval: De-export PwdVariable.

This commit is contained in:
Qi Xiao 2020-09-04 22:32:41 +01:00
parent 948432e766
commit a88ac31b39
3 changed files with 10 additions and 7 deletions

View File

@ -90,7 +90,6 @@ var builtinNs = Ns{
"true": vars.NewReadOnly(true),
"false": vars.NewReadOnly(false),
"paths": NewEnvListVar("PATH"),
"pwd": PwdVariable{},
"args": vars.NewReadOnly(vector.Empty),
}

View File

@ -172,7 +172,7 @@ func NewEvaler() *Evaler {
builtin["num-bg-jobs"] = vars.FromGet(func() interface{} {
return strconv.Itoa(ev.state.getNumBgJobs())
})
builtin["pwd"] = PwdVariable{ev}
builtin["pwd"] = NewPwdVar(ev)
return ev
}

View File

@ -6,20 +6,24 @@ import (
"github.com/elves/elvish/pkg/eval/vars"
)
// PwdVariable is a variable whose value always reflects the current working
// NewPwdVar returns a variable who value is synchronized with the path of the
// current working directory.
func NewPwdVar(ev *Evaler) vars.Var { return pwdVar{ev} }
// pwdVar is a variable whose value always reflects the current working
// directory. Setting it changes the current working directory.
type PwdVariable struct {
type pwdVar struct {
ev *Evaler
}
var _ vars.Var = PwdVariable{}
var _ vars.Var = pwdVar{}
// Getwd allows for unit test error injection.
var getwd func() (string, error) = os.Getwd
// Get returns the current working directory. It returns "/unknown/pwd" when
// it cannot be determined.
func (PwdVariable) Get() interface{} {
func (pwdVar) Get() interface{} {
pwd, err := getwd()
if err != nil {
return "/unknown/pwd"
@ -28,7 +32,7 @@ func (PwdVariable) Get() interface{} {
}
// Set changes the current working directory.
func (pwd PwdVariable) Set(v interface{}) error {
func (pwd pwdVar) Set(v interface{}) error {
path, ok := v.(string)
if !ok {
return ErrPathMustBeString