// retoor <retoor@molodetz.nl>
import "pathlib" for Path
System.print("=== Pathlib Module Demo ===\n")
System.print("--- Path Construction ---")
var p = Path.new("/home/user/documents/report.tar.gz")
System.print("Path: %(p)")
System.print("Name: %(p.name)")
System.print("Stem: %(p.stem)")
System.print("Suffix: %(p.suffix)")
System.print("Suffixes: %(p.suffixes)")
System.print("Parts: %(p.parts)")
System.print("\n--- Parent Navigation ---")
System.print("Parent: %(p.parent)")
System.print("Parents:")
for (ancestor in p.parents) {
System.print(" %(ancestor)")
}
System.print("\n--- Path Properties ---")
System.print("Root: %(p.root)")
System.print("Anchor: %(p.anchor)")
System.print("Is absolute: %(p.isAbsolute)")
var rel = Path.new("relative/path.txt")
System.print("%(rel) is absolute: %(rel.isAbsolute)")
System.print("\n--- Join Operator (/) ---")
var base = Path.new("/home/user")
var config = base / ".config" / "myapp" / "settings.json"
System.print("%(base) / .config / myapp / settings.json = %(config)")
System.print("\n--- joinpath ---")
var multi = Path.new("/opt").joinpath(["local", "bin", "wren_cli"])
System.print("Joined: %(multi)")
System.print("\n--- withName / withStem / withSuffix ---")
var orig = Path.new("/data/archive.tar.gz")
System.print("Original: %(orig)")
System.print("withName(\"backup.zip\"): %(orig.withName("backup.zip"))")
System.print("withStem(\"snapshot\"): %(orig.withStem("snapshot"))")
System.print("withSuffix(\".bak\"): %(orig.withSuffix(".bak"))")
System.print("\n--- relativeTo ---")
var full = Path.new("/home/user/projects/wren/src/main.c")
var project = Path.new("/home/user/projects/wren")
System.print("%(full) relative to %(project) = %(full.relativeTo(project))")
System.print("\n--- Glob Matching ---")
var file = Path.new("/tmp/notes.txt")
System.print("%(file.name) matches *.txt: %(file.match("*.txt"))")
System.print("%(file.name) matches *.md: %(file.match("*.md"))")
System.print("%(file.name) matches note?: %(file.match("note?"))")
System.print("%(file.name) matches n*s.txt: %(file.match("n*s.txt"))")
System.print("\n--- Home and CWD ---")
System.print("Home: %(Path.home)")
System.print("CWD: %(Path.cwd)")
System.print("\n--- Expand User ---")
var tilde = Path.new("~/.bashrc")
System.print("%(tilde) -> %(tilde.expanduser())")
System.print("\n--- Equality ---")
var a = Path.new("/tmp/test")
var b = Path.new("/tmp/test")
var c = Path.new("/tmp/other")
System.print("%(a) == %(b): %(a == b)")
System.print("%(a) == %(c): %(a == c)")
System.print("%(a) != %(c): %(a != c)")
System.print("\n--- Filesystem Operations ---")
var testDir = Path.new("/tmp/wren_pathlib_demo")
if (testDir.exists()) testDir.rmtree()
testDir.mkdir(true)
System.print("Created: %(testDir)")
System.print("Exists: %(testDir.exists())")
System.print("Is dir: %(testDir.isDir())")
var testFile = testDir / "hello.txt"
testFile.writeText("Hello from pathlib!")
System.print("Wrote: %(testFile)")
System.print("Content: %(testFile.readText())")
System.print("Is file: %(testFile.isFile())")
System.print("\n--- Stat ---")
var s = testFile.stat()
System.print("Size: %(s.size) bytes")
System.print("\n--- Touch ---")
var marker = testDir / "marker"
marker.touch()
System.print("Touched: %(marker) (exists: %(marker.exists()))")
System.print("\n--- Copy ---")
var copy = testDir / "hello_copy.txt"
testFile.copyfile(copy)
System.print("Copied to: %(copy)")
System.print("Copy content: %(copy.readText())")
System.print("\n--- Rename ---")
var renamed = testFile.rename(testDir / "greeting.txt")
System.print("Renamed to: %(renamed)")
System.print("Old exists: %(testFile.exists())")
System.print("New content: %(renamed.readText())")
System.print("\n--- Resolve ---")
var resolved = Path.new(".").resolve()
System.print("Resolved '.': %(resolved)")
System.print("\n--- Same File ---")
System.print("Same file: %(renamed.samefile(renamed))")
System.print("\n--- Owner and Group ---")
System.print("Owner: %(renamed.owner())")
System.print("Group: %(renamed.group())")
System.print("\n--- Mkdir with Parents ---")
var deep = testDir / "a" / "b" / "c"
deep.mkdir(true)
System.print("Created: %(deep)")
System.print("Exists: %(deep.exists())")
System.print("\n--- Iterdir ---")
(testDir / "file1.txt").writeText("one")
(testDir / "file2.txt").writeText("two")
var entries = testDir.iterdir()
System.print("Entries in %(testDir.name):")
for (entry in entries) {
var kind = entry.isDir() ? "dir" : "file"
System.print(" %(entry.name) (%(kind))")
}
System.print("\n--- Glob ---")
var txtFiles = testDir.glob("*.txt")
System.print("Text files in %(testDir.name):")
for (f in txtFiles) {
System.print(" %(f.name)")
}
System.print("\n--- Recursive Glob ---")
(testDir / "a" / "nested.txt").writeText("nested")
(testDir / "a" / "b" / "deep.txt").writeText("deep")
var allTxt = testDir.rglob("*.txt")
System.print("All text files (recursive):")
for (f in allTxt) {
System.print(" %(f.relativeTo(testDir))")
}
System.print("\n--- Walk ---")
System.print("Directory tree:")
for (entry in testDir.walk()) {
var dir = entry[0]
var dirs = entry[1]
var files = entry[2]
var indent = dir == testDir ? "" : " "
System.print("%(indent)%(dir.relativeTo(testDir))/ (%(dirs.count) dirs, %(files.count) files)")
}
System.print("\n--- Cleanup ---")
testDir.rmtree()
System.print("Cleaned up: %(testDir) (exists: %(testDir.exists()))")
System.print("\n=== Demo Complete ===")