console-rs / indicatif

A command line progress reporting library for Rust
MIT License
4.22k stars 238 forks source link

Using `instant::Instant` when compiling to WASM target #513

Closed azriel91 closed 1 year ago

azriel91 commented 1 year ago

Heya, I'm using indicatif in a WASM example, and it doesn't initialize when std::time::Instant is used.

I've got a branch (https://github.com/console-rs/indicatif/pull/514) where I've toggled to using instant::Instant based on the target architecture. Would this be something you're happy to take?

#[cfg(not(target_arch = "wasm32"))]
use std::time::Instant;

#[cfg(target_arch = "wasm32")]
use instant::Instant;
azriel91 commented 1 year ago

Thank you :bowing_man:.

sigmaSd commented 1 year ago

I'm curious how are you using indicatif in wasm ? is it not currently hard-coded to stdout/stderr, or do you mean you just want it to compile?

azriel91 commented 1 year ago

Heya, let's answer that in parts:

is it not currently hard-coded to stdout/stderr

not completely! ProgressBar::with_draw_target lets you pass in any ProgressDrawTarget, for which you can create a ProgressDrawTarget::term_like, and use InMemoryTerm for example.

do you mean you just want it to compile?

it's a start :upside_down_face:

how are you using indicatif in wasm

The smallest usage is to use indicatif to track state (ETA, progress, message, ..), and read those values and create a custom rendering.

If I wanted the progress bars to be rendered by indicatif in a WASM app, then one has to:

magic :magic_wand: :smile:

image

I haven't done the InMemoryTerm thing yet, but that screenshot is the output of a to-html converted progress bar output

sigmaSd commented 1 year ago

Hello, thanks for the detailed answer!

mikeymop commented 9 months ago

If I wanted the progress bars to be rendered by indicatif in a WASM app, then one has to:

* Use the `InMemoryTerm` as described above.

* Call `tick()` manually to render the terminal (no steady tick, because no threads).

* Pass the in-memory term string through [`ansi_to_html`](https://docs.rs/ansi-to-html/0.1.3/ansi_to_html/)

Hello, would you be so kind as to expand on this in a bit? I am trying to use indicatif to render within a wasm32 binary intended for use within a terminal.

I've tried building to wasm32-wasi and wasm32-wasmer-wasi with the ultimate goal of being POSIX compliant.

Using

clap = { version = "4.4.4", default-features = false, features = ["derive", "std", "help", "usage", "error-context", "suggestions"] }
indicatif = "0.17.7"