Skip to content

Commit 55bf3d5

Browse files
Add two new 16 color ANSI compatible themes
1 parent 7ecfd08 commit 55bf3d5

File tree

3 files changed

+77
-2
lines changed

3 files changed

+77
-2
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,11 @@ The following built-in themes are available:
4343
* `ConsoleTheme.None` - no styling
4444
* `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**
4545
* `SystemConsoleTheme.Grayscale` - a theme using only shades of gray, white, and black
46-
* `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
46+
* `AnsiConsoleTheme.Literate` - an ANSI 256-color version of the "literate" theme
47+
* `AnsiConsoleTheme.Literate16Color` - an ANSI 16-color version of the "literate" theme that works with light backgrounds
4748
* `AnsiConsoleTheme.Grayscale` - an ANSI 256-color version of the "grayscale" theme
4849
* `AnsiConsoleTheme.Code` - an ANSI 256-color Visual Studio Code-inspired theme
50+
* `AnsiConsoleTheme.Code16Color` - an ANSI 16-color Visual Studio Code-inspired theme that works with light backgrounds
4951

5052
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.
5153

src/Serilog.Sinks.Console/Sinks/SystemConsole/Themes/AnsiConsoleTheme.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ public class AnsiConsoleTheme : ConsoleTheme
3030
/// </summary>
3131
public static AnsiConsoleTheme Code { get; } = AnsiConsoleThemes.Code;
3232

33+
/// <summary>
34+
/// A 16-color theme along the lines of Visual Studio Code that should work on light backgrounds.
35+
/// </summary>
36+
public static AnsiConsoleTheme Code16Color { get; } = AnsiConsoleThemes.Code16Color;
37+
3338
/// <summary>
3439
/// A theme using only gray, black and white.
3540
/// </summary>
@@ -40,6 +45,11 @@ public class AnsiConsoleTheme : ConsoleTheme
4045
/// </summary>
4146
public static AnsiConsoleTheme Literate { get; } = AnsiConsoleThemes.Literate;
4247

48+
/// <summary>
49+
/// 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.
50+
/// </summary>
51+
public static AnsiConsoleTheme Literate16Color { get; } = AnsiConsoleThemes.Literate16Color;
52+
4353
readonly IReadOnlyDictionary<ConsoleThemeStyle, string> _styles;
4454
const string AnsiStyleReset = "\x1b[0m";
4555

@@ -77,4 +87,4 @@ public override void Reset(TextWriter output)
7787
output.Write(AnsiStyleReset);
7888
}
7989
}
80-
}
90+
}

src/Serilog.Sinks.Console/Sinks/SystemConsole/Themes/AnsiConsoleThemes.cs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,27 @@ namespace Serilog.Sinks.SystemConsole.Themes
1818
{
1919
static class AnsiConsoleThemes
2020
{
21+
const string Reset = "\x1b[0m";
22+
const string Bold = "\x1b[1m";
23+
24+
const string Black = "\x1b[30m";
25+
const string Red = "\x1b[31m";
26+
const string Green = "\x1b[32m";
27+
const string Yellow = "\x1b[33m";
28+
const string Blue = "\x1b[34m";
29+
const string Magenta = "\x1b[35m";
30+
const string Cyan = "\x1b[36m";
31+
const string White = "\x1b[37m";
32+
33+
const string BrightBlack = "\x1b[30;1m";
34+
const string BrightRed = "\x1b[31;1m";
35+
const string BrightGreen = "\x1b[32;1m";
36+
const string BrightYellow = "\x1b[33;1m";
37+
const string BrightBlue = "\x1b[34;1m";
38+
const string BrightMagenta = "\x1b[35;1m";
39+
const string BrightCyan = "\x1b[36;1m";
40+
const string BrightWhite = "\x1b[37;1m";
41+
2142
public static AnsiConsoleTheme Literate { get; } = new AnsiConsoleTheme(
2243
new Dictionary<ConsoleThemeStyle, string>
2344
{
@@ -39,6 +60,27 @@ static class AnsiConsoleThemes
3960
[ConsoleThemeStyle.LevelFatal] = "\x1b[38;5;0015m\x1b[48;5;0196m",
4061
});
4162

63+
public static AnsiConsoleTheme Literate16Color { get; } = new AnsiConsoleTheme(
64+
new Dictionary<ConsoleThemeStyle, string>
65+
{
66+
[ConsoleThemeStyle.Text] = Reset,
67+
[ConsoleThemeStyle.SecondaryText] = Reset,
68+
[ConsoleThemeStyle.TertiaryText] = Reset,
69+
[ConsoleThemeStyle.Invalid] = Yellow,
70+
[ConsoleThemeStyle.Null] = Blue,
71+
[ConsoleThemeStyle.Name] = Reset,
72+
[ConsoleThemeStyle.String] = Cyan,
73+
[ConsoleThemeStyle.Number] = Magenta,
74+
[ConsoleThemeStyle.Boolean] = Blue,
75+
[ConsoleThemeStyle.Scalar] = Green,
76+
[ConsoleThemeStyle.LevelVerbose] = Reset,
77+
[ConsoleThemeStyle.LevelDebug] = Bold,
78+
[ConsoleThemeStyle.LevelInformation] = BrightCyan,
79+
[ConsoleThemeStyle.LevelWarning] = BrightYellow,
80+
[ConsoleThemeStyle.LevelError] = "\x1b[38;5;0015m\x1b[48;5;0196m",
81+
[ConsoleThemeStyle.LevelFatal] = "\x1b[38;5;0015m\x1b[48;5;0196m",
82+
});
83+
4284
public static AnsiConsoleTheme Grayscale { get; } = new AnsiConsoleTheme(
4385
new Dictionary<ConsoleThemeStyle, string>
4486
{
@@ -80,5 +122,26 @@ static class AnsiConsoleThemes
80122
[ConsoleThemeStyle.LevelError] = "\x1b[38;5;0197m\x1b[48;5;0238m",
81123
[ConsoleThemeStyle.LevelFatal] = "\x1b[38;5;0197m\x1b[48;5;0238m",
82124
});
125+
126+
public static AnsiConsoleTheme Code16Color { get; } = new AnsiConsoleTheme(
127+
new Dictionary<ConsoleThemeStyle, string>
128+
{
129+
[ConsoleThemeStyle.Text] = Reset,
130+
[ConsoleThemeStyle.SecondaryText] = Reset,
131+
[ConsoleThemeStyle.TertiaryText] = Reset,
132+
[ConsoleThemeStyle.Invalid] = BrightYellow,
133+
[ConsoleThemeStyle.Null] = Cyan,
134+
[ConsoleThemeStyle.Name] = Cyan,
135+
[ConsoleThemeStyle.String] = Yellow,
136+
[ConsoleThemeStyle.Number] = BrightYellow,
137+
[ConsoleThemeStyle.Boolean] = Cyan,
138+
[ConsoleThemeStyle.Scalar] = Green,
139+
[ConsoleThemeStyle.LevelVerbose] = Reset,
140+
[ConsoleThemeStyle.LevelDebug] = Reset,
141+
[ConsoleThemeStyle.LevelInformation] = Bold,
142+
[ConsoleThemeStyle.LevelWarning] = BrightYellow,
143+
[ConsoleThemeStyle.LevelError] = "\x1b[38;5;0197m\x1b[48;5;0238m",
144+
[ConsoleThemeStyle.LevelFatal] = "\x1b[38;5;0197m\x1b[48;5;0238m",
145+
});
83146
}
84147
}

0 commit comments

Comments
 (0)