github.com/markusbkk/elvish@v0.0.0-20231204143114-91dc52438621/pkg/eval/builtin_fn_fs.go (about)

     1  package eval
     2  
     3  import (
     4  	"github.com/markusbkk/elvish/pkg/eval/errs"
     5  	"github.com/markusbkk/elvish/pkg/fsutil"
     6  )
     7  
     8  // Filesystem commands.
     9  
    10  func init() {
    11  	addBuiltinFns(map[string]interface{}{
    12  		// Directory
    13  		"cd": cd,
    14  
    15  		// Path
    16  		"tilde-abbr": tildeAbbr,
    17  	})
    18  }
    19  
    20  //elvdoc:fn cd
    21  //
    22  // ```elvish
    23  // cd $dirname
    24  // ```
    25  //
    26  // Change directory. This affects the entire process; i.e., all threads
    27  // whether running indirectly (e.g., prompt functions) or started explicitly
    28  // by commands such as [`peach`](#peach).
    29  //
    30  // Note that Elvish's `cd` does not support `cd -`.
    31  //
    32  // @cf pwd
    33  
    34  func cd(fm *Frame, args ...string) error {
    35  	var dir string
    36  	switch len(args) {
    37  	case 0:
    38  		var err error
    39  		dir, err = fsutil.GetHome("")
    40  		if err != nil {
    41  			return err
    42  		}
    43  	case 1:
    44  		dir = args[0]
    45  	default:
    46  		return errs.ArityMismatch{What: "arguments", ValidLow: 0, ValidHigh: 1, Actual: len(args)}
    47  	}
    48  
    49  	return fm.Evaler.Chdir(dir)
    50  }
    51  
    52  //elvdoc:fn tilde-abbr
    53  //
    54  // ```elvish
    55  // tilde-abbr $path
    56  // ```
    57  //
    58  // If `$path` represents a path under the home directory, replace the home
    59  // directory with `~`. Examples:
    60  //
    61  // ```elvish-transcript
    62  // ~> echo $E:HOME
    63  // /Users/foo
    64  // ~> tilde-abbr /Users/foo
    65  // ▶ '~'
    66  // ~> tilde-abbr /Users/foobar
    67  // ▶ /Users/foobar
    68  // ~> tilde-abbr /Users/foo/a/b
    69  // ▶ '~/a/b'
    70  // ```
    71  
    72  func tildeAbbr(path string) string {
    73  	return fsutil.TildeAbbr(path)
    74  }