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

Handle platform-specific default color for background & foreground properly #23

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
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