-
-
Notifications
You must be signed in to change notification settings - Fork 8
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
question: Forcing colors? #30
Comments
Both of import subprocess
import os
output = subprocess.check_output(
["python", "test.py", "--help"],
env={**os.environ, "FORCE_COLOR": "1"},
)
print(output) and # foo.py as FORCE_COLOR=1 python foo.py
import subprocess
output = subprocess.check_output(["python", "test.py", "--help"])
print(output) Seem to output the ansi escape codes with my local testing EDIT: woops, where |
The internal interface of bypassing from dataclasses import dataclass
import cappa
from cappa.help import format_help
@dataclass
class Main:
asdf: str
command = cappa.Command(Main)
final_command = cappa.Command.collect(command)
# plus or minus
help_arg = create_help_arg()
version_arg = create_version_arg()
completion_arg = create_completion_arg()
final_command.add_meta_actions(
help=help_arg, version=version_arg, completion=completion_arg
)
# end plus or minus
help = format_help(final_command, "insiders")
cappa.Output.from_theme().output(help) This is obviously not ideal, because it's what cappa is doing internally rather than a purposely public API :P |
That's weird. The following command does not print colors on my end: FORCE_COLOR=1 python -c "import subprocess, os; print(subprocess.check_output('python -minsiders -h'.split(), text=True))" Same thing in an interactive session 🤔 This doesn't work either: FORCE_COLOR=1 echo -e "$(python -m insiders -h)" |
Although your first example does print color for me |
I had tried |
OK, that was a painful investigation.
Conclusion: colors stop showing if I import We can close this, as there's nothing actionable for Cappa here, and #35 is more suited for the second question. |
To document my CLI, I use Markdown Exec which is able to execute code blocks and inject their output into the page, while converting ANSI colors to HTML style. My goal is to render the help message of my CLI with colors.
However, the code executed by Markdown Exec is executed in a subprocess, using pipes to capture standard output and error (
subprocess.PIPE
), and Cappa seems to do the common thing of detecting such pipes and disabling colors.I initially thought that I could override this with the
FORCE_COLOR=1
environment variable, since the Cappa backend uses Rich to print the help message. Unfortunately setting the variable seems to have no effect 🤔I'm now trying to manually print the help message from Python, to investigate, but I'm getting lost in the code. Here's what I have:
...but it only prints
Usage: insiders
, without any colors.So my questions are:
The text was updated successfully, but these errors were encountered: