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

Clear previously colored string #89

Closed
azzamsa opened this issue Jan 16, 2023 · 1 comment
Closed

Clear previously colored string #89

azzamsa opened this issue Jan 16, 2023 · 1 comment

Comments

@azzamsa
Copy link

azzamsa commented Jan 16, 2023

Hi.

Is there any way to clear all the colored styles in the string?
I tried default_color - OwoColorize in owo_colors , but no luck.

use owo_colors::OwoColorize;

fn main() {
    let colored_output: bool = true;

    let is_formatted = format!("{} files formatted", 5)
        .green()
        .to_string();
    let is_unchanged = format!("{} files unchanged", 10);
    let is_failed = format!("{} files failed to format", 4)
        .red()
        .to_string();
    let message = format!("{}. {}. {}", is_formatted, is_unchanged, is_failed);
    println!("{}", message.default_color());
}

The color is still there

image

Do we have something like:

  1. clear - colored::Colorize
  2. if_support_color that accepts a boolean value and returns String. if_supports_color(&'a self, colored_output: bool, apply: ApplyFn). The idea is similar to set_override in owo_colors - Rust, but I I need something like if_support_color that return String.

Otherwise, I need to use if-else everywhere to check if the user wants the color colored_output.

use owo_colors::OwoColorize;

fn main() {
    let colored_output: bool = true;

    let is_formatted = if colored_output { // ⚠ ⚠
        format!("{} files formatted", 5).green().to_string()
    } else {
        format!("{} files formatted", 5)
    };
    let is_unchanged = format!("{} files unchanged", 10);
    let is_failed = if colored_output { // ⚠ ⚠
        format!("{} files failed to format", 4).red().to_string()
    } else {
        format!("{} files failed to format", 4)
    };

    let message = format!("{}. {}. {}", is_formatted, is_unchanged, is_failed);
    println!("{}", message.default_color());
}

Or, do you have any better approach?

Thanks for owo-colors. ❤️

@azzamsa
Copy link
Author

azzamsa commented Jan 16, 2023

Solved! 🔥

use owo_colors::{OwoColorize, Stream::Stdout};

fn main() {
    owo_colors::set_override(false);

    let is_formatted = format!("{} files formatted", 5);
    let is_unchanged = format!("{} files unchanged", 10);
    let is_failed = format!("{} files failed to format", 4);
    println!(
        "{}. {}. {}",
        is_formatted.if_supports_color(Stdout, |text| text.green()),
        is_unchanged,
        is_failed.if_supports_color(Stdout, |text| text.red()),
    );
}

@azzamsa azzamsa closed this as completed Jan 16, 2023
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

No branches or pull requests

1 participant