fmtlib / fmt

A modern formatting library
https://fmt.dev
Other
20.56k stars 2.46k forks source link

add disable_colors #3918

Closed Toni500github closed 6 months ago

Toni500github commented 6 months ago

disable_colors is just a normal bool which the dev or the user can decide in the program if disable the printing of colors or not.

an example:

#include <fmt/color.h>

int main() {
  // prints "Hello" in red fg
  fmt::print(fg(fmt::terminal_color::red), "Hello\n");
  // let's disable all colors prints
  fmt::disable_colors = true;
  // prints "Hello" in blank color
  fmt::print(fg(fmt::terminal_color::red), "Hello\n");
}

this can be also useful in cases where the user has the enviroment variable NO_COLOR set (https://no-color.org). another example:

#include <fmt/color.h>

int main() {
    char *no_color = getenv("NO_COLOR");

    // prints "Hello" in red fg
    fmt::print(fg(fmt::terminal_color::red), "Hello\n");

    if (no_color != NULL && no_color[0] != '\0')
        fmt::disable_colors = true;

    // will prints "Hello" in blank color if we have $NO_COLOR set, else it will print it in red
    fmt::print(fg(fmt::terminal_color::red), "Hello\n");
}

Fixes #2137

Toni500github commented 6 months ago

if needs any changes just let me know

vitaut commented 6 months ago

Thanks for the PR but I don't think it's a good idea to introduce global state like this. You can write your own wrapper function that does this.