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
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-08-17 01:46:29 +08:00
|
|
|
//elvdoc:fn cd
|
|
|
|
//
|
|
|
|
// ```elvish
|
|
|
|
// cd $dirname
|
|
|
|
// ```
|
|
|
|
//
|
2020-08-17 03:18:20 +08:00
|
|
|
// Change directory. This affects the entire process; i.e., all threads
|
|
|
|
// whether running indirectly (e.g., prompt functions) or started explicitly
|
|
|
|
// by commands such as [`peach`](#peach).
|
2020-08-17 01:46:29 +08:00
|
|
|
//
|
|
|
|
// Note that Elvish's `cd` does not support `cd -`.
|
2020-08-17 03:18:20 +08:00
|
|
|
//
|
|
|
|
// @cf pwd
|
2020-08-17 01:46:29 +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
|
|
|
}
|
|
|
|
|
2020-08-17 01:46:29 +08:00
|
|
|
//elvdoc:fn tilde-abbr
|
|
|
|
//
|
|
|
|
// ```elvish
|
|
|
|
// tilde-abbr $path
|
|
|
|
// ```
|
|
|
|
//
|
|
|
|
// If `$path` represents a path under the home directory, replace the home
|
|
|
|
// directory with `~`. Examples:
|
|
|
|
//
|
|
|
|
// ```elvish-transcript
|
|
|
|
// ~> echo $E:HOME
|
|
|
|
// /Users/foo
|
|
|
|
// ~> tilde-abbr /Users/foo
|
|
|
|
// ▶ '~'
|
|
|
|
// ~> tilde-abbr /Users/foobar
|
|
|
|
// ▶ /Users/foobar
|
|
|
|
// ~> tilde-abbr /Users/foo/a/b
|
|
|
|
// ▶ '~/a/b'
|
|
|
|
// ```
|
|
|
|
|
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
|
|
|
}
|