/// <summary> /// Creates an ANSI console. /// </summary> /// <param name="settings">The settings.</param> /// <returns>An implementation of <see cref="IAnsiConsole"/>.</returns> public IAnsiConsole Create(AnsiConsoleSettings settings) { if (settings is null) { throw new ArgumentNullException(nameof(settings)); } var output = settings.Out ?? new AnsiConsoleOutput(System.Console.Out); if (output.Writer == null) { throw new InvalidOperationException("Output writer was null"); } // Detect if the terminal support ANSI or not var(supportsAnsi, legacyConsole) = DetectAnsi(settings, output.Writer); // Use console encoding or fall back to provided encoding var encoding = output.Writer.IsStandardOut() || output.Writer.IsStandardError() ? System.Console.OutputEncoding : output.Writer.Encoding; // Get the color system var colorSystem = settings.ColorSystem == ColorSystemSupport.Detect ? ColorSystemDetector.Detect(supportsAnsi) : (ColorSystem)settings.ColorSystem; // Get whether or not we consider the terminal interactive var interactive = settings.Interactive == InteractionSupport.Yes; if (settings.Interactive == InteractionSupport.Detect) { interactive = Environment.UserInteractive; } var profile = new Profile(output, encoding); profile.Capabilities.ColorSystem = colorSystem; profile.Capabilities.Ansi = supportsAnsi; profile.Capabilities.Links = supportsAnsi && !legacyConsole; profile.Capabilities.Legacy = legacyConsole; profile.Capabilities.Interactive = interactive; profile.Capabilities.Unicode = encoding.EncodingName.ContainsExact("Unicode"); // Enrich the profile ProfileEnricher.Enrich( profile, settings.Enrichment, settings.EnvironmentVariables); return(new AnsiConsoleFacade( profile, settings.ExclusivityMode ?? new DefaultExclusivityMode())); }
/// <summary> /// Creates an ANSI console. /// </summary> /// <param name="settings">The settings.</param> /// <returns>An implementation of <see cref="IAnsiConsole"/>.</returns> public IAnsiConsole Create(AnsiConsoleSettings settings) { if (settings is null) { throw new ArgumentNullException(nameof(settings)); } var buffer = settings.Out ?? System.Console.Out; // Detect if the terminal support ANSI or not var(supportsAnsi, legacyConsole) = DetectAnsi(settings, buffer); // Use the provided encoding or fall back to UTF-8 var encoding = buffer.IsStandardOut() || buffer.IsStandardError() ? System.Console.OutputEncoding : Encoding.UTF8; // Get the color system var colorSystem = settings.ColorSystem == ColorSystemSupport.Detect ? ColorSystemDetector.Detect(supportsAnsi) : (ColorSystem)settings.ColorSystem; // Get whether or not we consider the terminal interactive var interactive = settings.Interactive == InteractionSupport.Yes; if (settings.Interactive == InteractionSupport.Detect) { interactive = Environment.UserInteractive; } var profile = new Profile(buffer, encoding) { ColorSystem = colorSystem, }; profile.Capabilities.Ansi = supportsAnsi; profile.Capabilities.Links = supportsAnsi && !legacyConsole; profile.Capabilities.Legacy = legacyConsole; profile.Capabilities.Interactive = interactive; // Enrich the profile ProfileEnricher.Enrich( profile, settings.Enrichment, settings.EnvironmentVariables); return(new AnsiConsoleFacade( profile, settings.ExclusivityMode ?? new DefaultExclusivityMode())); }