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

[feature] Don't add escape sequences to stdout of (some) query commands, regardless of CLICOLOR_FORCE #14614

Closed
1 task done
BobIsOnFire opened this issue Aug 30, 2023 · 2 comments · Fixed by #14642
Closed
1 task done
Assignees

Comments

@BobIsOnFire
Copy link
Contributor

What is your suggestion?

Hi!

My use case: I am running various conan commands in a wrapper script. The wrapper is using its own buffering when running subprocesses, so when I run the script in the terminal, conan doesn't know that it actually runs in terminal, and disables colors in logs. I set CLICOLOR_FORCE env variable when I want to see colored logs, which works nicely for me.

Sadly, when I want to capture stdout of a command and do something with it, CLICOLOR_FORCE adds unwanted escape sequences and breaks my script:

$ conan list 'zlib/*' --format=json | cat -A
{$
    "Local Cache": {$
        "zlib/1.2.13": {}$
    }$
}$
$ CLICOLOR_FORCE=1 conan list 'zlib/*' --format=json | cat -A
{$
    "Local Cache": {$
        "zlib/1.2.13": {}$
    }$
}^[[0m$

Same can be observed if I run simple conan config home. This is kinda expected since I add a variable to force colors, but JSON output format is meant to be processed automatically, so I think it shouldn't be touched with color decorations at all.

Have you read the CONTRIBUTING guide?

  • I've read the CONTRIBUTING guide
@BobIsOnFire
Copy link
Contributor Author

It could make sense to add color decorators in cli_out_write only when either fg or bg is not None, this would be enough for my use case. I can prepare a PR myself if it is OK with you!

@memsharded memsharded self-assigned this Aug 30, 2023
@memsharded
Copy link
Member

Hi @BobIsOnFire

Thanks for the report and the investigation.

I agree, when printing to stdout any json it shouldn't use any escape sequences at all, irrespective of all colors enabled or forced. It makes sense to fix cli_out_write() method so this doesn't happen. It would also make sense to use print() or sys.stdout.write() for many outputs like json files that don't use colors, but probably fix cli_out_write() will be simpler. Thanks for offering to submit a PR!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants