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

Provide a mechanism to disable color formatting globally #2137

Closed
alliepiper opened this issue Feb 18, 2021 · 5 comments
Closed

Provide a mechanism to disable color formatting globally #2137

alliepiper opened this issue Feb 18, 2021 · 5 comments

Comments

@alliepiper
Copy link

alliepiper commented Feb 18, 2021

It would be nice to disable text styles globally. Ideally, both:

  1. An API call or global variable for applications to use, and
  2. An environment variable that users could set when an application doesn't expose this option.

Some terminals (MSVC dev terminals are a glaring example) can't handle the formatting markup, and produce unreadable output when formatting is used (#1794).

#1549 suggests a way to implement this by placing special logic at every call into fmtlib, but this is burdensome and error prone. Having a library-wide solution for this problem would be fantastic.

If there's already a good workaround for this, please let me know. I found some closed issues that were related, but nothing that quite does what I'm looking for here.

@nenin-sc
Copy link

nenin-sc commented Feb 19, 2021

You can try do something like this to force win console in appropriate mode somewhere in the beginning of your code:

 HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
  DWORD dwMode = 0;
  GetConsoleMode(hOut, &dwMode);
  dwMode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
  SetConsoleMode(hOut, dwMode);

In MinGW it working.

@alliepiper
Copy link
Author

That's a useful workaround, though I'll still need a way to disable color for file output, etc.

I'll see about enabling this workaround in my app, but long term -- is this something that fmtlib could do automatically? It'd be great if this worked out-of-the-box.

@vitaut
Copy link
Contributor

vitaut commented Feb 20, 2021

Thanks for the suggestion. I don't think that {fmt} should provide a mechanism to suppress color output based on the global state but you can easily write a formatting function that does this:

#include <fmt/color.h>

bool disable_color = false;

template <typename... Args>
void myprint(const fmt::text_style& ts, const Args&... args) {
  fmt::print(disable_color ? fmt::text_style() : ts, args...);
}

int main() {
  myprint(fg(fmt::terminal_color::red), "Hello\n");
  disable_color = true;
  myprint(fg(fmt::terminal_color::red), "Hello\n");
}

Godbolt: https://godbolt.org/z/bczTor

Additionally, it would be nice to fix color formatting on Windows and/or provide an API that enables ANSI escape sequences (basically what @nenin-sc suggested). PRs would be welcome =).

@vitaut vitaut closed this as completed Feb 20, 2021
@alliepiper
Copy link
Author

Fair enough :)

I'll probably just disable color on cmd for now, but if I find some time to fix it for real later I'll make a PR here.

@kvtb
Copy link

kvtb commented Oct 21, 2021

Using own wrappers might be a solution if one is using fmt directly.
But if one uses another library (for example folly) which use fmt, then own wrappers should be inserted between two 3rd party libraries, which is difficult to maintain.

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

Successfully merging a pull request may close this issue.

4 participants