diff --git a/.local/share/common-lisp/source/ambrevar/file.lisp b/.local/share/common-lisp/source/ambrevar/file.lisp index c23332b6..30e99204 100644 --- a/.local/share/common-lisp/source/ambrevar/file.lisp +++ b/.local/share/common-lisp/source/ambrevar/file.lisp @@ -46,24 +46,43 @@ If none, return the empty string unlike `pathname-type'." (or (pathname-type (path file)) "")) +(defmethod directory? ((file file)) + (eq (kind file) :directory)) + +(defmethod file? ((file file)) + (eq (kind file) :regular-file)) + +(defun separator (&optional char?) + (if char? + (uiop:directory-separator-for-host) + (string (uiop:directory-separator-for-host)))) + (export-always 'basename) (defmethod basename ((file file)) - "Return the file basename (including the extension)." - (let ((last-separator (position (uiop:directory-separator-for-host) - (path file) :from-end t))) - (subseq (path file) + "Return the file basename (including the extension). +This returns the directory name for directories." + (let* ((path (path file)) + (path (if (str:ends-with? (separator) path) + (subseq path 0 (1- (length path))) + path)) + (last-separator (position (separator :char) + path :from-end t))) + (subseq path (1+ last-separator)))) (defmethod print-object ((file file) stream) (print-unreadable-object (file stream :type t :identity t) - (write-string (basename file) stream))) + (write-string (str:concat (basename file) + (when (directory? file) "/")) + stream))) (export-always 'file) (defun file (path) (let ((native-path (uiop:truename* (if (pathnamep path) path (uiop:parse-native-namestring path))))) - (assert (uiop:file-exists-p native-path)) + (assert (or (uiop:file-exists-p native-path) + (uiop:directory-exists-p native-path))) (let ((stat (osicat-posix:stat native-path))) ;; From Osicat's `file-permissions': (flet ((stat-permissions (stat)