mirror of
https://github.com/go-sylixos/elvish.git
synced 2024-12-05 03:17:50 +08:00
Add $pwd.
This commit is contained in:
parent
a880b74a1b
commit
160e1456d3
|
@ -60,6 +60,7 @@ func NewEvaler(st *store.Store) *Evaler {
|
|||
"true": NewRoVariable(Bool(true)),
|
||||
"false": NewRoVariable(Bool(false)),
|
||||
"paths": &EnvPathList{envName: "PATH"},
|
||||
"pwd": PwdVariable{},
|
||||
}
|
||||
for _, b := range builtinFns {
|
||||
ev.global[FnPrefix+b.Name] = NewRoVariable(b)
|
||||
|
|
24
eval/pwd.go
Normal file
24
eval/pwd.go
Normal file
|
@ -0,0 +1,24 @@
|
|||
package eval
|
||||
|
||||
import "os"
|
||||
|
||||
// PwdVariable is a variable whose value always reflects the current working
|
||||
// directory. Setting it changes the current working directory.
|
||||
type PwdVariable struct{}
|
||||
|
||||
var _ Variable = PwdVariable{}
|
||||
|
||||
func (PwdVariable) Get() Value {
|
||||
pwd, err := os.Getwd()
|
||||
maybeThrow(err)
|
||||
return String(pwd)
|
||||
}
|
||||
|
||||
func (PwdVariable) Set(v Value) {
|
||||
path, ok := v.(String)
|
||||
if !ok {
|
||||
throw(ErrPathMustBeString)
|
||||
}
|
||||
err := os.Chdir(string(path))
|
||||
maybeThrow(err)
|
||||
}
|
Loading…
Reference in New Issue
Block a user