// retoor <retoor@molodetz.nl>
import "io" for Directory, File, Stat
class FileExplorer {
static listRecursive(path, depth) {
var indent = " " * depth
var files = []
// We wrap Directory.list in a try to handle permission errors
var fiber = Fiber.new {
files = Directory.list(path)
}
fiber.try()
if (fiber.error != null) {
System.print("\%(indent)[!] Error accessing \%(path)")
return
}
// Sort files manually since String comparison isn't built-in
files.sort(Fn.new {|a, b|
var ba = a.bytes
var bb = b.bytes
var len = ba.count < bb.count ? ba.count : bb.count
for (i in 0...len) {
if (ba[i] < bb[i]) return true
if (ba[i] > bb[i]) return false
}
return ba.count < bb.count
})
for (file in files) {
if (file == "." || file == "..") continue
var fullPath = path + "/" + file
if (path == ".") fullPath = "./" + file
var stat = Stat.path(fullPath)
if (stat.isDirectory) {
System.print("\%(indent)[D] \%(file)/")
listRecursive(fullPath, depth + 1)
} else {
var size = stat.size
var unit = "B"
if (size > 1024) {
size = size / 1024
unit = "KB"
}
System.print("\%(indent)[F] \%(file) (\%(size.round)\%(unit))")
}
}
}
}
System.print("Recursive directory listing for current path:")
System.print("--------------------------------------------")
FileExplorer.listRecursive(".", 0)