Skip to content
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 12 commits into from
Feb 5, 2024
Merged

Support reading input from stdin #3339

merged 12 commits into from
Feb 5, 2024

Conversation

s-cerevisiae
Copy link
Contributor

@s-cerevisiae s-cerevisiae commented Feb 4, 2024

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:

  • In typst-syntax, FileId gets a new method new_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.
  • In typst-cli, the file reading logic in world.rs is rewritten so that every read operation goes through FileReader, which is a wrapper around FileId that chooses the correct behavior for the ID.
  • Also in world.rs the field input is changed from PathBuf to Option<PathBuf>, and everywhere that uses it is changed too. Most of the use sites doesn't actually require input to be present, with two notable exceptions:
    • In compile.rs, the output method contains an expect to unwrap the Option. This is ok because the CLI arg parsing ensures that user cannot have empty output in this case, and they can get meaningful error if it is broken.
    • In watch.rs the displayed filename defaults to <stdin>.
  • In args.rs the positional argument INPUT has its own type and parser now. This is to treat - specially and to work around the weird special casing of Option<T> that clap applies. Code outside of this module only accesses the field through the method that converts it to an Option.

(Solves #348 and partially #410)

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)
Copy link
Member

@laurmaedje laurmaedje left a 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.

crates/typst-cli/src/args.rs Outdated Show resolved Hide resolved
crates/typst-cli/src/compile.rs Outdated Show resolved Hide resolved
crates/typst-cli/src/world.rs Outdated Show resolved Hide resolved
crates/typst-cli/src/world.rs Outdated Show resolved Hide resolved
crates/typst-cli/src/world.rs Outdated Show resolved Hide resolved
crates/typst-syntax/src/file.rs Outdated Show resolved Hide resolved
crates/typst-syntax/src/file.rs Outdated Show resolved Hide resolved
@s-cerevisiae
Copy link
Contributor Author

@laurmaedje All done! Thanks for the review.

@laurmaedje laurmaedje added this pull request to the merge queue Feb 5, 2024
@laurmaedje
Copy link
Member

Thank you!

Merged via the queue into typst:main with commit 70b354e Feb 5, 2024
4 checks passed
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.

None yet

2 participants