blob: b81fc95b83d2fd9e2e4bdc8c10b8c64e1e0ade43 (
plain)
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
|
# ls
# it has some features but not all of them obv
fn mode_string(type, mode) -> string do
let out = "-"
if (type == 2) out = "d"
if (type == 3) out = "c"
const chars = "rwxrwxrwx"
for bit in 0..9 do
if ((mode band (1 shl (8 - bit))) != 0) do
out = out ~ chars[bit]
end else do
out = out ~ "-"
end
end
return out
end
fn human_size(n) -> string do
if (n < 1024)
return "#{n}"
if n < 1048576 do
const whole = math.floor(n / 1024)
const frac = math.floor((n - whole * 1024) * 10 / 1024)
return "#{whole}.#{frac}K"
end
const whole = math.floor(n / 1048576)
const frac = math.floor((n - whole * 1048576) * 10 / 1048576)
return "#{whole}.#{frac}M"
end
fn pad_left(text, width) -> string do
let out = text
while (out:len() < width) do out = " " ~ out end
return out
end
fn pad_right(text, width) -> string do
let out = text
while (out:len() < width) do out = out ~ " " end
return out
end
const context = env.ctx
let long = :false
let all = :false
let human = :false
let target = :nil
for ai in 1..context.argv:len() do
const a = context.argv[ai]
if (a:len() > 1 and a[0] == "-") do
for ci in 1..a:len() do
const c = a[ci]
if (c == "l") long = :true
if (c == "a") all = :true
if (c == "h") human = :true
end
end else do
target = a
end
end
const path = if (target == :nil) wd.path else expand_path(target)
let records = {}
let failed = :false
if vfs:is_dir(path) do
const items = vfs:entries(path)
if items == :nil do
fail("ls: #{path}: cannot read\n")
failed = :true
end else do
for idx in 0..items:len() do
const e = items[idx]
if (all or (e.name[0] != "."))
records:push(e)
end
end
end else do
const info = vfs:stat(path)
if info == :nil do
fail("ls: #{path}: no such file\n")
failed = :true
end else do
const parts = path:split("/")
records:push({ name = parts[parts:len() - 1], type = info.type, size = info.size, mode = info.mode, uid = info.uid })
end
end
fn decorate(record) -> string do
if (record.type == 2)
return record.name ~ "/"
return record.name
end
if failed == :false do
if records:len() == 0 do
puts(" (empty)\n")
end else if long do
# precompute
let owners = {}
let sizes = {}
let owner_w = 0
let size_w = 0
for idx in 0..records:len() do
const r = records[idx]
const owner = user_name(r.uid)
const sz = if (human) human_size(r.size) else "#{r.size}"
owners:push(owner)
sizes:push(sz)
if (owner:len() > owner_w) owner_w = owner:len()
if (sz:len() > size_w) size_w = sz:len()
end
for idx in 0..records:len() do
const r = records[idx]
puts("#{mode_string(r.type, r.mode)} #{pad_right(owners[idx], owner_w)} #{pad_left(sizes[idx], size_w)} #{decorate(r)}\n")
end
end else do
for idx in 0..records:len() do
puts(" #{decorate(records[idx])}\n")
end
end
end
|