using System; using System.Collections.Generic; using System.IO; 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 TextWriter _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 TextWriter Out { get => _out; set { _out = value ?? throw new InvalidOperationException("Output buffer cannot be null"); // Reset the width and height if not a TTY. if (!Capabilities.Tty) { _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"); } // Need to update the output encoding for stdout? if (_out.IsStandardOut() || _out.IsStandardError()) { System.Console.OutputEncoding = value; } _encoding = value; } } /// /// Gets or sets an explicit console width. /// public int Width { get => GetWidth(); set { if (_width <= 0) { throw new InvalidOperationException("Console width must be greater than zero"); } _width = value; } } /// /// Gets or sets an explicit console height. /// public int Height { get => GetHeight(); set { if (_height <= 0) { throw new InvalidOperationException("Console height must be greater than zero"); } _height = value; } } /// /// Gets or sets the color system. /// public ColorSystem ColorSystem { get; set; } /// /// 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(TextWriter @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(this); } /// /// 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)ColorSystem; } internal void AddEnricher(string name) { if (name is null) { throw new ArgumentNullException(nameof(name)); } _enrichers.Add(name); } private int GetWidth() { if (_width != null) { return _width.Value; } if (!Capabilities.Tty) { return ConsoleHelper.GetSafeWidth(Constants.DefaultTerminalWidth); } return Constants.DefaultTerminalWidth; } private int GetHeight() { if (_height != null) { return _height.Value; } if (!Capabilities.Tty) { return ConsoleHelper.GetSafeHeight(Constants.DefaultTerminalHeight); } return Constants.DefaultTerminalHeight; } } }