console-rs / console

A rust console and terminal abstraction
MIT License
932 stars 111 forks source link

support for synchronized output #195

Open Funami580 opened 9 months ago

Funami580 commented 9 months ago

I got the advice that support for synchronized output should be implemented in this crate instead of indicatif (related indicatif PR: https://github.com/console-rs/indicatif/pull/618).

Currently, only checking whether synchronized output is supported is implemented. Before I continue, I want to know what the public API should look like (previous examples: https://github.com/console-rs/indicatif/pull/618#issuecomment-1872589850).

@djc already suggested to use guards, and that I should ask the console maintainers, too. I'd also like to see some vague code for the public API, for example:

let term = Term::stdout();
if term.supports_sync_update() {
    term.begin_sync_update();
}
// do something
if term.supports_sync_update() {
    term.end_sync_update();
}
term.flush();

Another thing to notice is that we don't necessarily need to check whether that feature is supported, as terminals should ignore the control sequence (e. g. begin sync: \x1b[?2026h) if they don't support it, I think. But we could still provide the check function to the end users, as they might make use of that information.

mitsuhiko commented 8 months ago

It does make sense to me, but I'm not entirely sure if having such a stateful external API is not a massive footgun. Unfortunately since you actually need to flip those flags on the term I'm not sure if something nice can be done. There is nothing that prevents you from having two terminals to stdout/stderr in this crate. So anyone flipping this flag would obviously affect the output.

If such an API is added, I think a guard like in your other PR should be added as well.

Definitely not opposed though.