-
Notifications
You must be signed in to change notification settings - Fork 0
/
fs.elv
103 lines (82 loc) · 1.43 KB
/
fs.elv
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
use github.com/champii/elvish-base/utils
# fs utils
fn isFile [x]{
not (utils:has_failed { test -f $x })
}
fn isDir [x]{
not (utils:has_failed { test -d $x })
}
fn type [x]{
if (isFile $x) {
put 'file'
} else {
put 'dir'
}
}
fn kilo [x]{
* $x 1024
}
fn mega [x]{
* (kilo $x) 1024
}
fn giga [x]{
* (mega $x) 1024
}
# files
fn exists [filename]{
not (utils:has_failed { stat $filename })
}
fn size [filename]{
set os = (uname -s)
if (==s $os "Linux") {
put (stat --printf="%s" $filename)
} else {
put (stat -f %z $filename)
}
}
fn pretty_size [filename]{
i = 0
x = (size $filename)
while (>= $x 1024) {
i = (+ $i 1)
x = (/ $x 1024)
}
x = (utils:floor $x)
if (eq $i 1) {
put $x"Ko"
} elif (eq $i 2) {
put $x"Mo"
} elif (eq $i 3) {
put $x"Go"
} else {
put $x
}
}
fn zero [filename]{
== (size $filename) 0
}
fn zero_or_null [filename]{
not (and (exists $filename) (not (zero $filename)))
}
fn list_empty {
@list = (e:ls)
utils:filter [x]{ eq (size $x) 0 } $list
}
fn list_gt [s]{
@list = (e:ls)
utils:filter [x]{ >= (size $x) $s } $list
}
fn File [path]{
fileObj = [
&path=(path:abs $path)
&type=(type (path:abs $path))
&cat~={ e:cat $fileObj[path] }
&size~={ pretty_size $fileObj[path]}
&infos~={
echo Path: $fileObj[path]
echo Type: $fileObj[type]
echo Size: ($fileObj[size~])
}
]
put $fileObj
}