ambrevar/file: Add print-object for files, move file-extension and file-basename.

master
Pierre Neidhardt 2021-01-15 12:44:45 +01:00
parent 6965271af1
commit 81a54a0a7a
2 changed files with 18 additions and 15 deletions

View File

@ -39,7 +39,24 @@
(cons #.(cons 'member (mapcar #'first osicat::+permissions+))))))
(:accessor-name-transformer #'name-identity))
;; TODO: Customize `print-object'.
(export-always 'extension)
(defmethod extension ((file file))
"Return the file extension.
If none, return the empty string unlike `pathname-type'."
(or (pathname-type (path file))
""))
(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)
(1+ last-separator))))
(defmethod print-object ((file file) stream)
(print-unreadable-object (file stream :type t :identity t)
(write-string (basename file) stream)))
(export-always 'file)
(defun file (path)

View File

@ -57,20 +57,6 @@ keeping the same cookie saves about 15% of time.")
"Return the FILE description as per the `file' UNIX command."
(magicffi:magic-file %magic-cookie-description file))
(export-always 'file-extension)
(defun file-extension (file)
"Return the file extension.
If none, return the empty string unlike `pathname-type'."
(or (pathname-type file)
""))
(export-always 'file-basename)
(defun file-basename (file)
"Return the file basename (including the extension)."
(apply #'str:concat (pathname-name file)
(sera:and-let* ((ext (file-extension file)))
`("." ,ext))))
(export-always 'match-extensions)
(defun match-extensions (extension &rest more-extensions)
"Return a predicate for files that match on of the provided extensions.