Skip to content

Commit

Permalink
Fixed handle platform-specific default color for background & foreground
Browse files Browse the repository at this point in the history
  • Loading branch information
tautcony committed Jan 30, 2025
1 parent 4ead00a commit 1fcf4d8
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 14 deletions.
10 changes: 5 additions & 5 deletions src/DotMake.CommandLine/CliTheme.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class CliTheme
/// </summary>
public CliTheme()
{

}

/// <summary>
Expand All @@ -33,13 +33,13 @@ public CliTheme(CliTheme baseTheme)

/// <summary>
/// Gets or sets the default color used by the app.
/// <para>Default is <see langword="null"/> which also means <see cref="ConsoleColor.Gray"/>.</para>
/// <para>Default is <see langword="null"/>, which is equivalent to <see cref="ConsoleColor.Gray"/> on Windows. On *nix-like platforms, the default is unset (<see cref="ConsoleColor"/>(-1)).</para>
/// </summary>
public ConsoleColor? DefaultColor { get; init; }

/// <summary>
/// Gets or sets the default background color used by the app.
/// <para>Default is <see langword="null"/> which also means <see cref="ConsoleColor.Black"/>.</para>
/// <para>Default is <see langword="null"/>, which is equivalent to <see cref="ConsoleColor.Black"/> on Windows. On *nix-like platforms, the default is unset (<see cref="ConsoleColor"/>(-1)).</para>
/// </summary>
public ConsoleColor? DefaultBgColor { get; init; }

Expand Down Expand Up @@ -101,7 +101,7 @@ public CliTheme(CliTheme baseTheme)
/// <para>First column is similar to:</para>
/// <code language="console">
/// &lt;argument-1&gt;
///
///
/// -o, --option-1 &lt;option-1&gt;
/// -v, --version
/// -?, -h, --help
Expand All @@ -117,7 +117,7 @@ public CliTheme(CliTheme baseTheme)
/// <para>Second column is similar to:</para>
/// <code language="console">
/// Description for Argument1 [required]
///
///
/// Description for Option1 [default: DefaultForOption1]
/// Show version information
/// Show help and usage information
Expand Down
17 changes: 8 additions & 9 deletions src/DotMake.CommandLine/ConsoleExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,9 @@ public static void SetColor(ConsoleColor? color, ConsoleColor? defaultColor = nu
{
if (ColorIsSupported && !Console.IsOutputRedirected)
{
//Color 07 will set it to the default scheme that cmd.exe uses.
//0 = Black
//7 = White (ConsoleColor.Gray is 7)
//https://superuser.com/a/158769
// https://learn.microsoft.com/en-us/dotnet/api/system.console.foregroundcolor?view=net-8.0#remarks
// On Windows, the default color is gray (ConsoleColor.Gray).
// On *nix-like platforms, the default color is unset ((ConsoleColor)-1).
if (color == null)
Console.ForegroundColor = defaultColor ?? ConsoleColor.Gray;
else
Expand All @@ -36,12 +35,12 @@ public static void SetBgColor(ConsoleColor? color, ConsoleColor? defaultColor =
{
if (ColorIsSupported && !Console.IsOutputRedirected)
{
//Color 07 will set it to the default scheme that cmd.exe uses.
//0 = Black
//7 = White (ConsoleColor.Gray is 7)
//https://superuser.com/a/158769
// https://learn.microsoft.com/en-us/dotnet/api/system.console.backgroundcolor?view=net-8.0#remarks
// On Windows, the default color is black (ConsoleColor.Black).
// On *nix-like platforms, the default color is unset ((ConsoleColor)-1).
if (color == null)
Console.BackgroundColor = defaultColor ?? ConsoleColor.Black;
// Console.BackgroundColor = defaultColor ?? ConsoleColor.Black;
Console.BackgroundColor = defaultColor ?? (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? ConsoleColor.Black : (ConsoleColor)(-1));
else
Console.BackgroundColor = color.Value;
}
Expand Down

0 comments on commit 1fcf4d8

Please sign in to comment.