forked from elves/elvish
/
builtin_fn_fs.go
84 lines (69 loc) · 1.58 KB
/
builtin_fn_fs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package eval
import (
"errors"
"os"
"path/filepath"
"github.com/elves/elvish/eval/types"
"github.com/elves/elvish/store/storedefs"
"github.com/elves/elvish/util"
)
// Filesystem.
var ErrStoreNotConnected = errors.New("store not connected")
func init() {
addBuiltinFns(map[string]interface{}{
// Directory
"cd": cd,
"dir-history": dirs,
// Path
"path-abs": filepath.Abs,
"path-base": filepath.Base,
"path-clean": filepath.Clean,
"path-dir": filepath.Dir,
"path-ext": filepath.Ext,
"eval-symlinks": filepath.EvalSymlinks,
"tilde-abbr": tildeAbbr,
// File types
"-is-dir": isDir,
})
}
func cd(fm *Frame, args ...string) error {
var dir string
switch len(args) {
case 0:
dir = mustGetHome("")
case 1:
dir = args[0]
default:
return ErrArgs
}
return Chdir(dir, fm.DaemonClient)
}
func cdInner(dir string, fm *Frame) {
maybeThrow(Chdir(dir, fm.DaemonClient))
}
var dirDescriptor = types.NewStructDescriptor("path", "score")
func newDirStruct(path string, score float64) *types.Struct {
return types.NewStruct(dirDescriptor,
[]interface{}{path, floatToElv(score)})
}
func dirs(ec *Frame) error {
if ec.DaemonClient == nil {
throw(ErrStoreNotConnected)
}
dirs, err := ec.DaemonClient.Dirs(storedefs.NoBlacklist)
if err != nil {
return err
}
out := ec.ports[1].Chan
for _, dir := range dirs {
out <- newDirStruct(dir.Path, dir.Score)
}
return nil
}
func tildeAbbr(path string) string {
return util.TildeAbbr(path)
}
func isDir(path string) bool {
fi, err := os.Stat(path)
return err == nil && fi.Mode().IsDir()
}