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

click.secho() on Windows strips color information despite color=True. (Windows only #2791

Closed
zapta opened this issue Oct 24, 2024 · 1 comment

Comments

@zapta
Copy link

zapta commented Oct 24, 2024

Summary:

click.secho(, color=True) is supposed to preserve text coloring despite writing to a pipe. This works correctly on Mac (colors are preserved) but not on Windows the colors are stripped.

To reproduce:

Save the two files below in a directory and run python parent.py. The parent job will launch the child job which will output to the stdout pipe colored text. The parent jobs echoes that text and it should be colored (green).

File child.py

import click
import sys

# A message with color info.
msg = click.style("GREEN", fg="green")

# Dump message to stderr. This bypasses the parent job.
msg_hex = " ".join("{:02x}".format(ord(c)) for c in msg)
print(f"[CHILD ] Sending  (as hex): [{msg_hex}]", file=sys.stderr)

# Write to stdout. Should be send to the parent via pipe while
# preserving the coloring. 
# * reset=False prevents appending color off sequence.
# * nl="" prevents append EOL char.
click.secho(msg, color=True, reset=False, nl="")

File parent.py

import subprocess, time, os, sys

# Run the child process.
child = subprocess.Popen(["python", "child.py"],
                     stdout=subprocess.PIPE,
                     stderr=None)

# Dump the captured stdout.
for line in iter(child.stdout.readline, b''):
    # Dump line as hex.
    hex_dump =  " ".join("{:02x}".format(c) for c in line)
    print(f"[PARENT] recieved (as hex): [{hex_dump}]")

    # Dump line as text. Should print colored text.
    str = line.decode("utf-8")
    print(f"[PARENT] recieved (as str): {str}")

Expected abehavior:

On Mac the behavior is correct and the text that the parent echoes from the pipe is colored.

image

Actual behavior:

On windows, however, the ansi colors are stripped (see the shorter hex string) and the output text is not colored.

image

Environment:

  • Python version: 3.12.0
  • Click version: 8.1.7
@davidism
Copy link
Member

Duplicate of #2606

@davidism davidism marked this as a duplicate of #2606 Oct 24, 2024
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 8, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants