From 4503824271a0490be8efdbd3b890ce3a1c5a5396 Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Fri, 6 Sep 2024 22:39:42 -0400 Subject: [PATCH] Add path resolving --- examples/file.tm | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/examples/file.tm b/examples/file.tm index 99383a5..4f4d864 100644 --- a/examples/file.tm +++ b/examples/file.tm @@ -62,12 +62,20 @@ func resolve_path(path:Text, relative_to=".")->Text: inline C { extern char *resolve_path(char*, char*, char*); } - !! Resolving: ($path_c_str, $relative_to_c_str, $relative_to_c_str) - >> resolved := inline C ( + resolved := inline C ( resolve_path($path_c_str, $relative_to_c_str, $relative_to_c_str) ): CString return Text.from_c_string(resolved) +func relative_path(path:Text, relative_to=".")->Text: + path = resolve_path(path, relative_to) + + relative_to = resolve_path(relative_to) + if path:matches($/{start}$relative_to$"/"{..}/): + return path:slice(relative_to.length + 2, -1) + + return path + struct WriteHandle(_file:@Memory): func write(h:WriteHandle, text:Text, flush=yes): inline C { @@ -181,12 +189,14 @@ func command(cmd:Text)->FileReadResult: else: return Failure(builtin_last_err()) -func main(): - word := "" - when command("shuf -n 1 /usr/share/dict/words") is Success(w): - >> word = w - is Failure(msg): - fail(msg) +func main(path:Text, relative_to="."): + >> resolve_path(path, relative_to) + >> relative_path(resolve_path(path, relative_to)) + # word := "" + # when command("shuf -n 1 /usr/share/dict/words") is Success(w): + # >> word = w + # is Failure(msg): + # fail(msg) # when writing("test.txt") is Open(f): # say("Writing {word} to test.txt")