Add AnsiConsole.Write method

This commit also obsoletes the `AnsiConsole.Render` method.

Closes #576
This commit is contained in:
Patrik Svensson 2021-10-05 00:45:46 +02:00 committed by Phil Scott
parent ca2e6ce0ad
commit e0395dfa2b
19 changed files with 49 additions and 39 deletions

View File

@ -23,7 +23,7 @@ namespace Demo.Utilities
value ?? "[grey]null[/]"); value ?? "[grey]null[/]");
} }
AnsiConsole.Render(table); AnsiConsole.Write(table);
} }
} }
} }

View File

@ -35,7 +35,7 @@ namespace Spectre.Console.Examples
CreatePanel("None", BoxBorder.None), CreatePanel("None", BoxBorder.None),
}; };
AnsiConsole.Render( AnsiConsole.Write(
new Padder( new Padder(
new Columns(items).PadRight(2), new Columns(items).PadRight(2),
new Padding(2,0,0,0))); new Padding(2,0,0,0)));
@ -77,13 +77,13 @@ namespace Spectre.Console.Examples
CreateTable("Markdown", TableBorder.Markdown), CreateTable("Markdown", TableBorder.Markdown),
}; };
AnsiConsole.Render(new Columns(items).Collapse()); AnsiConsole.Write(new Columns(items).Collapse());
} }
private static void HorizontalRule(string title) private static void HorizontalRule(string title)
{ {
AnsiConsole.WriteLine(); AnsiConsole.WriteLine();
AnsiConsole.Render(new Rule($"[white bold]{title}[/]").RuleStyle("grey").LeftAligned()); AnsiConsole.Write(new Rule($"[white bold]{title}[/]").RuleStyle("grey").LeftAligned());
AnsiConsole.WriteLine(); AnsiConsole.WriteLine();
} }
} }

View File

@ -5,7 +5,7 @@ namespace Spectre.Console.Examples
public static void Main(string[] args) public static void Main(string[] args)
{ {
AnsiConsole.WriteLine(); AnsiConsole.WriteLine();
AnsiConsole.Render(new Calendar(2020, 10) AnsiConsole.Write(new Calendar(2020, 10)
.RoundedBorder() .RoundedBorder()
.HighlightStyle(Style.Parse("red")) .HighlightStyle(Style.Parse("red"))
.HeaderStyle(Style.Parse("yellow")) .HeaderStyle(Style.Parse("yellow"))

View File

@ -39,9 +39,9 @@ namespace Spectre.Console.Examples
private static void Render(IRenderable canvas, string title) private static void Render(IRenderable canvas, string title)
{ {
AnsiConsole.WriteLine(); AnsiConsole.WriteLine();
AnsiConsole.Render(new Rule($"[yellow]{title}[/]").LeftAligned().RuleStyle("grey")); AnsiConsole.Write(new Rule($"[yellow]{title}[/]").LeftAligned().RuleStyle("grey"));
AnsiConsole.WriteLine(); AnsiConsole.WriteLine();
AnsiConsole.Render(canvas); AnsiConsole.Write(canvas);
} }
} }
} }

View File

@ -32,7 +32,7 @@ namespace Spectre.Console.Examples
private static void Render(string title, IRenderable chart) private static void Render(string title, IRenderable chart)
{ {
AnsiConsole.Render( AnsiConsole.Write(
new Panel(chart) new Panel(chart)
.Padding(1, 1) .Padding(1, 1)
.Header(title)); .Header(title));

View File

@ -20,7 +20,7 @@ namespace Spectre.Console.Examples
{ {
AnsiConsole.ResetColors(); AnsiConsole.ResetColors();
AnsiConsole.WriteLine(); AnsiConsole.WriteLine();
AnsiConsole.Render(new Rule("[yellow bold underline]3-bit Colors[/]").RuleStyle("grey").LeftAligned()); AnsiConsole.Write(new Rule("[yellow bold underline]3-bit Colors[/]").RuleStyle("grey").LeftAligned());
AnsiConsole.WriteLine(); AnsiConsole.WriteLine();
for (var i = 0; i < 8; i++) for (var i = 0; i < 8; i++)
@ -43,7 +43,7 @@ namespace Spectre.Console.Examples
{ {
AnsiConsole.ResetColors(); AnsiConsole.ResetColors();
AnsiConsole.WriteLine(); AnsiConsole.WriteLine();
AnsiConsole.Render(new Rule("[yellow bold underline]4-bit Colors[/]").RuleStyle("grey").LeftAligned()); AnsiConsole.Write(new Rule("[yellow bold underline]4-bit Colors[/]").RuleStyle("grey").LeftAligned());
AnsiConsole.WriteLine(); AnsiConsole.WriteLine();
for (var i = 0; i < 16; i++) for (var i = 0; i < 16; i++)
@ -66,7 +66,7 @@ namespace Spectre.Console.Examples
{ {
AnsiConsole.ResetColors(); AnsiConsole.ResetColors();
AnsiConsole.WriteLine(); AnsiConsole.WriteLine();
AnsiConsole.Render(new Rule("[yellow bold underline]8-bit Colors[/]").RuleStyle("grey").LeftAligned()); AnsiConsole.Write(new Rule("[yellow bold underline]8-bit Colors[/]").RuleStyle("grey").LeftAligned());
AnsiConsole.WriteLine(); AnsiConsole.WriteLine();
for (var i = 0; i < 16; i++) for (var i = 0; i < 16; i++)
@ -93,10 +93,10 @@ namespace Spectre.Console.Examples
{ {
AnsiConsole.ResetColors(); AnsiConsole.ResetColors();
AnsiConsole.WriteLine(); AnsiConsole.WriteLine();
AnsiConsole.Render(new Rule("[yellow bold underline]24-bit Colors[/]").RuleStyle("grey").LeftAligned()); AnsiConsole.Write(new Rule("[yellow bold underline]24-bit Colors[/]").RuleStyle("grey").LeftAligned());
AnsiConsole.WriteLine(); AnsiConsole.WriteLine();
AnsiConsole.Render(new ColorBox(width: 80, height: 15)); AnsiConsole.Write(new ColorBox(width: 80, height: 15));
} }
} }
} }

View File

@ -16,7 +16,7 @@ namespace Spectre.Console.Examples
} }
// Render all cards in columns // Render all cards in columns
AnsiConsole.Render(new Columns(cards)); AnsiConsole.Write(new Columns(cards));
} }
private static string GetCardContent(User user) private static string GetCardContent(User user)

View File

@ -14,7 +14,7 @@ namespace Spectre.Console.Examples
private static void RenderEmoji() private static void RenderEmoji()
{ {
AnsiConsole.Render( AnsiConsole.Write(
new Panel("[yellow]Hello :globe_showing_europe_africa:![/]") new Panel("[yellow]Hello :globe_showing_europe_africa:![/]")
.RoundedBorder()); .RoundedBorder());
} }

View File

@ -14,17 +14,17 @@ namespace Spectre.Console.Examples
catch (Exception ex) catch (Exception ex)
{ {
AnsiConsole.WriteLine(); AnsiConsole.WriteLine();
AnsiConsole.Render(new Rule("Default").LeftAligned()); AnsiConsole.Write(new Rule("Default").LeftAligned());
AnsiConsole.WriteLine(); AnsiConsole.WriteLine();
AnsiConsole.WriteException(ex); AnsiConsole.WriteException(ex);
AnsiConsole.WriteLine(); AnsiConsole.WriteLine();
AnsiConsole.Render(new Rule("Compact").LeftAligned()); AnsiConsole.Write(new Rule("Compact").LeftAligned());
AnsiConsole.WriteLine(); AnsiConsole.WriteLine();
AnsiConsole.WriteException(ex, ExceptionFormats.ShortenEverything | ExceptionFormats.ShowLinks); AnsiConsole.WriteException(ex, ExceptionFormats.ShortenEverything | ExceptionFormats.ShowLinks);
AnsiConsole.WriteLine(); AnsiConsole.WriteLine();
AnsiConsole.Render(new Rule("Compact + Custom colors").LeftAligned()); AnsiConsole.Write(new Rule("Compact + Custom colors").LeftAligned());
AnsiConsole.WriteLine(); AnsiConsole.WriteLine();
AnsiConsole.WriteException(ex, new ExceptionSettings AnsiConsole.WriteException(ex, new ExceptionSettings
{ {

View File

@ -4,9 +4,9 @@ namespace Spectre.Console.Examples
{ {
public static void Main(string[] args) public static void Main(string[] args)
{ {
AnsiConsole.Render(new FigletText("Left aligned").LeftAligned().Color(Color.Red)); AnsiConsole.Write(new FigletText("Left aligned").LeftAligned().Color(Color.Red));
AnsiConsole.Render(new FigletText("Centered").Centered().Color(Color.Green)); AnsiConsole.Write(new FigletText("Centered").Centered().Color(Color.Green));
AnsiConsole.Render(new FigletText("Right aligned").RightAligned().Color(Color.Blue)); AnsiConsole.Write(new FigletText("Right aligned").RightAligned().Color(Color.Blue));
} }
} }
} }

View File

@ -16,7 +16,7 @@ namespace Spectre.Console.Examples
grid.AddRow(" [blue]-c[/], [blue]--configuration[/] <CONFIGURATION>", "The configuration to run for."); grid.AddRow(" [blue]-c[/], [blue]--configuration[/] <CONFIGURATION>", "The configuration to run for.");
grid.AddRow(" [blue]-v[/], [blue]--verbosity[/] <LEVEL>", "Set the [grey]MSBuild[/] verbosity level."); grid.AddRow(" [blue]-v[/], [blue]--verbosity[/] <LEVEL>", "Set the [grey]MSBuild[/] verbosity level.");
AnsiConsole.Render(grid); AnsiConsole.Write(grid);
} }
} }
} }

View File

@ -19,7 +19,7 @@ namespace Spectre.Console.Examples
.AddRow("[b]Buffer height[/]", $"{AnsiConsole.Console.Profile.Height}") .AddRow("[b]Buffer height[/]", $"{AnsiConsole.Console.Profile.Height}")
.AddRow("[b]Encoding[/]", $"{AnsiConsole.Console.Profile.Encoding.EncodingName}"); .AddRow("[b]Encoding[/]", $"{AnsiConsole.Console.Profile.Encoding.EncodingName}");
AnsiConsole.Render( AnsiConsole.Write(
new Panel(grid) new Panel(grid)
.Header("Information")); .Header("Information"));
} }

View File

@ -8,20 +8,20 @@ namespace Spectre.Console.Examples
"[underline]I[/] heard [underline on blue]you[/] like panels\n\n\n\n" + "[underline]I[/] heard [underline on blue]you[/] like panels\n\n\n\n" +
"So I put a panel in a panel").Centered(); "So I put a panel in a panel").Centered();
AnsiConsole.Render( AnsiConsole.Write(
new Panel( new Panel(
new Panel(content) new Panel(content)
.Border(BoxBorder.Rounded))); .Border(BoxBorder.Rounded)));
// Left adjusted panel with text // Left adjusted panel with text
AnsiConsole.Render( AnsiConsole.Write(
new Panel(new Text("Left adjusted\nLeft").LeftAligned()) new Panel(new Text("Left adjusted\nLeft").LeftAligned())
.Expand() .Expand()
.SquareBorder() .SquareBorder()
.Header("[red]Left[/]")); .Header("[red]Left[/]"));
// Centered ASCII panel with text // Centered ASCII panel with text
AnsiConsole.Render( AnsiConsole.Write(
new Panel(new Text("Centered\nCenter").Centered()) new Panel(new Text("Centered\nCenter").Centered())
.Expand() .Expand()
.AsciiBorder() .AsciiBorder()
@ -29,7 +29,7 @@ namespace Spectre.Console.Examples
.HeaderAlignment(Justify.Center)); .HeaderAlignment(Justify.Center));
// Right adjusted, rounded panel with text // Right adjusted, rounded panel with text
AnsiConsole.Render( AnsiConsole.Write(
new Panel(new Text("Right adjusted\nRight").RightAligned()) new Panel(new Text("Right adjusted\nRight").RightAligned())
.Expand() .Expand()
.RoundedBorder() .RoundedBorder()

View File

@ -28,8 +28,8 @@ namespace Spectre.Console.Examples
// Summary // Summary
AnsiConsole.WriteLine(); AnsiConsole.WriteLine();
AnsiConsole.Render(new Rule("[yellow]Results[/]").RuleStyle("grey").LeftAligned()); AnsiConsole.Write(new Rule("[yellow]Results[/]").RuleStyle("grey").LeftAligned());
AnsiConsole.Render(new Table().AddColumns("[grey]Question[/]", "[grey]Answer[/]") AnsiConsole.Write(new Table().AddColumns("[grey]Question[/]", "[grey]Answer[/]")
.RoundedBorder() .RoundedBorder()
.BorderColor(Color.Grey) .BorderColor(Color.Grey)
.AddRow("[grey]Name[/]", name) .AddRow("[grey]Name[/]", name)
@ -43,7 +43,7 @@ namespace Spectre.Console.Examples
private static string AskName() private static string AskName()
{ {
AnsiConsole.WriteLine(); AnsiConsole.WriteLine();
AnsiConsole.Render(new Rule("[yellow]Strings[/]").RuleStyle("grey").LeftAligned()); AnsiConsole.Write(new Rule("[yellow]Strings[/]").RuleStyle("grey").LeftAligned());
var name = AnsiConsole.Ask<string>("What's your [green]name[/]?"); var name = AnsiConsole.Ask<string>("What's your [green]name[/]?");
return name; return name;
} }
@ -51,7 +51,7 @@ namespace Spectre.Console.Examples
private static string AskFruit() private static string AskFruit()
{ {
AnsiConsole.WriteLine(); AnsiConsole.WriteLine();
AnsiConsole.Render(new Rule("[yellow]Lists[/]").RuleStyle("grey").LeftAligned()); AnsiConsole.Write(new Rule("[yellow]Lists[/]").RuleStyle("grey").LeftAligned());
var favorites = AnsiConsole.Prompt( var favorites = AnsiConsole.Prompt(
new MultiSelectionPrompt<string>() new MultiSelectionPrompt<string>()
@ -90,7 +90,7 @@ namespace Spectre.Console.Examples
private static string AskSport() private static string AskSport()
{ {
AnsiConsole.WriteLine(); AnsiConsole.WriteLine();
AnsiConsole.Render(new Rule("[yellow]Choices[/]").RuleStyle("grey").LeftAligned()); AnsiConsole.Write(new Rule("[yellow]Choices[/]").RuleStyle("grey").LeftAligned());
return AnsiConsole.Prompt( return AnsiConsole.Prompt(
new TextPrompt<string>("What's your [green]favorite sport[/]?") new TextPrompt<string>("What's your [green]favorite sport[/]?")
@ -104,7 +104,7 @@ namespace Spectre.Console.Examples
private static int AskAge() private static int AskAge()
{ {
AnsiConsole.WriteLine(); AnsiConsole.WriteLine();
AnsiConsole.Render(new Rule("[yellow]Integers[/]").RuleStyle("grey").LeftAligned()); AnsiConsole.Write(new Rule("[yellow]Integers[/]").RuleStyle("grey").LeftAligned());
return AnsiConsole.Prompt( return AnsiConsole.Prompt(
new TextPrompt<int>("How [green]old[/] are you?") new TextPrompt<int>("How [green]old[/] are you?")
@ -124,7 +124,7 @@ namespace Spectre.Console.Examples
private static string AskPassword() private static string AskPassword()
{ {
AnsiConsole.WriteLine(); AnsiConsole.WriteLine();
AnsiConsole.Render(new Rule("[yellow]Secrets[/]").RuleStyle("grey").LeftAligned()); AnsiConsole.Write(new Rule("[yellow]Secrets[/]").RuleStyle("grey").LeftAligned());
return AnsiConsole.Prompt( return AnsiConsole.Prompt(
new TextPrompt<string>("Enter [green]password[/]?") new TextPrompt<string>("Enter [green]password[/]?")
@ -135,7 +135,7 @@ namespace Spectre.Console.Examples
private static string AskColor() private static string AskColor()
{ {
AnsiConsole.WriteLine(); AnsiConsole.WriteLine();
AnsiConsole.Render(new Rule("[yellow]Optional[/]").RuleStyle("grey").LeftAligned()); AnsiConsole.Write(new Rule("[yellow]Optional[/]").RuleStyle("grey").LeftAligned());
return AnsiConsole.Prompt( return AnsiConsole.Prompt(
new TextPrompt<string>("[grey][[Optional]][/] What is your [green]favorite color[/]?") new TextPrompt<string>("[grey][[Optional]][/] What is your [green]favorite color[/]?")

View File

@ -34,7 +34,7 @@ namespace Spectre.Console.Examples
private static void Render(Rule rule) private static void Render(Rule rule)
{ {
AnsiConsole.Render(rule); AnsiConsole.Write(rule);
AnsiConsole.WriteLine(); AnsiConsole.WriteLine();
} }
} }

View File

@ -83,7 +83,7 @@ namespace Spectre.Console.Examples
// Render the table // Render the table
AnsiConsole.WriteLine(); AnsiConsole.WriteLine();
AnsiConsole.Render(table); AnsiConsole.Write(table);
} }
private static IRenderable GetColorTable() private static IRenderable GetColorTable()

View File

@ -4,7 +4,7 @@ namespace Spectre.Console.Examples
{ {
public static void Main() public static void Main()
{ {
AnsiConsole.Render(CreateTable()); AnsiConsole.Write(CreateTable());
} }
private static Table CreateTable() private static Table CreateTable()

View File

@ -8,7 +8,7 @@ namespace Spectre.Console.Examples
// Render the tree // Render the tree
var tree = BuildTree(); var tree = BuildTree();
AnsiConsole.Render(tree); AnsiConsole.Write(tree);
} }
private static Tree BuildTree() private static Tree BuildTree()

View File

@ -12,7 +12,17 @@ namespace Spectre.Console
/// Renders the specified object to the console. /// Renders the specified object to the console.
/// </summary> /// </summary>
/// <param name="renderable">The object to render.</param> /// <param name="renderable">The object to render.</param>
[Obsolete("Consider using AnsiConsole.Write instead.")]
public static void Render(IRenderable renderable) public static void Render(IRenderable renderable)
{
Write(renderable);
}
/// <summary>
/// Renders the specified <see cref="IRenderable"/> to the console.
/// </summary>
/// <param name="renderable">The object to render.</param>
public static void Write(IRenderable renderable)
{ {
if (renderable is null) if (renderable is null)
{ {