sway
sway copied to clipboard
Refactor formatted `stdout`/`stderr` writing (e.g. bold, colored) in `forc` to be closer to `fuelup`s implementation
Currently forc has quite the mess of custom functions for outputting to stdout with certain colours or other formatting.
We should refactor this to be closer to fuelup's implementation which is quite a bit more concise.
Rather than writing directly to stdout (as is the case in fuelup), we should instead see if we can write formatted output via tracing to allow for formatted output to be filtered/handled in the same way the rest of the output is. We might need to consider looking into an alternative or custom tracing subscriber implementation to achieve this.
Edit: #1661 is related to this.
@mitchmindtree Alright if I close this?
I think we can leave this one open as this issue is more about taking on fuelup's approach to handling coloured and bold writing. See this fuelup module for an example with its bold and colored_bold implementations. You can see an example of their use here.
The benefit of fuelup's approach is that when we want to write a coloured or bold string that also happens to be formatted, we don't need to allocate an intermediary String using format! - instead, we can instead use write! to write directly to the stream.
E.g. today in forc, printing a coloured string with foo and bar formatted, requires first allocating a String using format! before it gets passed by reference to the println function:
println_green(&format!("{}-{}", foo, bar));
The equivalent fuelup approach might instead look like this:
fmt::green(|s| writeln!(s, "{}-{}", foo, bar));
Here, rather than allocating an intermediary string, we instead write foo and bar formatted together directly to the stream s.
All that said, this is by no means a high priority :) Just something that could be nice to solve if we can work it out. It's also not at all clear to me yet how we'd provide an API like fuelup's while also writing to the tracing logger (the example above just writes to stdio).
This doesn't seem compatible with tracing because with tracing there is no direct access to the stdout stream. With tracing you have to create an event which the subscriber listens for. Each event needs to own it's own fields and refer to an allocated string.
With tracing, the equivalent would be:
tracing::info!("{}-{}", Colour::Green.paint(foo), Colour::Green.paint(foo));
One possible advantage here is you can have multiple colors in one line, or highlight one word with a color.
Or if the whole line is green:
tracing::info!(Colour::Green.paint(&format!("{}-{}", foo, bar)));
Ahh thanks for digging into this @sdankel and clarifying! Good point about tracing not exposing any kind of stream abstraction - happy to close this in favour of your suggested approach :+1: