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

Use spectre.console lib to manage ANSI escape code #497

Merged
merged 1 commit into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 11 additions & 27 deletions src/D2L.Bmx/ConsoleWriter.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using Spectre.Console;

namespace D2L.Bmx;

internal interface IConsoleWriter {
Expand All @@ -11,56 +13,38 @@ internal interface IConsoleWriter {
// if stdout is redirected (e.g. typical use case for `bmx print`), we won't get any coloured text on stderr.
// See https://github.com/dotnet/runtime/issues/83146.
// Furthermore, ANSI escape codes give us greater control over the spread of custom background colour.
// TODO: use a library to manage ANSI codes and NO_COLOR?
internal class ConsoleWriter : IConsoleWriter {
// .NET runtime subscribes to the informal standard from https://no-color.org/. We should too.
// https://github.com/dotnet/runtime/blob/v9.0.0-preview.6.24327.7/src/libraries/Common/src/System/Console/ConsoleUtils.cs#L32-L34
private readonly bool _noColor
= Environment.GetEnvironmentVariable( "NO_COLOR" ) == "1" || !VirtualTerminal.TryEnableOnStderr();

void IConsoleWriter.WriteParameter( string description, string value, ParameterSource source ) {
if( _noColor ) {
Console.Error.WriteLine( $"{description}: {value} (from {source})" );
return;
}
// description: default
// value: bright cyan
// source: grey / bright black
Console.Error.WriteLine( $"\x1b[0m{description}: \x1b[96m{value} \x1b[90m(from {source})\x1b[0m" );
string valueColor = _noColor ? "default" : "cyan2";
string sourceColor = _noColor ? "default" : "grey";
AnsiConsole.MarkupLine( $"[default]{description}:[/] [{valueColor}]{value}[/] [{sourceColor}](from {source})[/]" );
}

void IConsoleWriter.WriteUpdateMessage( string text ) {
if( _noColor ) {
Console.Error.WriteLine( text );
Console.Error.WriteLine();
return;
}
// Trim entries so we don't have extra `\r` characters on Windows.
// Splitting on `Environment.NewLine` isn't as safe, because we might also use `\n` on Windows.
string[] lines = text.Split( '\n', StringSplitOptions.TrimEntries );
int maxLineLength = lines.Max( l => l.Length );
string color = _noColor ? "default" : "black on white";
foreach( string line in lines ) {
string paddedLine = line.PadRight( maxLineLength );
Console.Error.WriteLine( $"\x1b[0m\x1b[30;47m{paddedLine}\x1b[0m" );
AnsiConsole.MarkupLine( $"[{color}]{paddedLine}[/]" );
}
Console.Error.WriteLine();
}

void IConsoleWriter.WriteWarning( string text ) {
if( _noColor ) {
Console.Error.WriteLine( text );
return;
}
// bright yellow - 93
Console.Error.WriteLine( $"\x1b[0m\x1b[93m{text}\x1b[0m" );
string color = _noColor ? "default" : "yellow";
AnsiConsole.MarkupLine( $"[{color}]{text}[/]" );
}

void IConsoleWriter.WriteError( string text ) {
if( _noColor ) {
Console.Error.WriteLine( text );
return;
}
// bright red - 91
Console.Error.WriteLine( $"\x1b[0m\x1b[91m{text}\x1b[0m" );
string color = _noColor ? "default" : "red";
AnsiConsole.MarkupLine( $"[{color}]{text}[/]" );
}
}
1 change: 1 addition & 0 deletions src/D2L.Bmx/D2L.Bmx.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<PackageReference Include="HtmlAgilityPack" Version="1.11.68" />
<PackageReference Include="ini-parser-netstandard" Version="2.5.2" />
<PackageReference Include="PuppeteerSharp" Version="20.0.3" />
<PackageReference Include="Spectre.Console" Version="0.49.1" />
<PackageReference Include="System.CommandLine" Version="2.0.0-beta4.22272.1" />
</ItemGroup>

Expand Down
Loading