Support reading input from stdin#3339
Merged
laurmaedje merged 12 commits intotypst:mainfrom Feb 5, 2024
Merged
Conversation
This also introduces a statically allocated `FileId` called `STDIN_ID`, which is being compared with when constructing a `FileReader` from a `FileId` in order to read a file with the correct way.
This special-cases the filename `-` as the input argument to be treated as reading from the standard input. Users can use `./-` instead for reading a real file named `-` in the current directory.
Data is better modeled when directly using `InputPath`.
It's not needed; this also encourages the better use of it (not extracting the underlying id)
laurmaedje
reviewed
Feb 5, 2024
Member
laurmaedje
left a comment
There was a problem hiding this comment.
Looks very good! Just a few remarks.
Contributor
Author
|
@laurmaedje All done! Thanks for the review. |
Member
|
Thank you! |
1 task
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR extends the ability of the CLI to be able to read from stdin. It enables various use cases like piping into the CLI (which is very useful when building automation around typst), and sending pre-processed files to it.
Using
-as the input path will trigger this behavior. It is required to provide an output path when using this functionality.It should be considered a (slight) breaking change to the user interface. After this change, a file in the current directory that is named
-needs to be referred to as./-instead.In order to do this, following changes are made to the code base:
typst-syntax,FileIdgets a new methodnew_fake, to create new file ids which cannot be looked up with a path (i.e. the only identifier for it is the ID number itself.). This is to allocate a special "fake"FileIdfor stdin to distinguish it when reading the file.typst-cli, the file reading logic inworld.rsis rewritten so that every read operation goes throughFileReader, which is a wrapper aroundFileIdthat chooses the correct behavior for the ID.world.rsthe fieldinputis changed fromPathBuftoOption<PathBuf>, and everywhere that uses it is changed too. Most of the use sites doesn't actually requireinputto be present, with two notable exceptions:compile.rs, theoutputmethod contains anexpectto unwrap theOption. This is ok because the CLI arg parsing ensures that user cannot have emptyoutputin this case, and they can get meaningful error if it is broken.watch.rsthe displayed filename defaults to<stdin>.args.rsthe positional argumentINPUThas its own type and parser now. This is to treat-specially and to work around the weird special casing ofOption<T>thatclapapplies. Code outside of this module only accesses the field through the method that converts it to anOption.(Solves #348 and partially #410)