using System; using System.Collections.Generic; using System.Text; namespace Spectre.Console { /// /// Represents a console profile. /// public sealed class Profile { private readonly HashSet _enrichers; private static readonly string[] _defaultEnricher = new[] { "Default" }; private IAnsiConsoleOutput _out; private Encoding _encoding; private Capabilities _capabilities; private int? _width; private int? _height; /// /// Gets the enrichers used to build this profile. /// public IReadOnlyCollection Enrichers { get { if (_enrichers.Count > 0) { return _enrichers; } return _defaultEnricher; } } /// /// Gets or sets the out buffer. /// public IAnsiConsoleOutput Out { get => _out; set { _out = value ?? throw new InvalidOperationException("Output buffer cannot be null"); // Reset the width and height if this is a terminal. if (value.IsTerminal) { _width = null; _height = null; } } } /// /// Gets or sets the console output encoding. /// public Encoding Encoding { get => _encoding; set { if (value == null) { throw new InvalidOperationException("Encoding cannot be null"); } _out.SetEncoding(value); _encoding = value; } } /// /// Gets or sets an explicit console width. /// public int Width { get => _width ?? _out.Width; set { if (value <= 0) { throw new InvalidOperationException("Console width must be greater than zero"); } _width = value; } } /// /// Gets or sets an explicit console height. /// public int Height { get => _height ?? _out.Height; set { if (value <= 0) { throw new InvalidOperationException("Console height must be greater than zero"); } _height = value; } } /// /// Gets or sets the capabilities of the profile. /// public Capabilities Capabilities { get => _capabilities; set { _capabilities = value ?? throw new InvalidOperationException("Profile capabilities cannot be null"); } } /// /// Initializes a new instance of the class. /// /// The output buffer. /// The output encoding. public Profile(IAnsiConsoleOutput @out, Encoding encoding) { _enrichers = new HashSet(StringComparer.OrdinalIgnoreCase); _out = @out ?? throw new ArgumentNullException(nameof(@out)); _encoding = encoding ?? throw new ArgumentNullException(nameof(encoding)); _capabilities = new Capabilities(_out); } /// /// Checks whether the current profile supports /// the specified color system. /// /// The color system to check. /// true if the color system is supported, otherwise false. public bool Supports(ColorSystem colorSystem) { return (int)colorSystem <= (int)Capabilities.ColorSystem; } internal void AddEnricher(string name) { if (name is null) { throw new ArgumentNullException(nameof(name)); } _enrichers.Add(name); } } }