Nothing Special   »   [go: up one dir, main page]

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

doctest outputs ANSI escapes to non-color TTYs, resulting in visible junk #832

Open
snej opened this issue Nov 28, 2023 · 6 comments
Open
Labels
category/reporting Issues related to reporting functionality type/feature-proposal

Comments

@snej
Copy link
snej commented Nov 28, 2023

Description

doctest’s check for whether to enable color output is too simplistic: it just tests isatty(STDOUT_FILENO) (e.g. in the color_to_stream function.) But not all TTYs support ANSI color; one common exception is Xcode’s console pane.

This causes junk to appear in the output for just about anyone developing native Mac or iOS apps unless they disable color on the command line.

Steps to reproduce

Run doctest tests in an Xcode project, with output going to the console pane as usual.

  • Expected: Plain output
  • Actual: Lots of “[0;36m and the like in the console output

Extra information

On Unix systems you can test the TERM environment variable, but that doesn’t work on Windows. Here’s a reliable function that I’ve used in other projects:

static bool isColor(int fd) {
    if (!isatty(fd))
        return false;

    if (const char *term = getenv("TERM")) {
        if (strstr(term,"ANSI") || strstr(term,"ansi") || strstr(term,"color"))
            return true;
    }

#ifdef _MSC_VER
#ifndef ENABLE_VIRTUAL_TERMINAL_PROCESSING
#define ENABLE_VIRTUAL_TERMINAL_PROCESSING  0x0004
#endif
    if (GetRealOSVersion().dwMajorVersion >= 10) {
        HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
        DWORD consoleMode;
        if(GetConsoleMode(hConsole, &consoleMode)) {
            SetConsoleMode(hConsole, consoleMode | ENABLE_VIRTUAL_TERMINAL_PROCESSING);
        }
        return true;
    }
#endif

    return false;
}
@onqtam
Copy link
Member
onqtam commented Nov 29, 2023

Thanks! And for now you could use DOCTEST_CONFIG_COLORS_NONE

@cdeln cdeln closed this as completed Aug 15, 2024
@onqtam
Copy link
Member
onqtam commented Aug 16, 2024

I actually haven't added the code by @snej in the dev branch, and it would be cool if the library simply works by default - reopening

@onqtam onqtam reopened this Aug 16, 2024
@cdeln
Copy link
cdeln commented Aug 16, 2024

You must have, the -nc,--no-colors switch is implemented in 2.4.11 at least.

@cdeln
Copy link
cdeln commented Aug 16, 2024

I tried using #define DOCTEST_CONFIG_COLORS_NONE before including the doctest header and that works as well.

@onqtam
Copy link
Member
onqtam commented Aug 16, 2024

yeah, that works indeed, but it would be cool if the library detected as much as possible and handle stuff automatically so users don't have to dig through the docs - I don't see automatic detection as feature creep (while many other things could be considered as such)

@cdeln
Copy link
cdeln commented Aug 16, 2024

Sure thing, though I think it's more effort than just squeezing in an isatty call somewhere if that is supposed to work across all platforms without generating any compiler warnings. But it's possible that the change is trivial ofc.

@snej If you are up for it then you can have a stab at implementing it and make a PR.

@cdeln cdeln added type/feature-proposal category/reporting Issues related to reporting functionality and removed enhancement labels Aug 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
category/reporting Issues related to reporting functionality type/feature-proposal
Projects
None yet
Development

No branches or pull requests

3 participants