-
-
Notifications
You must be signed in to change notification settings - Fork 298
/
builtin_fn_fs.go
80 lines (66 loc) · 1.49 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
package eval
import (
"errors"
"os"
"path/filepath"
"github.com/elves/elvish/eval/vals"
"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 fm.Chdir(dir)
}
var dirDescriptor = vals.NewStructDescriptor("path", "score")
func newDirStruct(path string, score float64) *vals.Struct {
return vals.NewStruct(dirDescriptor,
[]interface{}{path, vals.FromGo(score)})
}
func dirs(fm *Frame) error {
if fm.DaemonClient == nil {
return ErrStoreNotConnected
}
dirs, err := fm.DaemonClient.Dirs(storedefs.NoBlacklist)
if err != nil {
return err
}
out := fm.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()
}