Portable way to show the contents of the current d

2019-07-25 16:07发布

I want to provide additional information to the user during a restart-case for missing input files.

Is there a portable way to print out the files in the current directory?

标签: common-lisp
2条回答
一夜七次
2楼-- · 2019-07-25 16:52

Please take a look at File System Concepts, specifically at directory.

E.g.,

  • use (directory "~/*") to get the list of files in your home,
  • use (directory "~/*/") to get the list of directories in your home,
  • and (directory "~/**/*") to get the list of all files in all subdirectoris in your home

Note that directory may take implementation-specific arguments like :full.

If you are using asdf (you should!), you might want to take a look at uiop which comes with it.

PS. If your implementation does not support ~ in pathname to refer to the home directory, you can use user-homedir-pathname instead.

查看更多
放荡不羁爱自由
3楼-- · 2019-07-25 17:04

The reference to uiop was the hint I needed. This is what I use now in my test case and which works perfectly for me:

 (defun file-is-missing ()
  (let* ((cwd (uiop:getcwd))
         (files (uiop:directory-files cwd)))
    (format t "~&Current directory: ~a~%" cwd)
    (dolist (file files)
      (format t "~&~3T~a" file))
    (format t "~&Provide new file: ")
    (list (read-line))))

(defun read-file (file)
  (restart-case
      (with-open-file (s file :direction :input :if-does-not-exist :error)
        (do ((l (read-line s) (read-line s nil 'eof)))
            ((eq l 'eof) "Reached end of file.")
          (format t "~&*** ~A~%" l)))
    (New-File (filename)
      :interactive file-is-missing
      (read-file filename))))
查看更多
登录 后发表回答