2017-12-17 13:20:03 +08:00
|
|
|
package eval
|
|
|
|
|
|
|
|
import (
|
2021-06-04 10:51:38 +08:00
|
|
|
"src.elv.sh/pkg/eval/errs"
|
2022-01-03 07:23:52 +08:00
|
|
|
"src.elv.sh/pkg/fsutil"
|
2017-12-17 13:20:03 +08:00
|
|
|
)
|
|
|
|
|
2020-08-17 01:46:29 +08:00
|
|
|
// Filesystem commands.
|
2017-12-17 13:20:03 +08:00
|
|
|
|
|
|
|
func init() {
|
2022-03-20 23:50:25 +08:00
|
|
|
addBuiltinFns(map[string]any{
|
2017-12-17 13:20:03 +08:00
|
|
|
// Directory
|
2022-01-03 07:23:52 +08:00
|
|
|
"cd": cd,
|
2017-12-17 13:20:03 +08:00
|
|
|
|
|
|
|
// Path
|
2021-04-09 06:07:50 +08:00
|
|
|
"tilde-abbr": tildeAbbr,
|
2017-12-17 13:20:03 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-02-04 14:30:36 +08:00
|
|
|
func cd(fm *Frame, args ...string) error {
|
2017-12-17 13:20:03 +08:00
|
|
|
var dir string
|
2018-02-04 14:30:36 +08:00
|
|
|
switch len(args) {
|
|
|
|
case 0:
|
2018-09-28 05:19:47 +08:00
|
|
|
var err error
|
2022-06-20 06:34:36 +08:00
|
|
|
dir, err = getHome("")
|
2018-09-28 05:19:47 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-02-04 14:30:36 +08:00
|
|
|
case 1:
|
|
|
|
dir = args[0]
|
|
|
|
default:
|
2021-06-04 10:51:38 +08:00
|
|
|
return errs.ArityMismatch{What: "arguments", ValidLow: 0, ValidHigh: 1, Actual: len(args)}
|
2017-12-17 13:20:03 +08:00
|
|
|
}
|
|
|
|
|
2021-01-05 07:54:13 +08:00
|
|
|
return fm.Evaler.Chdir(dir)
|
2017-12-17 13:20:03 +08:00
|
|
|
}
|
|
|
|
|
2018-02-04 14:30:36 +08:00
|
|
|
func tildeAbbr(path string) string {
|
2020-09-03 12:27:18 +08:00
|
|
|
return fsutil.TildeAbbr(path)
|
2017-12-17 13:20:03 +08:00
|
|
|
}
|