github.com/kolbycrouch/elvish@v0.14.1-0.20210614162631-215b9ac1c423/pkg/eval/builtin_fn_fs.go (about)

     1  package eval
     2  
     3  import (
     4  	"errors"
     5  
     6  	"src.elv.sh/pkg/fsutil"
     7  	"src.elv.sh/pkg/store"
     8  )
     9  
    10  // Filesystem commands.
    11  
    12  // ErrStoreNotConnected is thrown by dir-history when the store is not connected.
    13  var ErrStoreNotConnected = errors.New("store not connected")
    14  
    15  //elvdoc:fn path-\*
    16  //
    17  // ```elvish
    18  // path-abs $path
    19  // path-base $path
    20  // path-clean $path
    21  // path-dir $path
    22  // path-ext $path
    23  // ```
    24  //
    25  // See [godoc of path/filepath](https://godoc.org/path/filepath). Go errors are
    26  // turned into exceptions.
    27  //
    28  // These functions are deprecated. Use the equivalent functions in the
    29  // [path:](path.html) module.
    30  
    31  func init() {
    32  	addBuiltinFns(map[string]interface{}{
    33  		// Directory
    34  		"cd":          cd,
    35  		"dir-history": dirs,
    36  
    37  		// Path
    38  		"tilde-abbr": tildeAbbr,
    39  	})
    40  }
    41  
    42  //elvdoc:fn cd
    43  //
    44  // ```elvish
    45  // cd $dirname
    46  // ```
    47  //
    48  // Change directory. This affects the entire process; i.e., all threads
    49  // whether running indirectly (e.g., prompt functions) or started explicitly
    50  // by commands such as [`peach`](#peach).
    51  //
    52  // Note that Elvish's `cd` does not support `cd -`.
    53  //
    54  // @cf pwd
    55  
    56  func cd(fm *Frame, args ...string) error {
    57  	var dir string
    58  	switch len(args) {
    59  	case 0:
    60  		var err error
    61  		dir, err = fsutil.GetHome("")
    62  		if err != nil {
    63  			return err
    64  		}
    65  	case 1:
    66  		dir = args[0]
    67  	default:
    68  		return ErrArgs
    69  	}
    70  
    71  	return fm.Evaler.Chdir(dir)
    72  }
    73  
    74  //elvdoc:fn dir-history
    75  //
    76  // ```elvish
    77  // dir-history
    78  // ```
    79  //
    80  // Return a list containing the interactive directory history. Each element is a map with two keys:
    81  // `path` and `score`. The list is sorted by descending score.
    82  //
    83  // Example:
    84  //
    85  // ```elvish-transcript
    86  // ~> dir-history | take 1
    87  // ▶ [&path=/Users/foo/.elvish &score=96.79928]
    88  // ```
    89  //
    90  // @cf edit:command-history
    91  
    92  type dirHistoryEntry struct {
    93  	Path  string
    94  	Score float64
    95  }
    96  
    97  func (dirHistoryEntry) IsStructMap() {}
    98  
    99  func dirs(fm *Frame) error {
   100  	daemon := fm.Evaler.DaemonClient()
   101  	if daemon == nil {
   102  		return ErrStoreNotConnected
   103  	}
   104  	dirs, err := daemon.Dirs(store.NoBlacklist)
   105  	if err != nil {
   106  		return err
   107  	}
   108  	out := fm.OutputChan()
   109  	for _, dir := range dirs {
   110  		out <- dirHistoryEntry{dir.Path, dir.Score}
   111  	}
   112  	return nil
   113  }
   114  
   115  //elvdoc:fn tilde-abbr
   116  //
   117  // ```elvish
   118  // tilde-abbr $path
   119  // ```
   120  //
   121  // If `$path` represents a path under the home directory, replace the home
   122  // directory with `~`. Examples:
   123  //
   124  // ```elvish-transcript
   125  // ~> echo $E:HOME
   126  // /Users/foo
   127  // ~> tilde-abbr /Users/foo
   128  // ▶ '~'
   129  // ~> tilde-abbr /Users/foobar
   130  // ▶ /Users/foobar
   131  // ~> tilde-abbr /Users/foo/a/b
   132  // ▶ '~/a/b'
   133  // ```
   134  
   135  func tildeAbbr(path string) string {
   136  	return fsutil.TildeAbbr(path)
   137  }