Skip to content

Implement read-dir - #4380

Closed
lucifer1004 wants to merge 3 commits into
typst:mainfrom
lucifer1004:readdir
Closed

lucifer1004 wants to merge 3 commits into
typst:mainfrom
lucifer1004:readdir

Conversation

@lucifer1004

Copy link
Copy Markdown
Contributor

This PR works as a POC solution for #2123, which I think is a missing basic feature, especially when writing a thesis/novel or editing a collection of works.

The design is very primitive and the implementation may introduce some bugs, but all these can be discussed and changed. A major purpose of this PR is to support those who are in favor of the directory walking feature.

@lucifer1004
lucifer1004 marked this pull request as draft June 12, 2024 03:44
@lucifer1004
lucifer1004 marked this pull request as ready for review June 12, 2024 05:32
@Andrew15-5

Copy link
Copy Markdown
Contributor

If anything, the name should be changed. It contains 2 words without any separator. The standard library has only single word names. So this is unusual.

I have 2 propositions: read-dir, directory.
But first one is just a function like calc.max, while the second one is more like an element (image).

If we would go with the second, then it would have to be an #[element] that would have children (at the very least, maybe readable too).

Since directory is not a building block, it means that ultimately making it an element is useless. But I just thought of another approach. Like image has image.decode (IIRC, this can be changed in the near future), read can have read.dir or read.directory.

I don't know if you can add functions/methods to a function (read is a function while image is an element), but this to me feels like the best way to go in the current design.

@lucifer1004

Copy link
Copy Markdown
Contributor Author

On the Rust side, #[func(name="read.dir")] is viable but the exported function cannot be used in Typst since it will not be interpreted as a whole.

@lucifer1004 lucifer1004 changed the title Implement readdir Implement read-dir Jun 13, 2024
@lucifer1004

Copy link
Copy Markdown
Contributor Author

I renamed the function to read-dir in accordance with the naming convention of the standard library.

@laurmaedje

Copy link
Copy Markdown
Member

Thanks for the PR! A remaining concern of mine is the one from #2123 (comment): How does one distinguish files from folders into which one might want to recurse?

I'm also not certain how typst watch would efficiently watch for file system changes that affect this. It might not turn out to be problematic, but I'd like to know for sure before introducing the functionality.

There is also the problem of non-UTF-8 paths (which I think fall into an unwrap_or_default() here), but it's maybe not a huge deal (though we should definitely handle them more gracefully that returning an empty string).

@Andrew15-5

Copy link
Copy Markdown
Contributor

How does one distinguish files from folders into which one might want to recurse?

I think the simplest solution will be to use / identifier at the end (ls -p). It won't complicate the code (just a bit), but the output of the function will be a bit harder to parse. Like if you want to get just the names, then you would have to strip the / manually (although this can be a switch that is turned off by default).

I'm also not certain how typst watch would efficiently watch for file system changes that affect this.

I think it's exactly the same as using inotifywait -r . which outputs Setting up watches. Beware: since -r was given, this may take a while!. It will be a performance problem if you watch your home directory, but even a big Typst project wouldn't be that big to notice a performance hit, I think. In fact, typst watch already acts like inotifywait -r .. Right now, I can only think of a problem that this function call can't be memoized. Or you would have to store metadata about currently tracked files, and when typst watch sees a new/changed/removed file and #read-dir() is checking the path where the change occurred, then it will rerun the function. If the function (1) checks a different path or (2) a new change is just some file content has been changed (no new/removed files), then it won't be rerun.

I can't say anything concrete about the UTF-8 problem, but I think that one of those lossy methods can be used and a warning/note in the documentation can be added that the invalid UTF-8 names will drop the invalid bytes. Maybe the other solution would be to add (another) switch that will control whether the output will be an array of the lossy UTF-8 strings, or an array of bytes elements.

@lucifer1004

Copy link
Copy Markdown
Contributor Author
  1. I just added a dir-trailing option which defaults to none. This can be used to distinguish sub-directories from files. I tried to add some tests, but found that the handling of assets is specialized. I may need some help here.

  2. For typst watch, when there is no change to the source file, change of directory contents will not trigger a re-build. But I think this is minor, since we only need to perform a no-op (type in something and delete, then save) to trigger it.

  3. For non UTF-8 directory name, the problem is that we cannot type in the exact name (using other encodings) in a Typst source file.

  4. In the current implementation, FileError is reused, but adding FileError::IsFile is kind of ridiculous. I would like your ideas on how to express this type of error (trying to read a file as a directory) more clearly.

@istudyatuni

Copy link
Copy Markdown
Contributor

I don't like dir-trailing, I think it won't play well with path/resource type, when it will be implemented

I suggest returning some path type, which will hold internally PathBuf, will have is-dir, is-file, will be loosely converted to string, and when passed to functions like #image, will be treated as-is, allowing for non-utf8 paths

@laurmaedje

Copy link
Copy Markdown
Member

I agree that a path type is the better option. I think we should defer this until we have a solution for paths/resources worked out and implemented. I'm sorry to be blocking this, but I think a little patience will reward us with a cleaner solution in the end. Still, thanks for your work @lucifer1004!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants