From a279772cf08534d359436798fc00fd12996501db Mon Sep 17 00:00:00 2001 From: JKamsker Date: Mon, 11 Mar 2024 10:41:26 +0100 Subject: [PATCH 1/2] Add ConsoleSizeEnricher --- .../Enrichment/CI/ConsoleSizeEnricher.cs | 35 +++++++++++++++++++ .../Enrichment/ProfileEnricher.cs | 3 ++ 2 files changed, 38 insertions(+) create mode 100644 src/Spectre.Console/Enrichment/CI/ConsoleSizeEnricher.cs diff --git a/src/Spectre.Console/Enrichment/CI/ConsoleSizeEnricher.cs b/src/Spectre.Console/Enrichment/CI/ConsoleSizeEnricher.cs new file mode 100644 index 000000000..8eacf39e4 --- /dev/null +++ b/src/Spectre.Console/Enrichment/CI/ConsoleSizeEnricher.cs @@ -0,0 +1,35 @@ +namespace Spectre.Console.Enrichment.CI; + +internal class ConsoleSizeEnricher : IProfileEnricher +{ + private IDictionary? _environmentVariables; + + public string Name => "ConsoleSizeEnricher"; + + public bool Enabled(IDictionary environmentVariables) + { + _environmentVariables = environmentVariables; + return environmentVariables.ContainsKey("CONSOLE_WIDTH") + || environmentVariables.ContainsKey("CONSOLE_HEIGHT"); + } + + public void Enrich(Profile profile) + { + if (_environmentVariables is null) + { + return; + } + + if (_environmentVariables.TryGetValue("CONSOLE_WIDTH", out var width) + && int.TryParse(width, out var w)) + { + profile.Width = w; + } + + if (_environmentVariables.TryGetValue("CONSOLE_HEIGHT", out var height) + && int.TryParse(height, out var h)) + { + profile.Height = h; + } + } +} \ No newline at end of file diff --git a/src/Spectre.Console/Enrichment/ProfileEnricher.cs b/src/Spectre.Console/Enrichment/ProfileEnricher.cs index 8465a7408..6eee66414 100644 --- a/src/Spectre.Console/Enrichment/ProfileEnricher.cs +++ b/src/Spectre.Console/Enrichment/ProfileEnricher.cs @@ -1,3 +1,5 @@ +using Spectre.Console.Enrichment.CI; + namespace Spectre.Console.Enrichment; internal static class ProfileEnricher @@ -17,6 +19,7 @@ internal static class ProfileEnricher new TeamCityEnricher(), new TfsEnricher(), new TravisEnricher(), + new ConsoleSizeEnricher(), }; public static void Enrich( From 583db96c2b6ee0bf8fcc18e46d3059e0b23a387c Mon Sep 17 00:00:00 2001 From: JKamsker Date: Mon, 11 Mar 2024 10:44:32 +0100 Subject: [PATCH 2/2] Fallback to EnvironmentVariables --- .../Enrichment/CI/ConsoleSizeEnricher.cs | 29 ++++++++++++++++--- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/src/Spectre.Console/Enrichment/CI/ConsoleSizeEnricher.cs b/src/Spectre.Console/Enrichment/CI/ConsoleSizeEnricher.cs index 8eacf39e4..1727792d8 100644 --- a/src/Spectre.Console/Enrichment/CI/ConsoleSizeEnricher.cs +++ b/src/Spectre.Console/Enrichment/CI/ConsoleSizeEnricher.cs @@ -15,10 +15,7 @@ public bool Enabled(IDictionary environmentVariables) public void Enrich(Profile profile) { - if (_environmentVariables is null) - { - return; - } + _environmentVariables ??= GetEnvironmentVariables(null); if (_environmentVariables.TryGetValue("CONSOLE_WIDTH", out var width) && int.TryParse(width, out var w)) @@ -32,4 +29,28 @@ public void Enrich(Profile profile) profile.Height = h; } } + + private static IDictionary GetEnvironmentVariables(IDictionary? variables) + { + if (variables != null) + { + return new Dictionary(variables, StringComparer.OrdinalIgnoreCase); + } + + return Environment.GetEnvironmentVariables() + .Cast() + .Aggregate( + new Dictionary(StringComparer.OrdinalIgnoreCase), + (dictionary, entry) => + { + var key = (string)entry.Key; + if (!dictionary.TryGetValue(key, out _)) + { + dictionary.Add(key, entry.Value as string ?? string.Empty); + } + + return dictionary; + }, + dictionary => dictionary); + } } \ No newline at end of file