Skip to content

allow resource/file enum/searching #6155

Closed as not planned
Closed as not planned

Description

@terefang

Description

how would a be able to pickup the filenames of resources contained in a particular asset folder ?

i would like to do something like below:

#let quarter-files-array = file.glob('/assets/backgrounds/quarter-*.jpg')

or

#let quarter-files-array = file.find('/assets/backgrounds/', filter-function)

or

#let quarter-files-array = file.scan('/assets/backgrounds/', filter-function)

Use Case

instead of hardcoding a template, the template can dynaically layout based on the particular resources found.

Comment

  • glob -- is the basic posix fnmatch/glob function.
  • find -- lists directory entries with attributes to be matched by the filter function
  • scan -- as find but also recursive into sub-directories

a good alternative would be to allow a glob/ant-like patterns (string or array of strings) instead of the filter-function like:

#let quarter-files-array = file.find('/assets/backgrounds/', 'quarter-*.jpg')
#let quarter-files-array = file.scan('/assets/backgrounds/', '**/quatter-*.png')

Activity

  1. added
    feature request
    on Apr 8, 2025
  2. added
    scripting
    change request
    and removed
    feature request
    on Apr 8, 2025
  3. added
    feature request
    and removed
    change request
    on Apr 8, 2025
  4. laurmaedje commented on Apr 8, 2025

    @laurmaedje
    Member

    I think #2123 is probably better as a base API since what you want could be built on top fairly easily.

  5. terefang commented on Apr 8, 2025

    @terefang
    Author

    while the primitives list_dir and is_dir might be a good start, you are basically asking every user to reinvent the wheel on top of it ?

    also what is missing is fnmatch/glob/ant-style matching, which again causes each user to reinvent the wheel.

    there are standard apis for it in libc:

    while in rust there is:

    and you might also need to expose dirname/basename/extname like functions from Path.

    should i also bring up the standard libs of other scripting languages (perl, python, lua, etc) ?

    there is also a speed issue to be considered.

    while the methods with match patterns could run entirely (and fast) in rust, doing (re-implementing) that in highlevel typst-script might be overkill.

  6. laurmaedje commented on Apr 8, 2025

    @laurmaedje
    Member

    while the primitives list_dir and is_dir might be a good start, you are basically asking every user to reinvent the wheel on top of it ?

    The filtering primitive you expose is really just a list + an array filter, so what is left beyond those is the glob. Glob is a fairly high-level feature and could easily be part of a package on Typst Universe, so that users don't have to reinvent the wheel.

    should i also bring up the standard libs of other scripting languages (perl, python, lua, etc) ?

    while in rust there is: [...]

    All the ones you list for Rust are non-std packages from crates.io.

    there is also a speed issue to be considered.

    while the methods with match patterns could run entirely (and fast) in rust, doing (re-implementing) that in highlevel typst-script might be overkill.

    Even if done on the Rust side, it would need to go through a standard World trait API, which would likely be one that just lists the directory. So just the filtering itself would be faster. I don't think that speed is a huge concern in this case with the number of files we are likely talking about. If it turns out yes, a glob package could even make use of a WebAssembly plugin.


    As a final addition, I would ask you to make your point in a bit less confrontative way next time. I'm not asking any users to reinvent any wheels, I'm just trying to find a sweet spot in terms of what are stdlib APIs and what is left to packages.

  7. terefang commented on Apr 9, 2025

    @terefang
    Author

    As a final addition, I would ask you to make your point in a bit less confrontative way next time. I'm not asking any users to reinvent any wheels, I'm just trying to find a sweet spot in terms of what are stdlib APIs and what is left to packages.

    maybe i misunderstood, but you pointed me to an issue talking at the end about having list_dir/is_dir api

    ... with the comment to build on top of that

    ... surely i felt compelled to have to reinvent the wheel myself, as might need others who have the same use-case.

    we might have different approaches in thinking about how such functionality should be implemented.

    for me this functionality is part of a set of standard libraries that have solved the problems for good (some even a long time ago).

    forgive me if i am oblivious to the details and implications of the rust module ecosystem.

    so i was under the impression if i find something on doc.rs with a MIT or Unlicense license status it should be unproblematic to consider.

    please forgive if i might sounded rude, provocative or uncaring, but my neuro-divergence is sometimes causing difficulties for others.

  8. laurmaedje commented on Apr 10, 2025

    @laurmaedje
    Member

    so i was under the impression if i find something on doc.rs with a MIT or Unlicense license status it should be unproblematic to consider.

    As I understood the argument, it was that other languages standard libraries ship with glob built-in and I was just trying to say that https://docs.rs/path-matchers/latest/path_matchers/ is not std, but more like Typst Universe. Rust has file listing in std, but glob is left to third-party packages, and I think for Typst a similar setup could work.

  9. laurmaedje commented on Apr 6, 2026

    @laurmaedje
    Member

    I'll close this as out of scope and something that could be built on top of #2123.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    feature requestscripting

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions