-
Notifications
You must be signed in to change notification settings - Fork 275
Show colors in AzDo and GH actions #5535
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
Open
nohwnd
wants to merge
13
commits into
main
Choose a base branch
from
use-ansi-terminal-in-azdo
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
8f9e4a9
colors?
nohwnd 7ea4e6c
out
nohwnd 6313cbe
Fix assumption in exit tests, and avoid tight loop
nohwnd 4ce45a8
ignore output test
nohwnd c67458c
Merge branch 'main' into use-ansi-terminal-in-azdo
nohwnd 0be95eb
simple ansi
nohwnd 43ba144
Remove output
nohwnd 46bc03b
fix colors and write foreground color per line
nohwnd ea9eccb
Fix tests
nohwnd de9d398
revert versioning
nohwnd 2453771
Merge branch 'main' into use-ansi-terminal-in-azdo
nohwnd 0632b15
Fix indentation
nohwnd 56783dc
Merge branch 'main' into use-ansi-terminal-in-azdo
Evangelink File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
src/Platform/Microsoft.Testing.Platform/OutputDevice/Terminal/SimpleAnsiTerminal.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using Microsoft.Testing.Platform.Helpers; | ||
|
||
namespace Microsoft.Testing.Platform.OutputDevice.Terminal; | ||
|
||
/// <summary> | ||
/// Simple terminal that uses 4-bit ANSI for colors but does not move cursor and does not do other fancy stuff to stay compatible with CI systems like AzDO. | ||
/// The colors are set on start of every line to properly color multiline strings in AzDO output. | ||
/// </summary> | ||
internal sealed class SimpleAnsiTerminal : SimpleTerminal | ||
{ | ||
private string? _foregroundColor; | ||
private bool _prependColor; | ||
|
||
public SimpleAnsiTerminal(IConsole console) | ||
: base(console) | ||
{ | ||
} | ||
|
||
public override void Append(string value) | ||
{ | ||
// Previous write appended line, so we need to prepend color. | ||
if (_prependColor) | ||
{ | ||
Console.Write(_foregroundColor); | ||
// This line is not adding new line at the end, so we don't need to prepend color on next line. | ||
_prependColor = false; | ||
} | ||
|
||
Console.Write(SetColorPerLine(value)); | ||
} | ||
|
||
public override void AppendLine(string value) | ||
{ | ||
// Previous write appended line, so we need to prepend color. | ||
if (_prependColor) | ||
{ | ||
Console.Write(_foregroundColor); | ||
} | ||
|
||
Console.WriteLine(SetColorPerLine(value)); | ||
// This call appended new line so the next write to console needs to prepend color. | ||
_prependColor = true; | ||
} | ||
|
||
public override void SetColor(TerminalColor color) | ||
{ | ||
string setColor = $"{AnsiCodes.CSI}{(int)color}{AnsiCodes.SetColor}"; | ||
_foregroundColor = setColor; | ||
Console.Write(setColor); | ||
// This call set the color for current line, no need to prepend on next write. | ||
_prependColor = false; | ||
} | ||
|
||
public override void ResetColor() | ||
{ | ||
_foregroundColor = null; | ||
_prependColor = false; | ||
Console.Write(AnsiCodes.SetDefaultColor); | ||
} | ||
|
||
private string? SetColorPerLine(string value) | ||
=> _foregroundColor == null ? value : value.Replace("\n", $"\n{_foregroundColor}"); | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.