|
| 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 | +) |
0 commit comments