Skip to content

Commit f1024b9

Browse files
authored
Merge pull request #240 from damianpowell/dev
Colourise Redirected Output
2 parents 75e4ea1 + 0bc84f7 commit f1024b9

File tree

3 files changed

+35
-7
lines changed

3 files changed

+35
-7
lines changed

src/SeqCli/Cli/Commands/PrintCommand.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,13 @@ class PrintCommand : Command
3535
readonly InvalidDataHandlingFeature _invalidDataHandlingFeature;
3636

3737
string _filter, _template = OutputFormatFeature.DefaultOutputTemplate;
38-
bool _noColor;
38+
bool _noColor, _forceColor;
3939

4040
public PrintCommand(SeqCliOutputConfig outputConfig)
4141
{
4242
if (outputConfig == null) throw new ArgumentNullException(nameof(outputConfig));
4343
_noColor = outputConfig.DisableColor;
44+
_forceColor = outputConfig.ForceColor;
4445

4546
_fileInputFeature = Enable(new FileInputFeature("CLEF file to read", supportsWildcard: true));
4647

@@ -55,17 +56,30 @@ public PrintCommand(SeqCliOutputConfig outputConfig)
5556
_invalidDataHandlingFeature = Enable<InvalidDataHandlingFeature>();
5657

5758
Options.Add("no-color", "Don't colorize text output", v => _noColor = true);
59+
60+
Options.Add("force-color",
61+
"Force redirected output to have ANSI color (unless `--no-color` is also specified)",
62+
v => _forceColor = true);
5863
}
5964

6065
protected override async Task<int> Run()
6166
{
67+
var applyThemeToRedirectedOutput
68+
= _noColor == false && _forceColor == true;
69+
70+
var theme
71+
= _noColor ? ConsoleTheme.None
72+
: applyThemeToRedirectedOutput ? OutputFormatFeature.DefaultAnsiTheme
73+
: OutputFormatFeature.DefaultTheme;
74+
6275
var outputConfiguration = new LoggerConfiguration()
6376
.MinimumLevel.Is(LevelAlias.Minimum)
6477
.Enrich.With<RedundantEventTypeRemovalEnricher>()
6578
.Enrich.With<SurrogateLevelRemovalEnricher>()
6679
.WriteTo.Console(
6780
outputTemplate: _template,
68-
theme: _noColor ? ConsoleTheme.None : OutputFormatFeature.DefaultTheme);
81+
theme: theme,
82+
applyThemeToRedirectedOutput: applyThemeToRedirectedOutput);
6983

7084
if (_filter != null)
7185
outputConfiguration.Filter.ByIncludingOnly(_filter);

src/SeqCli/Cli/Features/OutputFormatFeature.cs

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,25 @@ class OutputFormatFeature : CommandFeature
3434
public const string DefaultOutputTemplate =
3535
"[{Timestamp:o} {Level:u3}] {Message:lj} {Properties:j}{NewLine}{Exception}";
3636

37-
public static readonly ConsoleTheme DefaultTheme = SystemConsoleTheme.Literate;
37+
public static readonly ConsoleTheme DefaultTheme = SystemConsoleTheme.Literate;
38+
public static readonly ConsoleTheme DefaultAnsiTheme = AnsiConsoleTheme.Code;
3839

39-
bool _json, _noColor;
40+
bool _json, _noColor, _forceColor;
4041

4142
public OutputFormatFeature(SeqCliOutputConfig outputConfig)
4243
{
4344
_noColor = outputConfig.DisableColor;
45+
_forceColor = outputConfig.ForceColor;
4446
}
4547

4648
public bool Json => _json;
4749

48-
ConsoleTheme Theme => _noColor ? ConsoleTheme.None : DefaultTheme;
50+
bool ApplyThemeToRedirectedOutput => _noColor == false && _forceColor == true;
51+
52+
ConsoleTheme Theme
53+
=> _noColor ? ConsoleTheme.None
54+
: ApplyThemeToRedirectedOutput ? DefaultAnsiTheme
55+
: DefaultTheme;
4956

5057
public override void Enable(OptionSet options)
5158
{
@@ -55,6 +62,10 @@ public override void Enable(OptionSet options)
5562
v => _json = true);
5663

5764
options.Add("no-color", "Don't colorize text output", v => _noColor = true);
65+
66+
options.Add("force-color",
67+
"Force redirected output to have ANSI color (unless `--no-color` is also specified)",
68+
v => _forceColor = true);
5869
}
5970

6071
public Logger CreateOutputLogger()
@@ -72,7 +83,8 @@ public Logger CreateOutputLogger()
7283
outputConfiguration.Enrich.With<SurrogateLevelRemovalEnricher>();
7384
outputConfiguration.WriteTo.Console(
7485
outputTemplate: DefaultOutputTemplate,
75-
theme: Theme);
86+
theme: Theme,
87+
applyThemeToRedirectedOutput: ApplyThemeToRedirectedOutput);
7688
}
7789

7890
return outputConfiguration.CreateLogger();
@@ -115,7 +127,8 @@ public void WriteEntity(Entity entity)
115127
.Enrich.With<StripStructureTypeEnricher>()
116128
.WriteTo.Console(
117129
outputTemplate: "{@Message:j}{NewLine}",
118-
theme: Theme)
130+
theme: Theme,
131+
applyThemeToRedirectedOutput: ApplyThemeToRedirectedOutput)
119132
.CreateLogger();
120133
writer.Information("{@Entity}", jo);
121134
}

src/SeqCli/Config/SeqCliOutputConfig.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,6 @@ namespace SeqCli.Config
1717
class SeqCliOutputConfig
1818
{
1919
public bool DisableColor { get; set; }
20+
public bool ForceColor { get; set; }
2021
}
2122
}

0 commit comments

Comments
 (0)