Support disabling colors
I couldn't figure out if there is a switch to disable colors on demand like if_supports_color or the various env vars.
In case you'd accept patches to use different ansi backends?
Hi @lu-zero
What do you imply by disable color?
You mean that if input string contains some ANSI colors but you don't wanna show it?
In case you'd accept patches to use different ansi backends?
We actually have no ANSI backends at all.
Meaning no libraries is used.
We only have a small set of features to help with it, but they are library agnostic, like Color https://docs.rs/tabled/latest/tabled/settings/struct.Color.html#example.
So what do you mean here?
If so you could use any library you want to achieve it. Like so.
table.with(Format::content(|s| {
ansi_str::AnsiStr::ansi_strip(s).to_string()
}));
There's no this shorthand currently for it; But I guess it's worth adding. If you have time you could try to file a PR.
So it would be called like this.
table.with(Charset::strip());
See https://github.com/zhiburt/tabled/blob/fd48cef72a3e87beada497689509f9ab7b5823dd/tabled/src/settings/formatting/charset.rs
I'd love to have a function that:
- checks the env according to one of those de-facto specs
- acts accordingly removing the ansi colors if they aren't supported and/or disabled
Probably adding this kind of filter might be already good :)
I am convinced it would be a client code responsibility to decide whether to use colors or not. You could literally use any color library and colorize input data.
Or you could just use if statement 😅 and Color combination.
See.
Notice that Color usage is beneficial if you are colorizing all string (not partially). Cause we don't do any changes to original string you must make otherwise. So less allocations. So generally must be faster.
use std::iter::{once, FromIterator};
use tabled::{
settings::{object::Columns, Color},
Table,
};
fn use_colors() -> bool {
match option_env!("COLOR") {
Some("1") => true,
_ => false,
}
}
fn main() {
let data = once(["lu-zero", "https://github.com/lu-zero"])
.chain(once(["lu0", "https://github.com/lu0"]));
let mut table = Table::from_iter(data);
if use_colors() {
table.modify(Columns::first(), Color::BG_BLACK | Color::FG_BLUE);
table.modify(Columns::last(), Color::BG_BLACK | Color::FG_RED);
}
println!("{table}");
}
The same example with owo-colors. Notice requires ansi feature (tabled = { version = "0.19", features = ["ansi] }).
use std::iter::{once, FromIterator};
use tabled::Table;
use owo_colors::OwoColorize;
fn main() {
let data = once([
"lu-zero"
.if_supports_color(owo_colors::Stream::Stdout, |text| text.bright_blue())
.to_string(),
"https://github.com/lu-zero"
.if_supports_color(owo_colors::Stream::Stdout, |text| text.bright_red())
.to_string(),
])
.chain(once([
"lu0"
.if_supports_color(owo_colors::Stream::Stdout, |text| text.bright_blue())
.to_string(),
"https://github.com/lu0"
.if_supports_color(owo_colors::Stream::Stdout, |text| text.bright_red())
.to_string(),
]));
let table = Table::from_iter(data);
println!("{table}");
}
If I misunderstand something please ping me to it :) And if you @lu-zero need some help let me know.
I need to experiment a little, but I possibly there isn't a lot or anything to change here. Thank you a lot!