11local targetPath = terminal.pwd
2- string first = args[1].toStr()
2+ local flags = args.filter(v -> v.startsWith("-"))
3+ local paths = args.filter(v -> !v.startsWith("-"))
34
4- if !first.startsWith("-") (
5- targetPath = terminal.resolvePath(args [1])
5+ if paths.len > 0 (
6+ targetPath = terminal.resolvePath(paths [1])
67)
7- if !targetPath.endsWith(".folder") (
8- targetPath ++= ".folder"
8+
9+ local folderPath = targetPath
10+ if folderPath.endsWith(".folder") (
11+ folderPath = folderPath.trim(1, -8)
912)
1013
11- file "exists" targetPath
14+ file "exists" folderPath ++ ".folder"
1215if !exists (
13- terminal.writeLine("ls: cannot access '" ++ args[1] ++ "': No such file or directory")
16+ local requested = paths.len > 0 ? paths[1] folderPath
17+ terminal.writeLine("ls: cannot access '" ++ requested ++ "': No such file or directory")
1418 return
1519)
1620
17- local getAll = args.contains("-a")
21+ local getAll = flags.contains("-a")
22+ local entries = listFiles(folderPath)
23+ array files = []
1824
19- local files = open(targetPath)
20- .map(f -> open(f, ["name", "type"]).join(""))
21- .filter(v -> getAll or v[1] != ":")
25+ for i entries.len (
26+ local name = entries[i]
27+ if getAll or name[1] != ":" (
28+ void files.append(name)
29+ )
30+ )
2231
23- local maxLen = files
24- .map(v -> (v.endsWith(".folder") ? (v.len - 8) v.len) + 4)
25- .max()
32+ if files.len == 0 (
33+ return
34+ )
35+
36+ local maxLen = 0
37+ for i files.len (
38+ local name = files[i]
39+ if name.endsWith(".folder") (
40+ name = name.trim(1, -8) ++ "/"
41+ )
42+ if name.len > maxLen (
43+ maxLen = name.len
44+ )
45+ )
46+ maxLen += 4
2647
2748local cols = floor((window.width - 30) / 10 / (maxLen + 2))
2849if cols < 1 (
2950 cols = 1
3051)
3152
3253array line = []
33- local i = 0
34- void files.map(v -> (
35- local ov = v
54+ local column = 0
55+ for i files.len (
56+ local name = files[i]
3657 local color = txtc
3758
38- if ov .endsWith(".folder") (
39- ov = ov .trim(1, -8) ++ "/"
59+ if name .endsWith(".folder") (
60+ name = name .trim(1, -8) ++ "/"
4061 color = global_accent
4162 )
42- if ov [1] == ":" (
63+ if name [1] == ":" (
4364 color = "#aaa"
4465 )
4566
46- local padded = ov .padEnd(" ", maxLen)
47- void line.append(color, padded)
67+ void line.append(color, name .padEnd(" ", maxLen) )
68+ column += 1
4869
49- i ++
50- if i % cols == 0 (
70+ if column == cols (
5171 terminal.writeLine(line)
5272 line = []
73+ column = 0
5374 )
54- ))
75+ )
5576
56- if i % cols != 0 (
77+ if line.len > 0 (
5778 terminal.writeLine(line)
58- )
79+ )
0 commit comments