-
-
Notifications
You must be signed in to change notification settings - Fork 797
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support reading input from stdin #3339
Merged
Merged
Conversation
This file contains 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
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks very good! Just a few remarks.
@laurmaedje All done! Thanks for the review. |
Thank you! |
1 task
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
,FileId
gets 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"FileId
for stdin to distinguish it when reading the file.typst-cli
, the file reading logic inworld.rs
is rewritten so that every read operation goes throughFileReader
, which is a wrapper aroundFileId
that chooses the correct behavior for the ID.world.rs
the fieldinput
is changed fromPathBuf
toOption<PathBuf>
, and everywhere that uses it is changed too. Most of the use sites doesn't actually requireinput
to be present, with two notable exceptions:compile.rs
, theoutput
method contains anexpect
to unwrap theOption
. This is ok because the CLI arg parsing ensures that user cannot have emptyoutput
in this case, and they can get meaningful error if it is broken.watch.rs
the displayed filename defaults to<stdin>
.args.rs
the positional argumentINPUT
has its own type and parser now. This is to treat-
specially and to work around the weird special casing ofOption<T>
thatclap
applies. Code outside of this module only accesses the field through the method that converts it to anOption
.(Solves #348 and partially #410)