aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2024-09-06 22:39:42 -0400
committerBruce Hill <bruce@bruce-hill.com>2024-09-06 22:39:42 -0400
commit4503824271a0490be8efdbd3b890ce3a1c5a5396 (patch)
treef9c56e5e793d3427416560ad32421c6f3bf13075 /examples
parenta90ef1973f1903a50f81797deb8611dd3373fb23 (diff)
Add path resolving
Diffstat (limited to 'examples')
-rw-r--r--examples/file.tm26
1 files changed, 18 insertions, 8 deletions
diff --git a/examples/file.tm b/examples/file.tm
index 99383a56..4f4d8645 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")