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

Resolving imports for Rust code execution #365

Closed
orhun opened this issue Sep 9, 2024 · 10 comments · Fixed by #366
Closed

Resolving imports for Rust code execution #365

orhun opened this issue Sep 9, 2024 · 10 comments · Fixed by #366

Comments

@orhun
Copy link
Contributor

orhun commented Sep 9, 2024

Hey again!

Is there a way to handle dependencies when using executable code blocks in the presentation. For example, I want to import a library like so:

```rust +exec
use ratatui::{
    crossterm::event::{self, Event, KeyCode, KeyEventKind},
    widgets::{Block, Paragraph},
};
```

But unfortunately this fails (when I do C-e) with import errors.

Is this currently possible to do so with presenterm? If not, would you be interested in supporting this?

@mfontanini
Copy link
Owner

Someone added support for rust-script so if you tag the code block with rust-script instead of rust and use their syntax it should work. Once cargo-script is stabilized (rust-lang/cargo#12207) I would expect to have rust code blocks switch to using this directly and rust-script would be deprecated.

I haven't really used this but it should work. Let me know if it doesn't!

@orhun
Copy link
Contributor Author

orhun commented Sep 9, 2024

Oh, that's perfect! It partly solves my problem.

Now the real challenge is:

````rust-script +line_numbers +exec
//! ```cargo
//! [dependencies]
//! ratatui = "0.28.1"
//! ```

use ratatui::{
    crossterm::event::{self, Event},
    text::Text,
    Frame,
};

fn main() {
    let mut terminal = ratatui::init();
    loop {
        terminal
            .draw(|frame: &mut Frame| {
                frame
                    .render_widget(Text::raw("Hello World!"), frame.area())
            })
            .expect("failed to draw frame");

        if matches!(
            event::read().expect("failed to read event"),
            Event::Key(_)
        ) {
            break;
        }
    }
    ratatui::restore();
}
````

I can't run TUI applications inside presenterm (since the terminal buffer is already taken by it) :/

Is there a way to <!-- suspend --> and <!-- resume --> the presenterm app for normally using the terminal and executing the code? (similar to this tutorial)

It would be super awesome to have this feature somehow since my presentation will be about TUIs. Otherwise I need to spawn another terminal to show the output of the code.

@mfontanini
Copy link
Owner

I created a PR for this #366, you need to annotate the code snippet with +exec +acquire_terminal and it should work. I still need to handle errors properly before I merge it but for now this seems to work. Can you give it a shot and let me know if something looks off? (besides stderr not being captured, that I'm aware of and may be able to get to it later today/tomorrow).

@orhun
Copy link
Contributor Author

orhun commented Sep 10, 2024

Thanks for the quick implementation! It works quite well with the code block I provided above. I also tried out different examples and the result is chef's kiss.

However, I realized now that the code blocks that are not executed yet say [not started] at the bottom. e.g.:

image

Also, inline links (e.g. <https://example.com>) renders a bit different:

image

I'm not sure if those are new features that are not released yet or a bug caused by the PR.

@mfontanini
Copy link
Owner

However, I realized now that the code blocks that are not executed yet say [not started] at the bottom. e.g.:

Yeah, I wasn't really sure what to do here. There's no "starting" this as it runs every time you run it, as opposed to normal snippet execution that can only run once. Any ideas on what to replace it with?

Also, inline links (e.g. https://example.com) renders a bit different:

This was implemented in #334 and is not released yet. To get the previous behavior you need to have links with empty titles, e.g. [](https://github.com/...).

@orhun
Copy link
Contributor Author

orhun commented Sep 10, 2024

Yeah, I wasn't really sure what to do here. There's no "starting" this as it runs every time you run it, as opposed to normal snippet execution that can only run once. Any ideas on what to replace it with?

It feels a bit verbose to add another label. Also, I didn't quite get the case where this is shown. Can you elaborate it further?

To get the previous behavior you need to have links with empty titles, e.g

Ah, nice feature :) Updated my slides accordingly, thanks for pointing it out!

@mfontanini
Copy link
Owner

This example showcases this asciicast.

The label/bar is there to display the state of the execution of the snippet. For the new acquire terminal mode, there is no state as you're either running presenterm or running the snippet. Once the snippet ends, control flow goes back to presenterm. I think it would be good to have some way of indicating this is a runnable script that will acquire the terminal but I don't know off the top of my head how (see #278 for rationale on this).

@orhun
Copy link
Contributor Author

orhun commented Sep 12, 2024

Ah, got it. Thanks for the explanation - I think it makes sense!

I had some time to test acquire_terminal further and it works pretty well. Any chance that it is going to be released soon?

@mfontanini
Copy link
Owner

Any chance that it is going to be released soon?

There's a bunch of changes going out in the next release but I need to do some relatively major fix on something that's going to take a bit of time. I'll make sure something goes out before your talk, that should be enough time for me to work on this.

@orhun
Copy link
Contributor Author

orhun commented Sep 13, 2024

Sounds good to me! I will be using the git version in the meantime and let you know if I experience anything.

Thanks for your work on this 💯

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 a pull request may close this issue.

2 participants