Skip to content

Commit 9d38ea2

Browse files
committed
big update for more terminal commands
1 parent 6624359 commit 9d38ea2

18 files changed

Lines changed: 801 additions & 55 deletions

:path/cp.osl

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
local src = args[1]
2+
local dst = args[2]
3+
4+
if src == null or dst == null (
5+
terminal.writeLine("cp: missing file operand")
6+
terminal.writeLine("Usage: cp <source> <destination>")
7+
return
8+
)
9+
10+
local srcPath = terminal.resolvePath(src)
11+
if !src.contains(".") (
12+
srcPath ++= ".folder"
13+
)
14+
15+
file "exists" srcPath
16+
if !exists (
17+
terminal.writeLine("cp: cannot stat '" ++ src ++ "': No such file or directory")
18+
return
19+
)
20+
21+
local dstPath = terminal.resolvePath(dst)
22+
local isDstDir = false
23+
24+
if !dst.contains(".") (
25+
dstPath ++= ".folder"
26+
isDstDir = true
27+
)
28+
29+
file "exists" dstPath
30+
if exists (
31+
isDstDir = true
32+
)
33+
34+
if isDstDir (
35+
local name = srcPath.split("/")[-1]
36+
dstPath = dstPath.trim(1, -8) ++ "/" ++ name
37+
)
38+
39+
local content = open(srcPath)
40+
file "set_file" dstPath content
41+
terminal.writeLine("copied '" ++ src ++ "' to '" ++ dst ++ "'")

:path/cut.osl

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
local input = null
2+
3+
if terminal.stdin != null (
4+
input = terminal.stdin
5+
) else if args.len > 0 (
6+
local target = args[args.len]
7+
if !target.startsWith("-") (
8+
local filePath = terminal.resolvePath(target)
9+
file "exists" filePath
10+
if exists (
11+
input = open(filePath)
12+
) else (
13+
terminal.writeLine("cut: " ++ target ++ ": No such file or directory")
14+
return
15+
)
16+
)
17+
)
18+
19+
if input == null (
20+
terminal.writeLine("cut: missing operand")
21+
return
22+
)
23+
24+
local delimiter = "\t"
25+
local fields = null
26+
local chars = null
27+
local i = 1
28+
while i <= args.len (
29+
local arg = args[i]
30+
if arg.startsWith("-d") (
31+
if arg.len > 2 (
32+
delimiter = arg[3]
33+
) else (
34+
delimiter = args[i + 1]
35+
i += 1
36+
)
37+
) else if arg.startsWith("-f") (
38+
if arg.len > 2 (
39+
fields = arg.trim(3, -1).split(",")
40+
) else (
41+
fields = args[i + 1].split(",")
42+
i += 1
43+
)
44+
) else if arg.startsWith("-c") (
45+
if arg.len > 2 (
46+
chars = arg.trim(3, -1)
47+
) else (
48+
chars = args[i + 1]
49+
i += 1
50+
)
51+
)
52+
i += 1
53+
)
54+
55+
local lines = input.split("\n")
56+
57+
for l lines.len (
58+
local line = lines[l]
59+
local output = ""
60+
61+
if chars != null (
62+
if chars.contains("-") (
63+
local parts = chars.split("-")
64+
local start = parts[1].toNum() ?? 1
65+
local end = parts[2].toNum() ?? line.len
66+
output = line.trim(start, end)
67+
) else (
68+
local idx = chars.toNum()
69+
if idx != null (
70+
output = line[idx] ?? ""
71+
)
72+
)
73+
) else if fields != null (
74+
local cols = line.split(delimiter)
75+
local parts = []
76+
for f fields.len (
77+
local idx = fields[f].toNum()
78+
if idx != null and idx <= cols.len (
79+
void parts.append(cols[idx])
80+
)
81+
)
82+
output = parts.join(delimiter)
83+
) else (
84+
output = line
85+
)
86+
87+
terminal.writeLine(output)
88+
)

:path/false.osl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
terminal.writeLine(false)

:path/file.osl

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
local target = args[1]
2+
3+
if target == null (
4+
terminal.writeLine("file: missing operand")
5+
return
6+
)
7+
8+
local filePath = terminal.resolvePath(target)
9+
file "exists" filePath
10+
11+
if !exists (
12+
terminal.writeLine("file: cannot open '" ++ target ++ "': No such file or directory")
13+
return
14+
)
15+
16+
local data = open(filePath, ["type"])
17+
local fileType = data[1]
18+
19+
if fileType == ".folder" (
20+
terminal.writeLine(target ++ ": directory")
21+
return
22+
)
23+
24+
local content = open(filePath)
25+
local lines = content.split("\n")
26+
local lineCount = lines.len
27+
local maxLineLen = 0
28+
29+
for i lines.len (
30+
if lines[i].len > maxLineLen (
31+
maxLineLen = lines[i].len
32+
)
33+
)
34+
35+
local hasBinary = false
36+
local hasNonASCII = false
37+
38+
for i content.len (
39+
local c = content[i].toNum()
40+
if c != null (
41+
if c == 0 (
42+
hasBinary = true
43+
)
44+
if c > 127 (
45+
hasNonASCII = true
46+
)
47+
)
48+
)
49+
50+
local desc = filePath
51+
if hasBinary (
52+
desc = "binary data"
53+
) else if hasNonASCII (
54+
desc = "Unicode text, UTF-8 text"
55+
) else (
56+
desc = "ASCII text"
57+
)
58+
59+
if maxLineLen > 500 (
60+
desc ++= ", with very long lines (" ++ maxLineLen ++ ")"
61+
) else if maxLineLen > 100 (
62+
desc ++= ", with long lines (" ++ maxLineLen ++ ")"
63+
)
64+
65+
desc ++= " (" ++ lineCount ++ " lines)"
66+
67+
terminal.writeLine(target ++ ": " ++ desc)

:path/find.osl

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
local searchPath = terminal.pwd
2+
local namePattern = null
3+
local typeFilter = null
4+
5+
for i args.len (
6+
local arg = args[i]
7+
if arg == "." or i == 1 (
8+
if arg != "." (
9+
searchPath = terminal.resolvePath(arg)
10+
)
11+
) else if arg.startsWith("-name") (
12+
namePattern = args[i + 1]
13+
) else if arg.startsWith("-type") (
14+
typeFilter = args[i + 1]
15+
)
16+
)
17+
18+
local results = []
19+
local startUuid = open(searchPath ++ ".folder", "uuid")[1]
20+
local stack = [[startUuid, 1]]
21+
22+
while stack.len > 0 (
23+
local current @= stack.pop()
24+
local uuid = current[1]
25+
local depth = current[2]
26+
27+
if depth > 50 (
28+
continue
29+
)
30+
31+
file "exists" uuid
32+
if !exists (
33+
continue
34+
)
35+
36+
local items = open(uuid)
37+
for j items.len (
38+
local itemUuid = items[j]
39+
local itemData = open(itemUuid, ["name", "type"])
40+
local itemName = itemData[1] ++ itemData[2]
41+
local isDir = itemData[2] == ".folder"
42+
43+
local match = true
44+
if namePattern != null (
45+
local pattern = namePattern
46+
if pattern[1] == "*" and pattern[-1] == "*" (
47+
match = itemName.contains(pattern.trim(2, -2))
48+
) else if pattern[1] == "*" (
49+
match = itemName.endsWith(pattern.trim(2, -1))
50+
) else if pattern[-1] == "*" (
51+
match = itemName.startsWith(pattern.trim(1, -2))
52+
) else (
53+
match = itemName == pattern
54+
)
55+
)
56+
if typeFilter != null (
57+
if typeFilter == "d" and !isDir (
58+
match = false
59+
) else if typeFilter == "f" and isDir (
60+
match = false
61+
)
62+
)
63+
64+
if match (
65+
void results.append(itemUuid)
66+
)
67+
68+
if isDir (
69+
void stack.append([itemUuid, depth + 1])
70+
)
71+
)
72+
)
73+
74+
for r results.len (
75+
array data = open(results[r], ["location", "name", "type"])
76+
string path = data[1] ++ "/" ++ data[2] ++ data[3]
77+
if path.startsWith(searchPath) (
78+
path = path.replaceFirst(searchPath, ".")
79+
)
80+
terminal.writeLine(path)
81+
)

:path/head.osl

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,36 @@
1-
local input = terminal.stdin ?? args[2]
2-
local count = args[1] ?? 10
3-
local lines = input.split("\n", count)
1+
local input = null
2+
local count = 10
3+
local filePath = null
44

5-
void lines.map(v -> (
6-
terminal.writeLine(v)
7-
))
5+
for i args.len (
6+
if args[i].startsWith("-n") (
7+
count = args[i].trim(3, -1).toNum() ?? args[i + 1].toNum() ?? 10
8+
) else if args[i].toNum() != null (
9+
count = args[i].toNum()
10+
) else if !args[i].startsWith("-") (
11+
filePath = terminal.resolvePath(args[i])
12+
)
13+
)
14+
15+
if terminal.stdin != null (
16+
input = terminal.stdin
17+
) else if filePath != null (
18+
file "exists" filePath
19+
if exists (
20+
input = open(filePath)
21+
) else (
22+
terminal.writeLine("head: cannot open '" ++ args[args.len] ++ "': No such file or directory")
23+
return
24+
)
25+
)
26+
27+
if input == null (
28+
terminal.writeLine("head: missing operand")
29+
return
30+
)
31+
32+
local lines = input.split("\n")
33+
34+
for i count.clamp(1, lines.len) (
35+
terminal.writeLine(lines[i])
36+
)

:path/history.osl

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
local file = null
2+
local numLines = null
3+
4+
for i args.len (
5+
if args[i].toNum() != null (
6+
numLines = args[i].toNum()
7+
)
8+
)
9+
10+
file "open" "~/:path/history.txt" "onlyaccess"
11+
local history = fileGet(4).split("\n").filter(v -> v.len > 0)
12+
file "close"
13+
14+
if numLines == null (
15+
for i history.len (
16+
terminal.writeLine(" " ++ (i + 1) ++ " " ++ history[i])
17+
)
18+
) else (
19+
local start = history.len - numLines
20+
if start < 1 (
21+
start = 1
22+
)
23+
for i history.len (
24+
if i >= start (
25+
terminal.writeLine(" " ++ (i + 1) ++ " " ++ history[i])
26+
)
27+
)
28+
)

:path/ls.osl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
local targetPath = terminal.pwd
2-
if args.len > 0 and args[1] != "-a" and !args[1].startsWith("-") (
2+
if typeof(args[1]) == "string" and args[1] != "-a" and !args[1].startsWith("-") (
33
targetPath = terminal.resolvePath(args[1])
44
if !targetPath.endsWith(".folder") (
55
targetPath ++= ".folder"

0 commit comments

Comments
 (0)