Skip to content

Help/viewer output emits raw ANSI when stdout is redirected (no plain-text fallback / NO_COLOR) #39

Description

@tig

What happened

Noticed while wiring TG.Cli into mcec (tig/mcec#252): piping or redirecting help output captures the escape sequences verbatim.

myapp --help > help.txt     # help.txt is full of ESC[39m ESC[49m ...
myapp help | some-tool      # same

MarkdownRenderer.RenderToAnsi deliberately skips the Console.OutputEncoding fixup when Console.IsOutputRedirected, but it still renders and writes ANSI. Anything that shells out to the CLI and reads stdout (scripts, CI logs, agents that were told to run myapp help) gets escape-littered text.

Suggestion

When Console.IsOutputRedirected is true (or NO_COLOR is set), degrade to plain text: either strip the escapes from the rendered output (a sanitizer pass exists already in TerminalEscapeSanitizer) or render the markdown without color attributes. --opencli/--json stay the machine-readable paths, but plain --help | less should not be garbage.

If the current behavior is intentional (keep colors through pipes for less -R style pagers), a --no-color global and a NO_COLOR env check would still be worth it, plus a line in the docs.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions