mirror of
https://github.com/nsnail/spectre.console.git
synced 2025-06-19 13:28:16 +08:00
Add method to write VT/ANSI control codes
Adds a new extension method IAnsiConsole.WriteAnsi that writes VT/ANSI control codes to the console. If VT/ANSI control codes are not supported, nothing will be written.
This commit is contained in:

committed by
Phil Scott

parent
91f910326c
commit
3463dde543
@ -0,0 +1,28 @@
|
||||
using System;
|
||||
|
||||
namespace Spectre.Console.Advanced
|
||||
{
|
||||
/// <summary>
|
||||
/// Contains extension methods for <see cref="IAnsiConsole"/>.
|
||||
/// </summary>
|
||||
public static class AnsiConsoleExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Writes a VT/Ansi control code sequence to the console (if supported).
|
||||
/// </summary>
|
||||
/// <param name="console">The console to write to.</param>
|
||||
/// <param name="sequence">The VT/Ansi control code sequence to write.</param>
|
||||
public static void WriteAnsi(this IAnsiConsole console, string sequence)
|
||||
{
|
||||
if (console is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(console));
|
||||
}
|
||||
|
||||
if (console.Profile.Capabilities.Ansi)
|
||||
{
|
||||
console.Write(new ControlCode(sequence));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -38,6 +38,11 @@ namespace Spectre.Console
|
||||
/// <param name="text">The text to write.</param>
|
||||
public static void Write(this IAnsiConsole console, string text)
|
||||
{
|
||||
if (console is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(console));
|
||||
}
|
||||
|
||||
console.Write(new Text(text, Style.Plain));
|
||||
}
|
||||
|
||||
@ -49,6 +54,11 @@ namespace Spectre.Console
|
||||
/// <param name="style">The text style.</param>
|
||||
public static void Write(this IAnsiConsole console, string text, Style style)
|
||||
{
|
||||
if (console is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(console));
|
||||
}
|
||||
|
||||
console.Write(new Text(text, style));
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user