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

Add ConsoleSizeEnricher #1492

Open
wants to merge 2 commits 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
56 changes: 56 additions & 0 deletions src/Spectre.Console/Enrichment/CI/ConsoleSizeEnricher.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
namespace Spectre.Console.Enrichment.CI;

internal class ConsoleSizeEnricher : IProfileEnricher
{
private IDictionary<string, string>? _environmentVariables;

public string Name => "ConsoleSizeEnricher";

public bool Enabled(IDictionary<string, string> environmentVariables)
{
_environmentVariables = environmentVariables;
return environmentVariables.ContainsKey("CONSOLE_WIDTH")
|| environmentVariables.ContainsKey("CONSOLE_HEIGHT");
}

public void Enrich(Profile profile)
{
_environmentVariables ??= GetEnvironmentVariables(null);

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;
}
}

private static IDictionary<string, string> GetEnvironmentVariables(IDictionary<string, string>? variables)
{
if (variables != null)
{
return new Dictionary<string, string>(variables, StringComparer.OrdinalIgnoreCase);
}

return Environment.GetEnvironmentVariables()
.Cast<System.Collections.DictionaryEntry>()
.Aggregate(
new Dictionary<string, string>(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);
}
}
3 changes: 3 additions & 0 deletions src/Spectre.Console/Enrichment/ProfileEnricher.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using Spectre.Console.Enrichment.CI;

namespace Spectre.Console.Enrichment;

internal static class ProfileEnricher
Expand All @@ -17,6 +19,7 @@ internal static class ProfileEnricher
new TeamCityEnricher(),
new TfsEnricher(),
new TravisEnricher(),
new ConsoleSizeEnricher(),
};

public static void Enrich(
Expand Down