Skip to content

Add --ansi option to force Ansi output #5081

Description

Summary

Add the complement to --no-ansi, that forces --ansi output, even if output is redirected.

Background and Motivation

When running the tests from a PowerShell script, I may want to display the test output but not have it as output from the PowerShell function.

mytest.exe | Out-Host
if ($LASTEXITCODE){
   throw "test failed"
}

This removes all coloring from the output, even though it's logically equivalent to passing it directly to the terminal for display.

Proposed Feature

Add --ansi parameter that forces ansi output, regardless of redirection. Let the user decide.

Alternative Designs

Always output ansi unless the user explicitly uses --no-ansi.
That has the drawback of potentially breaking existing scripts.

Activity

  1. Evangelink commented on Feb 20, 2025

    @Evangelink
    Member

    It's probably a quick and easy update so I don't see why not! Thanks for the suggestion Staffan Gustafsson (@powercode).

    cc Mariam Abdullah (@mariam-abdulla) as it might require some porting to dotnet test.

  2. added
    area/mtp
    area/terminal-reporter
    and removed
    needs/triage
    on Feb 20, 2025
  3. bradwilson commented on Apr 12, 2025

    @bradwilson

    Forcing ANSI should only be done if you know if you're running inside of a terminal that can handle it. The standard console host (CONHOST), for example, does not support ANSI:

    Image

  4. Evangelink commented on Apr 14, 2025

    @Evangelink
    Member

    Brad Wilson (@bradwilson) I agree and that's why this is not the default behavior. The default is trying to auto-detect if we can use ansi or not. We then have flags to force using or not using ANSI.

  5. nohwnd commented on May 5, 2025

    @nohwnd
    Member

    moving to 3.10, I think we need a better design for this, having --ansi and --no-ansi, leads to many combinations with similar options like --no-color and so on.

  6. Evangelink commented on May 5, 2025

    @Evangelink
    Member

    Jakub Jareš (@nohwnd) what about --ansi <auto|enable|disable>, --color <enable|disable> (this one may need some deps on ansi?).

    I think gradle (java) had something interesting where cli has only part of the commands, for extra granularity you needed the config file. Maybe a similar design could help so we don't have to support it all as CLI.

  7. nohwnd commented on May 6, 2025

    @nohwnd
    Member

    yes those are all good options, terminal logger does something similar with handling auto, and on/off true/false 0/1.

    Then it is just question of deprecating no-ansi no-color or not, but we cannot do that right away anyway.

  8. Youssef1313 commented on Jul 11, 2025

    @Youssef1313
    Member

    Jakub Jareš (@nohwnd) I moved to "Future" milestone. But feel free to move to "2.0.0" as a breaking change (and if you did, please add it to #5762 and add the "Breaking !!" label)

  9. Evangelink commented on Nov 28, 2025

    @Evangelink
    Member

    Given the v2 was missed, what about introducing the new API and deprecating the old ones (but keeping them working)? It feels like something we can implement quickly.

  10. Youssef1313 commented on Jan 8, 2026

    @Youssef1313
    Member

    Instead of going into the hassle of deprecations, aligning dotnet test implementation with MTP, etc.. I would suggest keep using the current --no-ansi and updating it such that it accepts one of three values:

    • auto (same as when omitted --no-ansi is completely omitted): we determine ourselves whether or not to use ansi terminal)
    • true (same as when --no-ansi is present without an arg): Force non ansi.
    • false: Force ansi.

    Double negation is bad, but this is IMO the approach with the least hassle.

  11. Youssef1313 commented on Jan 20, 2026

    @Youssef1313
    Member

    There is actually a 4th mode, simple ANSI. This is what we use in CI to show colors using ANSI, but we cannot move cursor to show progress in-place. This needs more design work.

    Given how niche this scenario is, and that in most cases the default behavior will be what the user needs, I think we might simply go with env variables. The proposed flow is:

    • --no-ansi is still present and is highest precedence. If explicitly passed in command-line, we force-disable ansi. Note: we might still produce colors even in this mode, but we fully rely on Console.ForegroundColor` API in this case (which will still attempt to write ANSI codes on Unix depending on this detection for ANSI, but uses Win32 APIs for changing color under Windows).
    • Env variable MICROSOFT_TESTING_PLATFORM_ANSI which is either enable, disable, simple.
      • enable forces use of ANSI terminal (the request in this issue)
      • disable force disables ANSI terminal (equivalent to --no-ansi)
      • simple force use of simple ANSI terminal (emits ANSI colors only).
    • If user didn't specify anything, we fallback to auto detection:
      • detect CI scenarios where we use simple ANSI.
      • Otherwise, detect if running environment is ANSI-capable or not.
  12. Youssef1313 commented on Jan 20, 2026

    @Youssef1313
    Member

    Alternatively, we could re-use the same env variable as .NET, DOTNET_SYSTEM_CONSOLE_ALLOW_ANSI_COLOR_REDIRECTION.

    I would also like to understand if the scenario here is about coloring only. In that case it's more of forcing simple ansi, not the full ansi implementation.

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

Metadata

Metadata

Labels

area/mtparea/terminal-reporter

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions