Remove verbs from extension methods

Removed the verbs from all extension methods that manipulate
properties which makes the API more succinct and easier to read.

Also added implicit conversion from string to Style.

As a good OSS citizen, I've obsoleted the old methods with
a warning for now, so this shouldn't break anyone using
the old methods.
This commit is contained in:
Patrik Svensson
2020-10-22 00:32:07 +02:00
committed by Patrik Svensson
parent 037a215a78
commit 041bd016a2
53 changed files with 1021 additions and 245 deletions

View File

@ -8,29 +8,23 @@ namespace BordersExample
{
public static void Main()
{
Debugger.Launch();
// Render panel borders
AnsiConsole.WriteLine();
AnsiConsole.MarkupLine("[white bold underline]PANEL BORDERS[/]");
AnsiConsole.WriteLine();
RenderPanelBorders();
HorizontalRule("PANEL BORDERS");
PanelBorders();
// Render table borders
AnsiConsole.WriteLine();
AnsiConsole.MarkupLine("[white bold underline]TABLE BORDERS[/]");
AnsiConsole.WriteLine();
RenderTableBorders();
HorizontalRule("TABLE BORDERS");
TableBorders();
}
private static void RenderPanelBorders()
private static void PanelBorders()
{
static IRenderable CreatePanel(string name, BoxBorder border)
{
return new Panel($"This is a panel with\nthe [yellow]{name}[/] border.")
.SetHeader($" {name} ", Style.Parse("blue"), Justify.Center)
.SetBorderStyle(Style.Parse("grey"))
.SetBorder(border);
.Header($" {name} ", Style.Parse("blue"), Justify.Center)
.Border(border)
.BorderStyle(Style.Parse("grey"));
}
var items = new[]
@ -49,19 +43,18 @@ namespace BordersExample
new Padding(2,0,0,0)));
}
private static void RenderTableBorders()
private static void TableBorders()
{
static IRenderable CreateTable(string name, TableBorder border)
{
var table = new Table().SetBorder(border);
var table = new Table().Border(border);
table.AddColumn("[yellow]Header 1[/]");
table.AddColumn("[yellow]Header 2[/]", col => col.RightAligned());
table.AddRow("Cell", "Cell");
table.AddRow("Cell", "Cell");
return new Panel(table)
.SetHeader($" {name} ", Style.Parse("blue"), Justify.Center)
.SetBorderStyle(Style.Parse("grey"))
.Header($" {name} ", Style.Parse("blue"), Justify.Center)
.NoBorder();
}
@ -88,5 +81,12 @@ namespace BordersExample
AnsiConsole.Render(new Columns(items).Collapse());
}
private static void HorizontalRule(string title)
{
AnsiConsole.WriteLine();
AnsiConsole.Render(new Rule($"[white bold]{title}[/]").RuleStyle("grey").LeftAligned());
AnsiConsole.WriteLine();
}
}
}

View File

@ -9,8 +9,8 @@ namespace Calendars
AnsiConsole.WriteLine();
AnsiConsole.Render(new Calendar(2020, 10)
.RoundedBorder()
.SetHighlightStyle(Style.Parse("red"))
.SetHeaderStyle(Style.Parse("yellow"))
.HighlightStyle(Style.Parse("red"))
.HeaderStyle(Style.Parse("yellow"))
.AddCalendarEvent("An event", 2020, 9, 22)
.AddCalendarEvent("Another event", 2020, 10, 2)
.AddCalendarEvent("A third event", 2020, 10, 13));

View File

@ -24,7 +24,7 @@ namespace ColorExample
AnsiConsole.ResetColors();
AnsiConsole.WriteLine();
AnsiConsole.Render(new Rule("[yellow bold underline]3-bit Colors[/]").SetStyle("grey").LeftAligned());
AnsiConsole.Render(new Rule("[yellow bold underline]3-bit Colors[/]").RuleStyle("grey").LeftAligned());
AnsiConsole.WriteLine();
for (var i = 0; i < 8; i++)
@ -47,7 +47,7 @@ namespace ColorExample
AnsiConsole.ResetColors();
AnsiConsole.WriteLine();
AnsiConsole.Render(new Rule("[yellow bold underline]4-bit Colors[/]").SetStyle("grey").LeftAligned());
AnsiConsole.Render(new Rule("[yellow bold underline]4-bit Colors[/]").RuleStyle("grey").LeftAligned());
AnsiConsole.WriteLine();
for (var i = 0; i < 16; i++)
@ -70,7 +70,7 @@ namespace ColorExample
AnsiConsole.ResetColors();
AnsiConsole.WriteLine();
AnsiConsole.Render(new Rule("[yellow bold underline]8-bit Colors[/]").SetStyle("grey").LeftAligned());
AnsiConsole.Render(new Rule("[yellow bold underline]8-bit Colors[/]").RuleStyle("grey").LeftAligned());
AnsiConsole.WriteLine();
for (var i = 0; i < 16; i++)
@ -97,7 +97,7 @@ namespace ColorExample
AnsiConsole.ResetColors();
AnsiConsole.WriteLine();
AnsiConsole.Render(new Rule("[yellow bold underline]24-bit Colors[/]").SetStyle("grey").LeftAligned());
AnsiConsole.Render(new Rule("[yellow bold underline]24-bit Colors[/]").RuleStyle("grey").LeftAligned());
AnsiConsole.WriteLine();
var index = 0;

View File

@ -19,8 +19,8 @@ namespace ColumnsExample
var cards = new List<Panel>();
foreach(var user in users.results)
{
cards.Add(new Panel(GetCard(user))
.SetHeader($"{user.location.country}")
cards.Add(new Panel(GetCardContent(user))
.Header($"{user.location.country}")
.RoundedBorder().Expand());
}
@ -28,7 +28,7 @@ namespace ColumnsExample
AnsiConsole.Render(new Columns(cards));
}
private static string GetCard(dynamic user)
private static string GetCardContent(dynamic user)
{
var name = $"{user.name.first} {user.name.last}";
var country = $"{user.location.city}";

View File

@ -32,15 +32,15 @@ namespace Exceptions
Format = ExceptionFormats.ShortenEverything | ExceptionFormats.ShowLinks,
Style = new ExceptionStyle
{
Exception = Style.WithForeground(Color.Grey),
Message = Style.WithForeground(Color.White),
NonEmphasized = Style.WithForeground(Color.Cornsilk1),
Parenthesis = Style.WithForeground(Color.Cornsilk1),
Method = Style.WithForeground(Color.Red),
ParameterName = Style.WithForeground(Color.Cornsilk1),
ParameterType = Style.WithForeground(Color.Red),
Path = Style.WithForeground(Color.Red),
LineNumber = Style.WithForeground(Color.Cornsilk1),
Exception = new Style().Foreground(Color.Grey),
Message = new Style().Foreground(Color.White),
NonEmphasized = new Style().Foreground(Color.Cornsilk1),
Parenthesis = new Style().Foreground(Color.Cornsilk1),
Method = new Style().Foreground(Color.Red),
ParameterName = new Style().Foreground(Color.Cornsilk1),
ParameterType = new Style().Foreground(Color.Red),
Path = new Style().Foreground(Color.Red),
LineNumber = new Style().Foreground(Color.Cornsilk1),
}
});
}

View File

@ -11,7 +11,7 @@ namespace GridExample
AnsiConsole.WriteLine();
var grid = new Grid();
grid.AddColumn(new GridColumn { NoWrap = true });
grid.AddColumn(new GridColumn().NoWrap());
grid.AddColumn(new GridColumn().PadLeft(2));
grid.AddRow("Options:");
grid.AddRow(" [blue]-h[/], [blue]--help[/]", "Show command line help.");

View File

@ -17,7 +17,7 @@ namespace InfoExample
AnsiConsole.Render(
new Panel(grid)
.SetHeader("Information"));
.Header("Information"));
}
private static string YesNo(bool value)

View File

@ -13,28 +13,33 @@ namespace PanelExample
AnsiConsole.Render(
new Panel(
new Panel(content)
.SetBorder(BoxBorder.Rounded)));
.Border(BoxBorder.Rounded)));
// Left adjusted panel with text
AnsiConsole.Render(
new Panel(new Text("Left adjusted\nLeft").LeftAligned())
.Expand()
.SquareBorder()
.SetHeader("Left", Style.WithForeground(Color.Red)));
.Header("Left")
.HeaderStyle("red"));
// Centered ASCII panel with text
AnsiConsole.Render(
new Panel(new Text("Centered\nCenter").Centered())
.Expand()
.AsciiBorder()
.SetHeader("Center", Style.WithForeground(Color.Green), Justify.Center));
.Header("Center")
.HeaderStyle("green")
.HeaderAlignment(Justify.Center));
// Right adjusted, rounded panel with text
AnsiConsole.Render(
new Panel(new Text("Right adjusted\nRight").RightAligned())
.Expand()
.RoundedBorder()
.SetHeader("Right", Style.WithForeground(Color.Blue), Justify.Right));
.Header("Right")
.HeaderStyle("blue")
.HeaderAlignment(Justify.Right));
}
}
}

View File

@ -7,21 +7,33 @@ namespace EmojiExample
public static void Main(string[] args)
{
// No title
Render(new Rule().SetStyle("yellow"));
WrapInPanel(
new Rule()
.RuleStyle(Style.Parse("yellow"))
.LeftAligned());
// Left aligned title
Render(new Rule("[white]Left aligned[/]").LeftAligned().SetStyle("red"));
WrapInPanel(
new Rule("[white]Left aligned[/]")
.RuleStyle(Style.Parse("red"))
.LeftAligned());
// Centered title
Render(new Rule("[silver]Centered[/]").Centered().SetStyle("green"));
WrapInPanel(
new Rule("[silver]Centered[/]")
.RuleStyle(Style.Parse("green"))
.Centered());
// Right aligned title
Render(new Rule("[grey]Right aligned[/]").RightAligned().SetStyle("blue"));
WrapInPanel(
new Rule("[grey]Right aligned[/]")
.RuleStyle(Style.Parse("blue"))
.RightAligned());
}
private static void Render(Rule rule)
private static void WrapInPanel(Rule rule)
{
AnsiConsole.Render(new Panel(rule).Expand().SetBorderStyle(Style.Parse("grey")));
AnsiConsole.Render(new Panel(rule).Expand().BorderStyle(Style.Parse("grey")));
AnsiConsole.WriteLine();
}
}

View File

@ -16,8 +16,8 @@ namespace TableExample
private static Table CreateTable()
{
var simple = new Table()
.SetBorder(TableBorder.Square)
.SetBorderColor(Color.Red)
.Border(TableBorder.Square)
.BorderColor(Color.Red)
.AddColumn(new TableColumn("[u]CDE[/]").Centered())
.AddColumn(new TableColumn("[u]FED[/]"))
.AddColumn(new TableColumn("[u]IHG[/]"))
@ -26,8 +26,8 @@ namespace TableExample
.AddRow("[blue]Hej[/]", "[yellow]Världen![/]", "");
var second = new Table()
.SetBorder(TableBorder.Rounded)
.SetBorderColor(Color.Green)
.Border(TableBorder.Rounded)
.BorderColor(Color.Green)
.AddColumn(new TableColumn("[u]Foo[/]"))
.AddColumn(new TableColumn("[u]Bar[/]"))
.AddColumn(new TableColumn("[u]Baz[/]"))
@ -37,12 +37,12 @@ namespace TableExample
return new Table()
.Centered()
.SetBorder(TableBorder.DoubleEdge)
.SetHeading("TABLE [yellow]HEADING[/]")
.SetFootnote("TABLE [yellow]FOOTNOTE[/]")
.AddColumn(new TableColumn(new Panel("[u]ABC[/]").SetBorderColor(Color.Red)))
.AddColumn(new TableColumn(new Panel("[u]DEF[/]").SetBorderColor(Color.Green)))
.AddColumn(new TableColumn(new Panel("[u]GHI[/]").SetBorderColor(Color.Blue)))
.Border(TableBorder.DoubleEdge)
.Heading("TABLE [yellow]HEADING[/]")
.Footnote("TABLE [yellow]FOOTNOTE[/]")
.AddColumn(new TableColumn(new Panel("[u]ABC[/]").BorderColor(Color.Red)))
.AddColumn(new TableColumn(new Panel("[u]DEF[/]").BorderColor(Color.Green)))
.AddColumn(new TableColumn(new Panel("[u]GHI[/]").BorderColor(Color.Blue)))
.AddRow(new Text("Hello").Centered(), new Markup("[red]World![/]"), Text.Empty)
.AddRow(second, new Text("Whaaat"), new Text("Lol"))
.AddRow(new Markup("[blue]Hej[/]").Centered(), new Markup("[yellow]Världen![/]"), Text.Empty);