2017-12-17 13:20:03 +08:00
|
|
|
package eval
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
|
2021-01-27 09:28:38 +08:00
|
|
|
"src.elv.sh/pkg/fsutil"
|
|
|
|
"src.elv.sh/pkg/store"
|
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
|
|
|
|
2019-04-19 05:15:34 +08:00
|
|
|
// ErrStoreNotConnected is thrown by dir-history when the store is not connected.
|
2017-12-17 13:20:03 +08:00
|
|
|
var ErrStoreNotConnected = errors.New("store not connected")
|
|
|
|
|
2020-01-18 21:12:50 +08:00
|
|
|
//elvdoc:fn path-\*
|
|
|
|
//
|
|
|
|
// ```elvish
|
|
|
|
// path-abs $path
|
|
|
|
// path-base $path
|
|
|
|
// path-clean $path
|
|
|
|
// path-dir $path
|
|
|
|
// path-ext $path
|
|
|
|
// ```
|
|
|
|
//
|
|
|
|
// See [godoc of path/filepath](https://godoc.org/path/filepath). Go errors are
|
|
|
|
// turned into exceptions.
|
2021-01-17 07:21:33 +08:00
|
|
|
//
|
|
|
|
// These functions are deprecated. Use the equivalent functions in the
|
|
|
|
// [path:](path.html) module.
|
2020-01-18 21:12:50 +08:00
|
|
|
|
2017-12-17 13:20:03 +08:00
|
|
|
func init() {
|
2018-02-07 03:39:40 +08:00
|
|
|
addBuiltinFns(map[string]interface{}{
|
2017-12-17 13:20:03 +08:00
|
|
|
// Directory
|
2018-02-04 14:30:36 +08:00
|
|
|
"cd": cd,
|
|
|
|
"dir-history": dirs,
|
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
|
2020-09-03 12:27:18 +08:00
|
|
|
dir, err = fsutil.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:
|
|
|
|
return ErrArgs
|
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 dir-history
|
|
|
|
//
|
|
|
|
// ```elvish
|
|
|
|
// dir-history
|
|
|
|
// ```
|
|
|
|
//
|
2021-05-07 07:01:33 +08:00
|
|
|
// Return a list containing the interactive directory history. Each element is a map with two keys:
|
|
|
|
// `path` and `score`. The list is sorted by descending score.
|
2020-08-17 01:46:29 +08:00
|
|
|
//
|
|
|
|
// Example:
|
|
|
|
//
|
|
|
|
// ```elvish-transcript
|
|
|
|
// ~> dir-history | take 1
|
|
|
|
// ▶ [&path=/Users/foo/.elvish &score=96.79928]
|
|
|
|
// ```
|
2021-05-07 07:01:33 +08:00
|
|
|
//
|
|
|
|
// @cf edit:command-history
|
2020-08-17 01:46:29 +08:00
|
|
|
|
2019-04-20 01:00:40 +08:00
|
|
|
type dirHistoryEntry struct {
|
2020-06-27 18:45:11 +08:00
|
|
|
Path string
|
|
|
|
Score float64
|
2018-01-03 08:43:27 +08:00
|
|
|
}
|
|
|
|
|
2020-06-27 18:45:11 +08:00
|
|
|
func (dirHistoryEntry) IsStructMap() {}
|
2019-04-20 01:00:40 +08:00
|
|
|
|
2018-03-01 10:17:56 +08:00
|
|
|
func dirs(fm *Frame) error {
|
2021-01-05 07:54:13 +08:00
|
|
|
daemon := fm.Evaler.DaemonClient()
|
|
|
|
if daemon == nil {
|
2018-03-01 23:13:00 +08:00
|
|
|
return ErrStoreNotConnected
|
2017-12-17 13:20:03 +08:00
|
|
|
}
|
2021-01-05 07:54:13 +08:00
|
|
|
dirs, err := daemon.Dirs(store.NoBlacklist)
|
2017-12-17 13:20:03 +08:00
|
|
|
if err != nil {
|
2018-02-04 14:30:36 +08:00
|
|
|
return err
|
2017-12-17 13:20:03 +08:00
|
|
|
}
|
2020-09-05 06:07:04 +08:00
|
|
|
out := fm.OutputChan()
|
2017-12-17 13:20:03 +08:00
|
|
|
for _, dir := range dirs {
|
2019-04-20 01:00:40 +08:00
|
|
|
out <- dirHistoryEntry{dir.Path, dir.Score}
|
2017-12-17 13:20:03 +08:00
|
|
|
}
|
2018-02-04 14:30:36 +08:00
|
|
|
return nil
|
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
|
|
|
}
|