From 8440378a393d1c4b64b48c8a93f68e38bc76d278 Mon Sep 17 00:00:00 2001 From: Daniel Barlow Date: Mon, 24 Mar 2025 22:37:24 +0000 Subject: [PATCH] anoia: make dirname handle tralning / like posix --- pkgs/anoia/Makefile | 1 + pkgs/anoia/init.fnl | 21 ++++++++++++++++++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/pkgs/anoia/Makefile b/pkgs/anoia/Makefile index df1435c..2d202d6 100644 --- a/pkgs/anoia/Makefile +++ b/pkgs/anoia/Makefile @@ -9,6 +9,7 @@ check: ln -s . anoia fennel ./run-tests.fnl $(CHECK) fennel test.fnl + mkdir -p $(outputdir) fennel test-svc.fnl $(servicedir) find $(outputdir) -ls test -f $(outputdir)/fish diff --git a/pkgs/anoia/init.fnl b/pkgs/anoia/init.fnl index 05ddd71..54be3b1 100644 --- a/pkgs/anoia/init.fnl +++ b/pkgs/anoia/init.fnl @@ -25,8 +25,27 @@ (fn basename [path] (string.match path ".*/([^/]-)$")) + (fn dirname [path] - (string.match path "(.*)/[^/]-$")) + (let [stripped (string.match path "(.-)/*$")] + (pick-values + 1 + (if (not path) "." + (= path "") "." + (not stripped) "." + (= stripped "") "/" + (string.match stripped ".+/.-") (stripped:gsub "(.*)(/.*)" "%1") + (string.match stripped "/") "/" + ".")))) + +(define-tests + ;; these are examples from dirname(3) + (expect= (dirname "/usr/lib") "/usr") + (expect= (dirname "/usr/") "/") + (expect= (dirname "usr") ".") + (expect= (dirname "/") "/") + (expect= (dirname ".") ".") + (expect= (dirname "..") ".")) (fn append-path [dirname filename] (let [base (or (string.match dirname "(.*)/$") dirname)