Skip to content

Commit

Permalink
Merge pull request #124 from ggobbe/feat/sixteen-colors-themes
Browse files Browse the repository at this point in the history
New 16-color ANSI theme `Sixteen`
  • Loading branch information
nblumhardt authored Dec 22, 2021
2 parents 4a76e28 + b6a4ae6 commit 59fe4b1
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 1 deletion.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,10 @@ The following built-in themes are available:
* `ConsoleTheme.None` - no styling
* `SystemConsoleTheme.Literate` - styled to replicate _Serilog.Sinks.Literate_, using the `System.Console` coloring modes supported on all Windows/.NET targets; **this is the default when no theme is specified**
* `SystemConsoleTheme.Grayscale` - a theme using only shades of gray, white, and black
* `AnsiConsoleTheme.Literate` - an ANSI 16-color version of the "literate" theme; we expect to update this to use 256-colors for a more refined look in future
* `AnsiConsoleTheme.Literate` - an ANSI 256-color version of the "literate" theme
* `AnsiConsoleTheme.Grayscale` - an ANSI 256-color version of the "grayscale" theme
* `AnsiConsoleTheme.Code` - an ANSI 256-color Visual Studio Code-inspired theme
* `AnsiConsoleTheme.Sixteen` - an ANSI 16-color theme that works well with both light and dark backgrounds

Adding a new theme is straightforward; examples can be found in the [`SystemConsoleThemes`](https://github.com/serilog/serilog-sinks-console/blob/dev/src/Serilog.Sinks.Console/Sinks/SystemConsole/Themes/SystemConsoleThemes.cs) and [`AnsiConsoleThemes`](https://github.com/serilog/serilog-sinks-console/blob/dev/src/Serilog.Sinks.Console/Sinks/SystemConsole/Themes/AnsiConsoleThemes.cs) classes.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ public class AnsiConsoleTheme : ConsoleTheme
/// </summary>
public static AnsiConsoleTheme Literate { get; } = AnsiConsoleThemes.Literate;

/// <summary>
/// A theme in the style of the original <i>Serilog.Sinks.Literate</i> using only standard 16 terminal colors that will work on light backgrounds.
/// </summary>
public static AnsiConsoleTheme Sixteen { get; } = AnsiConsoleThemes.Sixteen;

readonly IReadOnlyDictionary<ConsoleThemeStyle, string> _styles;
const string AnsiStyleReset = "\x1b[0m";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,5 +80,26 @@ static class AnsiConsoleThemes
[ConsoleThemeStyle.LevelError] = "\x1b[38;5;0197m\x1b[48;5;0238m",
[ConsoleThemeStyle.LevelFatal] = "\x1b[38;5;0197m\x1b[48;5;0238m",
});

public static AnsiConsoleTheme Sixteen { get; } = new AnsiConsoleTheme(
new Dictionary<ConsoleThemeStyle, string>
{
[ConsoleThemeStyle.Text] = AnsiEscapeSequence.Unthemed,
[ConsoleThemeStyle.SecondaryText] = AnsiEscapeSequence.Unthemed,
[ConsoleThemeStyle.TertiaryText] = AnsiEscapeSequence.Unthemed,
[ConsoleThemeStyle.Invalid] = AnsiEscapeSequence.Yellow,
[ConsoleThemeStyle.Null] = AnsiEscapeSequence.Blue,
[ConsoleThemeStyle.Name] = AnsiEscapeSequence.Unthemed,
[ConsoleThemeStyle.String] = AnsiEscapeSequence.Cyan,
[ConsoleThemeStyle.Number] = AnsiEscapeSequence.Magenta,
[ConsoleThemeStyle.Boolean] = AnsiEscapeSequence.Blue,
[ConsoleThemeStyle.Scalar] = AnsiEscapeSequence.Green,
[ConsoleThemeStyle.LevelVerbose] = AnsiEscapeSequence.Unthemed,
[ConsoleThemeStyle.LevelDebug] = AnsiEscapeSequence.Bold,
[ConsoleThemeStyle.LevelInformation] = AnsiEscapeSequence.BrightCyan,
[ConsoleThemeStyle.LevelWarning] = AnsiEscapeSequence.BrightYellow,
[ConsoleThemeStyle.LevelError] = AnsiEscapeSequence.BrightRed,
[ConsoleThemeStyle.LevelFatal] = AnsiEscapeSequence.BrightRed,
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright 2017 Serilog Contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace Serilog.Sinks.SystemConsole.Themes
{
static class AnsiEscapeSequence
{
public const string Unthemed = "";
public const string Reset = "\x1b[0m";
public const string Bold = "\x1b[1m";

public const string Black = "\x1b[30m";
public const string Red = "\x1b[31m";
public const string Green = "\x1b[32m";
public const string Yellow = "\x1b[33m";
public const string Blue = "\x1b[34m";
public const string Magenta = "\x1b[35m";
public const string Cyan = "\x1b[36m";
public const string White = "\x1b[37m";

public const string BrightBlack = "\x1b[30;1m";
public const string BrightRed = "\x1b[31;1m";
public const string BrightGreen = "\x1b[32;1m";
public const string BrightYellow = "\x1b[33;1m";
public const string BrightBlue = "\x1b[34;1m";
public const string BrightMagenta = "\x1b[35;1m";
public const string BrightCyan = "\x1b[36;1m";
public const string BrightWhite = "\x1b[37;1m";
}
}

0 comments on commit 59fe4b1

Please sign in to comment.