Skip to content

Commit

Permalink
use ANSI escape codes to control all console text colours
Browse files Browse the repository at this point in the history
  • Loading branch information
cfbao committed Jul 23, 2024
1 parent 2f7848c commit 511919e
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 35 deletions.
30 changes: 30 additions & 0 deletions src/D2L.Bmx/ConsoleWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ namespace D2L.Bmx;

internal interface IConsoleWriter {
void WriteParameter( string description, string value, ParameterSource source );
void WriteUpdateMessage( string text );
void WriteWarning( string text );
void WriteError( string text );
}

// We use ANSI escape codes to control colours, because .NET's `Console.ForegroundColor` only targets stdout,
Expand All @@ -21,4 +24,31 @@ void IConsoleWriter.WriteParameter( string description, string value, ParameterS
// source: grey / bright black
Console.Error.WriteLine( $"\x1b[0m{description}: \x1b[96m{value} \x1b[90m(from {source})\x1b[0m" );
}

void IConsoleWriter.WriteUpdateMessage( string text ) {
if( _noColor || !VirtualTerminal.TryEnableOnStderr() ) {
Console.Error.WriteLine( text );
}
string[] lines = text.Split( '\n' );
int maxLineLength = lines.Max( l => l.Length );
foreach( string line in lines ) {
Console.Error.WriteLine( $"\x1b[0m\x1b[30;47m{line.PadRight( maxLineLength )}\x1b[0m" );
}
}

void IConsoleWriter.WriteWarning( string text ) {
if( _noColor || !VirtualTerminal.TryEnableOnStderr() ) {
Console.Error.WriteLine( text );
}
// bright yellow - 93
Console.Error.WriteLine( $"\x1b[0m\x1b[93m{text}\x1b[0m" );
}

void IConsoleWriter.WriteError( string text ) {
if( _noColor || !VirtualTerminal.TryEnableOnStderr() ) {
Console.Error.WriteLine( text );
}
// bright red - 91
Console.Error.WriteLine( $"\x1b[0m\x1b[91m{text}\x1b[0m" );
}
}
9 changes: 4 additions & 5 deletions src/D2L.Bmx/OktaAuthenticator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,10 @@ mfaFactor is OktaMfaQuestionFactor // Security question factor is a static value
if( File.Exists( BmxPaths.CONFIG_FILE_NAME ) ) {
CacheOktaSession( user, org, sessionResp.Id, sessionResp.ExpiresAt );
} else {
Console.ResetColor();
Console.ForegroundColor = ConsoleColor.Yellow;
Console.Error.WriteLine( "No config file found. Your Okta session will not be cached. " +
"Consider running `bmx configure` if you own this machine." );
Console.ResetColor();
consoleWriter.WriteWarning(
"No config file found. Your Okta session will not be cached. " +
"Consider running `bmx configure` if you own this machine."
);
}
return new AuthenticatedOktaApi( Org: org, User: user, Api: oktaApi );
}
Expand Down
12 changes: 5 additions & 7 deletions src/D2L.Bmx/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@
}

if( context.ParseResult.CommandResult.Command != updateCommand ) {
var updateChecker = new UpdateChecker( new GitHubClient(), new VersionProvider() );
var updateChecker = new UpdateChecker( new GitHubClient(), new VersionProvider(), new ConsoleWriter() );
await updateChecker.CheckForUpdatesAsync();
}

Expand All @@ -258,17 +258,15 @@
order: MiddlewareOrder.ExceptionHandler + 1
)
.UseExceptionHandler( ( exception, context ) => {
Console.ResetColor();
Console.ForegroundColor = ConsoleColor.Red;
IConsoleWriter consoleWriter = new ConsoleWriter();
if( exception is BmxException ) {
Console.Error.WriteLine( exception.Message );
consoleWriter.WriteError( exception.Message );
} else {
Console.Error.WriteLine( "BMX exited with unexpected internal error" );
consoleWriter.WriteError( "BMX exited with unexpected internal error" );
}
if( Environment.GetEnvironmentVariable( "BMX_DEBUG" ) == "1" ) {
Console.Error.WriteLine( exception );
consoleWriter.WriteError( exception.ToString() );
}
Console.ResetColor();
} )
.Build()
.InvokeAsync( args );
25 changes: 2 additions & 23 deletions src/D2L.Bmx/UpdateChecker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace D2L.Bmx;

internal class UpdateChecker( IGitHubClient github, IVersionProvider versionProvider ) {
internal class UpdateChecker( IGitHubClient github, IVersionProvider versionProvider, IConsoleWriter consoleWriter ) {
public async Task CheckForUpdatesAsync() {
try {
var updateCheckCache = GetUpdateCheckCacheOrNull();
Expand All @@ -29,7 +29,7 @@ public async Task CheckForUpdatesAsync() {
Version? localVersion = versionProvider.GetAssemblyVersion();
if( latestVersion > localVersion ) {
string? displayVersion = versionProvider.GetInformationalVersion() ?? localVersion.ToString();
DisplayUpdateMessage(
consoleWriter.WriteUpdateMessage(
$"""
A new BMX release is available: v{latestVersion} (current: {displayVersion})
Run "bmx update" now to upgrade.
Expand All @@ -41,27 +41,6 @@ public async Task CheckForUpdatesAsync() {
}
}

private static void DisplayUpdateMessage( string message ) {
var originalBackgroundColor = Console.BackgroundColor;
var originalForegroundColor = Console.ForegroundColor;

Console.BackgroundColor = ConsoleColor.Gray;
Console.ForegroundColor = ConsoleColor.Black;

string[] lines = message.Split( "\n" );
int consoleWidth = Console.WindowWidth;

foreach( string line in lines ) {
Console.Error.Write( line.PadRight( consoleWidth ) );
Console.Error.WriteLine();
}

Console.BackgroundColor = originalBackgroundColor;
Console.ForegroundColor = originalForegroundColor;
Console.ResetColor();
Console.Error.WriteLine();
}

private static void SetUpdateCheckCache( Version version ) {
var cache = new UpdateCheckCache(
VersionName: version,
Expand Down

0 comments on commit 511919e

Please sign in to comment.