Compare commits

..

11 Commits

Author SHA1 Message Date
Patrik Svensson
380c6aca45 Add net5.0 target framework 2020-11-11 15:28:32 +01:00
Patrik Svensson
b1da5e7ba8 Add support for markup text in panel header 2020-11-07 20:43:53 +01:00
Patrik Svensson
be3350a411 Bump dotnet-example to 1.0.0 2020-11-03 18:14:41 +01:00
Patrik Svensson
a1d11e9d0c Add support for moving the cursor 2020-10-28 18:57:08 +01:00
Patrik Svensson
93d1971f48 Update rule example and docs 2020-10-27 17:30:50 +01:00
Martin Andersen
bca1c889d1 Add Padding extension
Add extension method for specifying horizontal and vertical padding.
Similar constructor for `Padding` already existed.

Update documentation for table column appearance (padding).

Update
`Should_Render_Padded_Object_Correctly_When_Nested_Within_Other_Object`
test to use new extension method.
2020-10-27 09:09:30 +01:00
Takahito Yamatoya
9915a0d6a8 Update Documentation for escape 2020-10-27 09:08:27 +01:00
Takahito Yamatoya
f34fc43d00 Update documentation for aligning tables 2020-10-27 09:08:27 +01:00
Patrik Svensson
e7f497050c Add row and column accessors for tables and grids 2020-10-26 18:15:27 +01:00
Martin Andersen
3e5e22d6c2 Update documentation
- ExceptionFormat -> ExceptionFormats.
- Fix link to documentation.
- Add cross reference to Styles.
- Render table in example code.
- Add code for setting background color.
2020-10-26 15:31:44 +01:00
Patrik Svensson
10daf727e9 Fix outdated documentation 2020-10-26 12:20:17 +01:00
104 changed files with 1516 additions and 672 deletions

View File

@@ -21,7 +21,7 @@ jobs:
- name: Setup dotnet
uses: actions/setup-dotnet@v1
with:
dotnet-version: '3.1.301' # SDK Version to use.
dotnet-version: 5.0.100
- name: Build
shell: bash
@@ -55,10 +55,15 @@ jobs:
with:
fetch-depth: 0
- name: Setup dotnet
- name: Setup dotnet 3.1.402
uses: actions/setup-dotnet@v1
with:
dotnet-version: 3.1.301
dotnet-version: 3.1.402
- name: Setup dotnet 5.0.100
uses: actions/setup-dotnet@v1
with:
dotnet-version: 5.0.100
- name: Integration Tests
shell: bash

View File

@@ -24,7 +24,7 @@ jobs:
- name: Setup dotnet
uses: actions/setup-dotnet@v1
with:
dotnet-version: '3.1.301' # SDK Version to use.
dotnet-version: 5.0.100
- name: Publish
shell: bash

View File

@@ -29,7 +29,7 @@ jobs:
- name: Setup dotnet
uses: actions/setup-dotnet@v1
with:
dotnet-version: '3.1.301' # SDK Version to use.
dotnet-version: 5.0.100
- name: Build
shell: bash
@@ -64,10 +64,15 @@ jobs:
with:
fetch-depth: 0
- name: Setup dotnet
- name: Setup dotnet 3.1.402
uses: actions/setup-dotnet@v1
with:
dotnet-version: 3.1.301
dotnet-version: 3.1.402
- name: Setup dotnet 5.0.100
uses: actions/setup-dotnet@v1
with:
dotnet-version: 5.0.100
- name: Build
shell: bash
@@ -90,10 +95,15 @@ jobs:
with:
fetch-depth: 0
- name: Setup dotnet
- name: Setup dotnet 3.1.402
uses: actions/setup-dotnet@v1
with:
dotnet-version: 3.1.301
dotnet-version: 3.1.402
- name: Setup dotnet 5.0.100
uses: actions/setup-dotnet@v1
with:
dotnet-version: 5.0.100
- name: Publish
shell: bash

View File

@@ -88,7 +88,7 @@ Spectre.Consoleでできることを見るために、
```
> dotnet tool install -g dotnet-example
> dotnet tool restore
```
このリポジトリで提供している例が一覧表示されます
@@ -381,8 +381,8 @@ AnsiConsole.WriteException(ex);
```csharp
AnsiConsole.WriteException(ex,
ExceptionFormat.ShortenPaths | ExceptionFormat.ShortenTypes |
ExceptionFormat.ShortenMethods | ExceptionFormat.ShowLinks);
ExceptionFormats.ShortenPaths | ExceptionFormats.ShortenTypes |
ExceptionFormats.ShortenMethods | ExceptionFormats.ShowLinks);
```
![exception](docs/input/assets/images/compact_exception.png)

View File

@@ -49,7 +49,7 @@ regular `System.Console` API.
If the current terminal does not support ANSI escape sequences,
`Spectre.Console` will fallback to using the `System.Console` API.
_NOTE: This library is currently under development and API's
_NOTE: This library is currently under development and APIs
might change or get removed at any point up until a 1.0 release._
### Using the static API
@@ -100,7 +100,7 @@ To see Spectre.Console in action, install the
global tool.
```
> dotnet tool install -g dotnet-example
> dotnet tool restore
```
Now you can list available examples in this repository:

View File

@@ -2,7 +2,7 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFramework>net5.0</TargetFramework>
<RunWorkingDirectory>$(MSBuildProjectDirectory)</RunWorkingDirectory>
<DefaultItemExcludes>$(DefaultItemExcludes);output\**;.gitignore</DefaultItemExcludes>
<MinVerSkip Condition="'$(Configuration)' == 'Debug'">true</MinVerSkip>
@@ -31,8 +31,8 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="Statiq.Web" Version="1.0.0-beta.5" />
<PackageReference Include="MinVer" PrivateAssets="All" Version="2.3.0" />
<PackageReference Include="Statiq.Web" Version="1.0.0-beta.11" />
<PackageReference Include="MinVer" PrivateAssets="All" Version="2.3.1" />
</ItemGroup>
<ItemGroup>

View File

@@ -10,7 +10,7 @@ The documentation site uses [Statiq](https://statiq.dev), a static site generato
> dotnet run preview --virtual-dir "spectre.console"
```
After the build is complete, you can navigate to [http://localhost:5080/spectre.consle](http://localhost:5080/spectre.console).
After the build is complete, you can navigate to [http://localhost:5080/spectre.console](http://localhost:5080/spectre.console).
**Note that the site runs under a virtual directory.**

View File

@@ -130,7 +130,7 @@
<div class="sidebar-nav-item @(Document.IdEquals(root) ? "active" : null)">
@if(root.ShowLink())
{
@Html.DocumentLink(root)
@Html.DocumentLink(root)
}
else
{
@@ -140,6 +140,11 @@
@foreach (IDocument document in OutputPages.GetChildrenOf(root).OnlyVisible())
{
if(string.IsNullOrWhiteSpace(document.GetTitle()))
{
continue;
}
DocumentList<IDocument> documentChildren = OutputPages.GetChildrenOf(document);
<div class="sidebar-nav-item @(Document.IdEquals(document) ? "active" : null) @(documentChildren.Any() ? "has-children" : null)">
@if(document.ShowLink())

Binary file not shown.

Before

Width:  |  Height:  |  Size: 5.7 KiB

After

Width:  |  Height:  |  Size: 24 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.3 MiB

After

Width:  |  Height:  |  Size: 1.4 MiB

View File

@@ -19,8 +19,8 @@ the hyperlinks are clickable is up to the terminal.
```csharp
AnsiConsole.WriteException(ex,
ExceptionFormat.ShortenPaths | ExceptionFormat.ShortenTypes |
ExceptionFormat.ShortenMethods | ExceptionFormat.ShowLinks);
ExceptionFormats.ShortenPaths | ExceptionFormats.ShortenTypes |
ExceptionFormats.ShortenMethods | ExceptionFormats.ShowLinks);
```
<img src="assets/images/compact_exception.png" style="max-width: 100%;">
@@ -36,15 +36,15 @@ AnsiConsole.WriteException(ex, new ExceptionSettings
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

@@ -1,4 +1,4 @@
Title: Welcome
Title: Welcome
Order: 0
---

View File

@@ -6,7 +6,7 @@ The class `Markup` allows you to output rich text to the console.
# Syntax
Console markup uses a syntax inspired by bbcode. If you write the style (see Styles)
Console markup uses a syntax inspired by bbcode. If you write the style (see [Styles](xref:styles))
in square brackets, e.g. `[bold red]`, that style will apply until it is closed with a `[/]`.
```csharp
@@ -21,6 +21,7 @@ rendering of `IRenderable` also have overloads for rendering rich text.
var table = new Table();
table.AddColumn(new TableColumn(new Markup("[yellow]Foo[/]")));
table.AddColumn(new TableColumn("[blue]Bar[/]"));
AnsiConsole.Render(table);
```
# Convenience methods
@@ -43,20 +44,24 @@ AnsiConsole.Markup("[[Hello]] "); // [Hello]
AnsiConsole.Markup("[red][[World]][/]"); // [World]
```
You can also use the `SafeMarkup` extension method.
You can also use the `EscapeMarkup` extension method.
```csharp
AnsiConsole.Markup("[red]{0}[/]", "Hello [World]".SafeMarkup());
AnsiConsole.Markup("[red]{0}[/]", "Hello [World]".EscapeMarkup());
```
You can also use the `Markup.Escape` method.
```csharp
AnsiConsole.Markup("[red]{0}[/]", Markup.Escape("Hello [World]"));
```
# Setting background color
You can set the background color in markup by prefixing the color with
`on`.
```
[bold yellow on blue]Hello[/]
[default on blue]World[/]
```csharp
AnsiConsole.Markup("[bold yellow on blue]Hello[/]");
AnsiConsole.Markup("[default on blue]World[/]");
```
# Rendering emojis
@@ -64,7 +69,7 @@ You can set the background color in markup by prefixing the color with
To output an emoji as part of markup, you can use emoji shortcodes.
```csharp
AnsiConsole.MarkupLine("Hello :globe_showing_europe_africa:!");
AnsiConsole.Markup("Hello :globe_showing_europe_africa:!");
```
For a list of emoji, see the [Emojis](xref:emojis) appendix section.

View File

@@ -12,7 +12,9 @@ To render a calendar, create a `Calendar` instance with a target date.
```csharp
var calendar = new Calendar(2020,10);
AnsiConsole.Render(calendar);
```
```text
2020 October
┌─────┬─────┬─────┬─────┬─────┬─────┬─────┐
│ Sun │ Mon │ Tue │ Wed │ Thu │ Fri │ Sat │
@@ -32,20 +34,22 @@ You can set the calendar's culture to show localized weekdays.
```csharp
var calendar = new Calendar(2020,10);
calendar.SetCulture("ja-JP");
calendar.Culture("ja-JP");
AnsiConsole.Render(calendar);
```
202010
┌────┬────┬────┬────┬────┬────┬────┐
├────┼────┼────┼────┼────┼────┼────┤
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
└────┴────┴────┴────┴────┴────┴────┘
```text
Oktober 2020
┌─────┬─────┬─────┬─────┬─────┬─────┬─────┐
│ Mån │ Tis │ Ons │ Tor │ Fre │ Lör │ Sön │
├─────┼─────┼─────┼─────┼─────┼─────┼─────┤
│ │ │ │ 1 │ 2 │ 3 │ 4 │
│ 5 │ 6 │ 7 │ 8 │ 9 │ 10 │ 11* │
│ 12 │ 13 │ 14 │ 15 │ 16 │ 17 │ 18 │
│ 19 │ 20 │ 21 │ 22 │ 23 │ 24 │ 25 │
│ 26 │ 27 │ 28 │ 29 │ 30 │ 31 │
│ │ │ │ │ │ │ │
└─────┴─────┴─────┴─────┴─────┴─────┴─────┘
```
## Header
@@ -54,9 +58,11 @@ You can hide the calendar header.
```csharp
var calendar = new Calendar(2020,10);
calendar.ShowHeader = false;
calendar.ShowHeader();
AnsiConsole.Render(calendar);
```
```text
┌─────┬─────┬─────┬─────┬─────┬─────┬─────┐
│ Sun │ Mon │ Tue │ Wed │ Thu │ Fri │ Sat │
├─────┼─────┼─────┼─────┼─────┼─────┼─────┤
@@ -72,13 +78,13 @@ AnsiConsole.Render(calendar);
You can set the header style of the calendar.
```csharp
var calendar = new Calendar(2020,10);
calendar.SetHeaderStyle(Style.Parse("blue bold"));
var calendar = new Calendar(2020, 10);
calendar.HeaderStyle(Style.Parse("blue bold"));
AnsiConsole.Render(calendar);
```
## Calendar Event
## Calendar Events
You can add an event to the calendar.
If a date has an event associated with it, the date gets highlighted in the calendar.
@@ -87,7 +93,9 @@ If a date has an event associated with it, the date gets highlighted in the cale
var calendar = new Calendar(2020,10);
calendar.AddCalendarEvent(2020, 10, 11);
AnsiConsole.Render(calendar);
```
```text
2020 October
┌─────┬─────┬─────┬─────┬─────┬─────┬─────┐
│ Sun │ Mon │ Tue │ Wed │ Thu │ Fri │ Sat │
@@ -106,9 +114,8 @@ AnsiConsole.Render(calendar);
You can set the highlight style for a calendar event via `SetHighlightStyle`.
```csharp
var calendar = new Calendar(2020,10);
var calendar = new Calendar(2020, 10);
calendar.AddCalendarEvent(2020, 10, 11);
calendar.SetHighlightStyle(Style.Parse("yellow bold"));
calendar.HighlightStyle(Style.Parse("yellow bold"));
AnsiConsole.Render(calendar);
```

View File

@@ -1,11 +1,11 @@
Title: Rule
Title: Rule
Order: 5
RedirectFrom: rule
---
The `Rule` class is used to render a horizontal rule (line) to the terminal.
![Rule](../assets/images/rule.png)
<img src="../assets/images/rule.png" style="width: 100%;" />
# Usage
@@ -23,8 +23,9 @@ You can set the rule title markup text.
```csharp
var rule = new Rule("[red]Hello[/]");
AnsiConsole.Render(rule);
```
// output
```text
───────────────────────────────── Hello ─────────────────────────────────
```
@@ -36,36 +37,36 @@ You can set the rule's title alignment.
var rule = new Rule("[red]Hello[/]");
rule.Alignment = Justify.Left;
AnsiConsole.Render(rule);
```
//output
```text
── Hello ────────────────────────────────────────────────────────────────
```
You can also specify it with a method
You can also specify it via an extension method:
```csharp
var rule = new Rule("[red]Hello[/]");
rule.LeftAligned();
AnsiConsole.Render(rule);
```
//output
```text
── Hello ────────────────────────────────────────────────────────────────
```
## Style
You can set the rule style.
## Styling
```csharp
var rule = new Rule("[red]Hello[/]");
rule.Style = Style.Parse("red dim");
AnsiConsole.Render(rule);
```
You can also specify it with a method
You can also specify it via an extension method
```csharp
var rule = new Rule("[red]Hello[/]");
rule.SetStyle("red dim");
rule.RuleStyle("red dim");
AnsiConsole.Render(rule);
```

View File

@@ -51,10 +51,10 @@ For a list of borders, see the [Borders](xref:borders) appendix section.
```csharp
// Sets the border
table.SetBorder(Border.None);
table.SetBorder(Border.Ascii);
table.SetBorder(Border.Square);
table.SetBorder(Border.Rounded);
table.Border(TableBorder.None);
table.Border(TableBorder.Ascii);
table.Border(TableBorder.Square);
table.Border(TableBorder.Rounded);
```
## Expand / Collapse
@@ -79,7 +79,16 @@ table.HideHeaders();
```csharp
// Sets the table width to 50 cells
table.SetWidth(50);
table.Width(50);
```
## Alignment
```csharp
table.Alignment(Justify.Right);
table.RightAligned();
table.Centered();
table.LeftAligned();
```
# Column appearance
@@ -91,31 +100,37 @@ table.SetWidth(50);
## Alignment
```csharp
// Set the alignment explicitly
column.SetAlignment(Justify.Right);
table.Columns[0].Alignment(Justify.Right);
table.Columns[0].LeftAligned();
table.Columns[0].Centered();
table.Columns[0].RightAligned();
```
## Padding
```csharp
// Set left and right padding
column.SetPadding(left: 3, right: 5);
// Set padding individually
table.Columns[0].PadLeft(3);
table.Columns[0].PadRight(5);
// Set padding individually.
column.PadLeft(3);
column.PadRight(5);
// Or chained together
table.Columns[0].PadLeft(3).PadRight(5);
// Or with the shorthand method if the left and right
// padding are identical. Vertical padding is ignored.
table.Columns[0].Padding(4, 0);
```
## Disable column wrapping
```csharp
// Disable column wrapping
column.NoWrap();
table.Columns[0].NoWrap();
```
## Set column width
```csharp
// Set the column width (no fluent extension method for this yet)
column.Width = 15;
// Set the column width
table.Columns[0].Width(15);
```

View File

@@ -3,7 +3,7 @@
"isRoot": true,
"tools": {
"cake.tool": {
"version": "0.38.4",
"version": "1.0.0-rc0001",
"commands": [
"dotnet-cake"
]
@@ -15,7 +15,7 @@
]
},
"dotnet-example": {
"version": "0.8.0",
"version": "1.1.0",
"commands": [
"dotnet-example"
]

View File

@@ -1,8 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFramework>net5.0</TargetFramework>
<IsPackable>false</IsPackable>
<Title>Borders</Title>
<Description>Demonstrates the different kind of borders.</Description>

View File

@@ -1,4 +1,3 @@
using System.Diagnostics;
using Spectre.Console;
using Spectre.Console.Rendering;
@@ -22,7 +21,7 @@ namespace BordersExample
static IRenderable CreatePanel(string name, BoxBorder border)
{
return new Panel($"This is a panel with\nthe [yellow]{name}[/] border.")
.Header($" {name} ", Style.Parse("blue"), Justify.Center)
.Header($" [blue]{name}[/] ", Justify.Center)
.Border(border)
.BorderStyle(Style.Parse("grey"));
}
@@ -54,7 +53,7 @@ namespace BordersExample
table.AddRow("Cell", "Cell");
return new Panel(table)
.Header($" {name} ", Style.Parse("blue"), Justify.Center)
.Header($" [blue]{name}[/] ", Justify.Center)
.NoBorder();
}

View File

@@ -2,7 +2,7 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFramework>net5.0</TargetFramework>
<IsPackable>false</IsPackable>
<Title>Calendars</Title>
<Description>Demonstrates how to render calendars.</Description>

View File

@@ -2,7 +2,7 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFramework>net5.0</TargetFramework>
<IsPackable>false</IsPackable>
<Title>Colors</Title>
<Description>Demonstrates how to use [yellow]c[/][red]o[/][green]l[/][blue]o[/][aqua]r[/][lime]s[/] in the console.</Description>

View File

@@ -2,7 +2,7 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFramework>net5.0</TargetFramework>
<IsPackable>false</IsPackable>
<Title>Columns</Title>
<Description>Demonstrates how to render data into columns.</Description>

View File

@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
<IsPackable>false</IsPackable>
<Title>Cursor</Title>
<Description>Demonstrates how to move the cursor.</Description>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\Spectre.Console\Spectre.Console.csproj" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,20 @@
using Spectre.Console;
namespace Cursor
{
public static class Program
{
public static void Main(string[] args)
{
AnsiConsole.Write("Hello");
// Move the cursor 3 cells to the right
AnsiConsole.Cursor.Move(CursorDirection.Right, 3);
AnsiConsole.Write("World");
// Move the cursor 5 cells to the left.
AnsiConsole.Cursor.Move(CursorDirection.Left, 5);
AnsiConsole.WriteLine("Universe");
}
}
}

View File

@@ -2,7 +2,7 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFramework>net5.0</TargetFramework>
<IsPackable>false</IsPackable>
<Title>Emojis</Title>
<Description>Demonstrates how to render emojis.</Description>

View File

@@ -2,7 +2,7 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFramework>net5.0</TargetFramework>
<IsPackable>false</IsPackable>
<Title>Exceptions</Title>
<Description>Demonstrates how to render formatted exceptions.</Description>

View File

@@ -2,7 +2,7 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFramework>net5.0</TargetFramework>
<IsPackable>false</IsPackable>
<Title>Grids</Title>
<Description>Demonstrates how to render grids in a console.</Description>

View File

@@ -2,7 +2,7 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFramework>net5.0</TargetFramework>
<IsPackable>false</IsPackable>
<Title>Info</Title>
<Description>Displays the capabilities of the current console.</Description>

View File

@@ -2,7 +2,7 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFramework>net5.0</TargetFramework>
<IsPackable>false</IsPackable>
<Title>Links</Title>
<Description>Demonstrates how to render links in a console.</Description>

View File

@@ -2,7 +2,7 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFramework>net5.0</TargetFramework>
<IsPackable>false</IsPackable>
<Title>Panels</Title>
<Description>Demonstrates how to render items in panels.</Description>

View File

@@ -20,16 +20,14 @@ namespace PanelExample
new Panel(new Text("Left adjusted\nLeft").LeftAligned())
.Expand()
.SquareBorder()
.Header("Left")
.HeaderStyle("red"));
.Header("[red]Left[/]"));
// Centered ASCII panel with text
AnsiConsole.Render(
new Panel(new Text("Centered\nCenter").Centered())
.Expand()
.AsciiBorder()
.Header("Center")
.HeaderStyle("green")
.Header("[green]Center[/]")
.HeaderAlignment(Justify.Center));
// Right adjusted, rounded panel with text
@@ -37,8 +35,7 @@ namespace PanelExample
new Panel(new Text("Right adjusted\nRight").RightAligned())
.Expand()
.RoundedBorder()
.Header("Right")
.HeaderStyle("blue")
.Header("[blue]Right[/]")
.HeaderAlignment(Justify.Right));
}
}

View File

@@ -10,30 +10,33 @@ namespace EmojiExample
WrapInPanel(
new Rule()
.RuleStyle(Style.Parse("yellow"))
.AsciiBorder()
.LeftAligned());
// Left aligned title
WrapInPanel(
new Rule("[white]Left aligned[/]")
new Rule("[blue]Left aligned[/]")
.RuleStyle(Style.Parse("red"))
.DoubleBorder()
.LeftAligned());
// Centered title
WrapInPanel(
new Rule("[silver]Centered[/]")
new Rule("[green]Centered[/]")
.RuleStyle(Style.Parse("green"))
.HeavyBorder()
.Centered());
// Right aligned title
WrapInPanel(
new Rule("[grey]Right aligned[/]")
new Rule("[red]Right aligned[/]")
.RuleStyle(Style.Parse("blue"))
.RightAligned());
}
private static void WrapInPanel(Rule rule)
{
AnsiConsole.Render(new Panel(rule).Expand().BorderStyle(Style.Parse("grey")));
AnsiConsole.Render(rule);
AnsiConsole.WriteLine();
}
}

View File

@@ -2,7 +2,7 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFramework>net5.0</TargetFramework>
<IsPackable>false</IsPackable>
<Title>Rules</Title>
<Description>Demonstrates how to render horizontal rules (lines).</Description>

View File

@@ -2,7 +2,7 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFramework>net5.0</TargetFramework>
<IsPackable>false</IsPackable>
<Title>Tables</Title>
<Description>Demonstrates how to render tables in a console.</Description>

View File

@@ -1,7 +1,7 @@
{
"projects": [ "src" ],
"sdk": {
"version": "3.1.301",
"version": "5.0.100",
"rollForward": "latestPatch"
}
}

View File

@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFrameworks>net5.0;netcoreapp3.1</TargetFrameworks>
<IsPackable>false</IsPackable>
</PropertyGroup>

View File

@@ -17,6 +17,7 @@ namespace Spectre.Console.Tests
public Encoding Encoding => _console.Encoding;
public int Width { get; }
public int Height => _console.Height;
public IAnsiConsoleCursor Cursor => _console.Cursor;
public TestableAnsiConsole(ColorSystem system, AnsiSupport ansi = AnsiSupport.Yes, int width = 80)
{
@@ -37,6 +38,11 @@ namespace Spectre.Console.Tests
_writer?.Dispose();
}
public void Clear(bool home)
{
_console.Clear(home);
}
public void Write(Segment segment)
{
_console.Write(segment);

View File

@@ -14,6 +14,7 @@ namespace Spectre.Console.Tests.Tools
public Capabilities Capabilities => _console.Capabilities;
public Encoding Encoding => _console.Encoding;
public IAnsiConsoleCursor Cursor => _console.Cursor;
public int Width { get; }
public int Height => _console.Height;
@@ -36,6 +37,11 @@ namespace Spectre.Console.Tests.Tools
_writer?.Dispose();
}
public void Clear(bool home)
{
_console.Clear(home);
}
public void Write(Segment segment)
{
_console.Write(segment);

View File

@@ -11,6 +11,7 @@ namespace Spectre.Console.Tests
{
public Capabilities Capabilities { get; }
public Encoding Encoding { get; }
public IAnsiConsoleCursor Cursor => throw new NotSupportedException();
public int Width { get; }
public int Height { get; }
@@ -42,6 +43,10 @@ namespace Spectre.Console.Tests
Writer.Dispose();
}
public void Clear(bool home)
{
}
public void Write(Segment segment)
{
if (segment is null)

View File

@@ -53,7 +53,7 @@ namespace Spectre.Console.Tests.Unit
grid.AddRow("Foo");
// Then
grid.RowCount.ShouldBe(1);
grid.Rows.Count.ShouldBe(1);
}
[Fact]

View File

@@ -77,7 +77,7 @@ namespace Spectre.Console.Tests.Unit
table.AddColumn("Bar", c => c.PadLeft(0).PadRight(0));
table.AddRow("Baz", "Qux");
table.AddRow(new Text("Corgi"), new Padder(new Panel("Waldo"))
.Padding(2, 1, 2, 1));
.Padding(2, 1));
// When
console.Render(new Padder(table)

View File

@@ -316,14 +316,14 @@ namespace Spectre.Console.Tests.Unit
var panel = new Panel(grid)
.Expand().RoundedBorder()
.BorderStyle(new Style().Foreground(Color.Grey))
.Header("Short paths ", new Style().Foreground(Color.Grey));
.Header("[grey]Short paths[/]");
// When
console.Render(panel);
// Then
console.Lines.Count.ShouldBe(4);
console.Lines[0].ShouldBe("╭─Short paths ─────────────────────────────────────────────────────────────────────╮");
console.Lines[0].ShouldBe("╭─Short paths─────────────────────────────────────────────────────────────────────╮");
console.Lines[1].ShouldBe("│ at System.Runtime.CompilerServices.TaskAwaiter. │");
console.Lines[2].ShouldBe("│ HandleNonSuccessAndDebuggerNotification(Task task) │");
console.Lines[3].ShouldBe("╰──────────────────────────────────────────────────────────────────────────────────╯");

View File

@@ -19,6 +19,34 @@ namespace Spectre.Console.Tests.Unit
console.Lines[0].ShouldBe("────────────────────────────────────────");
}
[Fact]
public void Should_Render_Default_Rule_With_Specified_Box()
{
// Given
var console = new PlainConsole(width: 40);
// When
console.Render(new Rule().DoubleBorder());
// Then
console.Lines.Count.ShouldBe(1);
console.Lines[0].ShouldBe("════════════════════════════════════════");
}
[Fact]
public void Should_Render_With_Specified_Box()
{
// Given
var console = new PlainConsole(width: 40);
// When
console.Render(new Rule("Hello World").DoubleBorder());
// Then
console.Lines.Count.ShouldBe(1);
console.Lines[0].ShouldBe("═════════════ Hello World ══════════════");
}
[Fact]
public void Should_Render_Default_Rule_With_Title_Centered_By_Default()
{

View File

@@ -98,7 +98,7 @@ namespace Spectre.Console.Tests.Unit
table.AddRow("Foo");
// Then
table.RowCount.ShouldBe(1);
table.Rows.Count.ShouldBe(1);
}
[Fact]

View File

@@ -48,6 +48,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Calendars", "..\examples\Ca
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Rules", "..\examples\Rules\Rules.csproj", "{8622A261-02C6-40CA-9797-E3F01ED87D6B}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Cursor", "..\examples\Cursor\Cursor.csproj", "{75C608C3-ABB4-4168-A229-7F8250B946D1}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@@ -226,6 +228,18 @@ Global
{8622A261-02C6-40CA-9797-E3F01ED87D6B}.Release|x64.Build.0 = Release|Any CPU
{8622A261-02C6-40CA-9797-E3F01ED87D6B}.Release|x86.ActiveCfg = Release|Any CPU
{8622A261-02C6-40CA-9797-E3F01ED87D6B}.Release|x86.Build.0 = Release|Any CPU
{75C608C3-ABB4-4168-A229-7F8250B946D1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{75C608C3-ABB4-4168-A229-7F8250B946D1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{75C608C3-ABB4-4168-A229-7F8250B946D1}.Debug|x64.ActiveCfg = Debug|Any CPU
{75C608C3-ABB4-4168-A229-7F8250B946D1}.Debug|x64.Build.0 = Debug|Any CPU
{75C608C3-ABB4-4168-A229-7F8250B946D1}.Debug|x86.ActiveCfg = Debug|Any CPU
{75C608C3-ABB4-4168-A229-7F8250B946D1}.Debug|x86.Build.0 = Debug|Any CPU
{75C608C3-ABB4-4168-A229-7F8250B946D1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{75C608C3-ABB4-4168-A229-7F8250B946D1}.Release|Any CPU.Build.0 = Release|Any CPU
{75C608C3-ABB4-4168-A229-7F8250B946D1}.Release|x64.ActiveCfg = Release|Any CPU
{75C608C3-ABB4-4168-A229-7F8250B946D1}.Release|x64.Build.0 = Release|Any CPU
{75C608C3-ABB4-4168-A229-7F8250B946D1}.Release|x86.ActiveCfg = Release|Any CPU
{75C608C3-ABB4-4168-A229-7F8250B946D1}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
@@ -244,6 +258,7 @@ Global
{C3E2CB5C-1517-4C75-B59A-93D4E22BEC8D} = {20595AD4-8D75-4AF8-B6BC-9C38C160423F}
{57691C7D-683D-46E6-AA4F-57A8C5F65D25} = {F0575243-121F-4DEE-9F6B-246E26DC0844}
{8622A261-02C6-40CA-9797-E3F01ED87D6B} = {F0575243-121F-4DEE-9F6B-246E26DC0844}
{75C608C3-ABB4-4168-A229-7F8250B946D1} = {F0575243-121F-4DEE-9F6B-246E26DC0844}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {5729B071-67A0-48FB-8B1B-275E6822086C}

View File

@@ -27,6 +27,11 @@ namespace Spectre.Console
/// </summary>
public static IAnsiConsole Console => _recorder ?? _console.Value;
/// <summary>
/// Gets the <see cref="IAnsiConsoleCursor"/>.
/// </summary>
public static IAnsiConsoleCursor Cursor => _recorder?.Cursor ?? _console.Value.Cursor;
/// <summary>
/// Gets the console's capabilities.
/// </summary>
@@ -56,7 +61,7 @@ namespace Spectre.Console
/// <returns>An <see cref="IAnsiConsole"/> instance.</returns>
public static IAnsiConsole Create(AnsiConsoleSettings settings)
{
return AnsiConsoleBuilder.Build(settings);
return BackendBuilder.Build(settings);
}
}
}

View File

@@ -0,0 +1,28 @@
namespace Spectre.Console
{
/// <summary>
/// Represents cursor direction.
/// </summary>
public enum CursorDirection
{
/// <summary>
/// Up
/// </summary>
Up,
/// <summary>
/// Down
/// </summary>
Down,
/// <summary>
/// Left
/// </summary>
Left,
/// <summary>
/// Right
/// </summary>
Right,
}
}

View File

@@ -96,5 +96,37 @@ namespace Spectre.Console
calendar.HeaderStyle = style ?? Style.Plain;
return calendar;
}
/// <summary>
/// Shows the calendar header.
/// </summary>
/// <param name="calendar">The calendar.</param>
/// <returns>The same instance so that multiple calls can be chained.</returns>
public static Calendar ShowHeader(this Calendar calendar)
{
if (calendar is null)
{
throw new ArgumentNullException(nameof(calendar));
}
calendar.ShowHeader = true;
return calendar;
}
/// <summary>
/// Hides the calendar header.
/// </summary>
/// <param name="calendar">The calendar.</param>
/// <returns>The same instance so that multiple calls can be chained.</returns>
public static Calendar HideHeader(this Calendar calendar)
{
if (calendar is null)
{
throw new ArgumentNullException(nameof(calendar));
}
calendar.ShowHeader = false;
return calendar;
}
}
}

View File

@@ -24,5 +24,24 @@ namespace Spectre.Console
obj.NoWrap = true;
return obj;
}
/// <summary>
/// Sets the width of the column.
/// </summary>
/// <typeparam name="T">An object implementing <see cref="IColumn"/>.</typeparam>
/// <param name="obj">The column.</param>
/// <param name="width">The column width.</param>
/// <returns>The same instance so that multiple calls can be chained.</returns>
public static T Width<T>(this T obj, int? width)
where T : class, IColumn
{
if (obj is null)
{
throw new ArgumentNullException(nameof(obj));
}
obj.Width = width;
return obj;
}
}
}

View File

@@ -0,0 +1,152 @@
namespace Spectre.Console
{
/// <summary>
/// Contains extension methods for <see cref="IAnsiConsoleCursor"/>.
/// </summary>
public static class CursorExtensions
{
/// <summary>
/// Shows the cursor.
/// </summary>
/// <param name="cursor">The cursor.</param>
public static void Show(this IAnsiConsoleCursor cursor)
{
if (cursor is null)
{
throw new System.ArgumentNullException(nameof(cursor));
}
cursor.Show(true);
}
/// <summary>
/// Hides the cursor.
/// </summary>
/// <param name="cursor">The cursor.</param>
public static void Hide(this IAnsiConsoleCursor cursor)
{
if (cursor is null)
{
throw new System.ArgumentNullException(nameof(cursor));
}
cursor.Show(false);
}
/// <summary>
/// Moves the cursor up.
/// </summary>
/// <param name="cursor">The cursor.</param>
public static void MoveUp(this IAnsiConsoleCursor cursor)
{
if (cursor is null)
{
throw new System.ArgumentNullException(nameof(cursor));
}
cursor.Move(CursorDirection.Up, 1);
}
/// <summary>
/// Moves the cursor up.
/// </summary>
/// <param name="cursor">The cursor.</param>
/// <param name="steps">The number of steps to move the cursor.</param>
public static void MoveUp(this IAnsiConsoleCursor cursor, int steps)
{
if (cursor is null)
{
throw new System.ArgumentNullException(nameof(cursor));
}
cursor.Move(CursorDirection.Up, steps);
}
/// <summary>
/// Moves the cursor down.
/// </summary>
/// <param name="cursor">The cursor.</param>
public static void MoveDown(this IAnsiConsoleCursor cursor)
{
if (cursor is null)
{
throw new System.ArgumentNullException(nameof(cursor));
}
cursor.Move(CursorDirection.Down, 1);
}
/// <summary>
/// Moves the cursor down.
/// </summary>
/// <param name="cursor">The cursor.</param>
/// <param name="steps">The number of steps to move the cursor.</param>
public static void MoveDown(this IAnsiConsoleCursor cursor, int steps)
{
if (cursor is null)
{
throw new System.ArgumentNullException(nameof(cursor));
}
cursor.Move(CursorDirection.Down, steps);
}
/// <summary>
/// Moves the cursor to the left.
/// </summary>
/// <param name="cursor">The cursor.</param>
public static void MoveLeft(this IAnsiConsoleCursor cursor)
{
if (cursor is null)
{
throw new System.ArgumentNullException(nameof(cursor));
}
cursor.Move(CursorDirection.Left, 1);
}
/// <summary>
/// Moves the cursor to the left.
/// </summary>
/// <param name="cursor">The cursor.</param>
/// <param name="steps">The number of steps to move the cursor.</param>
public static void MoveLeft(this IAnsiConsoleCursor cursor, int steps)
{
if (cursor is null)
{
throw new System.ArgumentNullException(nameof(cursor));
}
cursor.Move(CursorDirection.Left, steps);
}
/// <summary>
/// Moves the cursor to the right.
/// </summary>
/// <param name="cursor">The cursor.</param>
public static void MoveRight(this IAnsiConsoleCursor cursor)
{
if (cursor is null)
{
throw new System.ArgumentNullException(nameof(cursor));
}
cursor.Move(CursorDirection.Right, 1);
}
/// <summary>
/// Moves the cursor to the right.
/// </summary>
/// <param name="cursor">The cursor.</param>
/// <param name="steps">The number of steps to move the cursor.</param>
public static void MoveRight(this IAnsiConsoleCursor cursor, int steps)
{
if (cursor is null)
{
throw new System.ArgumentNullException(nameof(cursor));
}
cursor.Move(CursorDirection.Right, steps);
}
}
}

View File

@@ -6,6 +6,21 @@ namespace Spectre.Console.Internal
{
internal static class EnumerableExtensions
{
public static int GetCount<T>(this IEnumerable<T> source)
{
if (source is IList<T> list)
{
return list.Count;
}
if (source is T[] array)
{
return array.Length;
}
return source.Count();
}
public static void ForEach<T>(this IEnumerable<T> source, Action<T> action)
{
foreach (var item in source)
@@ -54,11 +69,13 @@ namespace Spectre.Console.Internal
return source.Select((value, index) => func(value, index));
}
#if !NET5_0
public static IEnumerable<(TFirst First, TSecond Second)> Zip<TFirst, TSecond>(
this IEnumerable<TFirst> source, IEnumerable<TSecond> first)
{
return source.Zip(first, (first, second) => (first, second));
}
#endif
public static IEnumerable<(TFirst First, TSecond Second, TThird Third)> Zip<TFirst, TSecond, TThird>(
this IEnumerable<TFirst> first, IEnumerable<TSecond> second, IEnumerable<TThird> third)

View File

@@ -1,4 +1,5 @@
using System;
using Spectre.Console.Internal;
using Spectre.Console.Rendering;
namespace Spectre.Console

View File

@@ -69,8 +69,8 @@ namespace Spectre.Console
throw new ArgumentNullException(nameof(grid));
}
var columns = new IRenderable[grid.ColumnCount];
Enumerable.Range(0, grid.ColumnCount).ForEach(index => columns[index] = Text.Empty);
var columns = new IRenderable[grid.Columns.Count];
Enumerable.Range(0, grid.Columns.Count).ForEach(index => columns[index] = Text.Empty);
grid.AddRow(columns);
return grid;

View File

@@ -30,10 +30,8 @@ namespace Spectre.Console
throw new ArgumentNullException(nameof(text));
}
style ??= panel.Header?.Style;
alignment ??= panel.Header?.Alignment;
return SetHeader(panel, new PanelHeader(text, style, alignment));
return SetHeader(panel, new PanelHeader(text, alignment));
}
/// <summary>
@@ -54,5 +52,18 @@ namespace Spectre.Console
panel.Header = header;
return panel;
}
/// <summary>
/// Sets the panel header style.
/// </summary>
/// <param name="panel">The panel.</param>
/// <param name="style">The header style.</param>
/// <returns>The same instance so that multiple calls can be chained.</returns>
[Obsolete("Use markup in header instead.")]
[EditorBrowsable(EditorBrowsableState.Never)]
public static Panel HeaderStyle(this Panel panel, Style style)
{
return panel;
}
}
}

View File

@@ -22,7 +22,7 @@ namespace Spectre.Console
throw new ArgumentNullException(nameof(obj));
}
return Padding(obj, new Padding(left, obj.Padding.Top, obj.Padding.Right, obj.Padding.Bottom));
return Padding(obj, new Padding(left, obj.Padding.GetTopSafe(), obj.Padding.GetRightSafe(), obj.Padding.GetBottomSafe()));
}
/// <summary>
@@ -40,7 +40,7 @@ namespace Spectre.Console
throw new ArgumentNullException(nameof(obj));
}
return Padding(obj, new Padding(obj.Padding.Left, top, obj.Padding.Right, obj.Padding.Bottom));
return Padding(obj, new Padding(obj.Padding.GetLeftSafe(), top, obj.Padding.GetRightSafe(), obj.Padding.GetBottomSafe()));
}
/// <summary>
@@ -58,7 +58,7 @@ namespace Spectre.Console
throw new ArgumentNullException(nameof(obj));
}
return Padding(obj, new Padding(obj.Padding.Left, obj.Padding.Top, right, obj.Padding.Bottom));
return Padding(obj, new Padding(obj.Padding.GetLeftSafe(), obj.Padding.GetTopSafe(), right, obj.Padding.GetBottomSafe()));
}
/// <summary>
@@ -76,11 +76,11 @@ namespace Spectre.Console
throw new ArgumentNullException(nameof(obj));
}
return Padding(obj, new Padding(obj.Padding.Left, obj.Padding.Top, obj.Padding.Right, bottom));
return Padding(obj, new Padding(obj.Padding.GetLeftSafe(), obj.Padding.GetTopSafe(), obj.Padding.GetRightSafe(), bottom));
}
/// <summary>
/// Sets the left and right padding.
/// Sets the left, top, right and bottom padding.
/// </summary>
/// <typeparam name="T">An object implementing <see cref="IPaddable"/>.</typeparam>
/// <param name="obj">The paddable object instance.</param>
@@ -95,6 +95,20 @@ namespace Spectre.Console
return Padding(obj, new Padding(left, top, right, bottom));
}
/// <summary>
/// Sets the horizontal and vertical padding.
/// </summary>
/// <typeparam name="T">An object implementing <see cref="IPaddable"/>.</typeparam>
/// <param name="obj">The paddable object instance.</param>
/// <param name="horizontal">The left and right padding.</param>
/// <param name="vertical">The top and bottom padding.</param>
/// <returns>The same instance so that multiple calls can be chained.</returns>
public static T Padding<T>(this T obj, int horizontal, int vertical)
where T : class, IPaddable
{
return Padding(obj, new Padding(horizontal, vertical));
}
/// <summary>
/// Sets the padding.
/// </summary>

View File

@@ -0,0 +1,48 @@
namespace Spectre.Console
{
/// <summary>
/// Contains extension methods for <see cref="Padding"/>.
/// </summary>
public static class PaddingExtensions
{
/// <summary>
/// Gets the left padding.
/// </summary>
/// <param name="padding">The padding.</param>
/// <returns>The left padding or zero if <c>padding</c> is null.</returns>
public static int GetLeftSafe(this Padding? padding)
{
return padding?.Left ?? 0;
}
/// <summary>
/// Gets the right padding.
/// </summary>
/// <param name="padding">The padding.</param>
/// <returns>The right padding or zero if <c>padding</c> is null.</returns>
public static int GetRightSafe(this Padding? padding)
{
return padding?.Right ?? 0;
}
/// <summary>
/// Gets the top padding.
/// </summary>
/// <param name="padding">The padding.</param>
/// <returns>The top padding or zero if <c>padding</c> is null.</returns>
public static int GetTopSafe(this Padding? padding)
{
return padding?.Top ?? 0;
}
/// <summary>
/// Gets the bottom padding.
/// </summary>
/// <param name="padding">The padding.</param>
/// <returns>The bottom padding or zero if <c>padding</c> is null.</returns>
public static int GetBottomSafe(this Padding? padding)
{
return padding?.Bottom ?? 0;
}
}
}

View File

@@ -12,10 +12,9 @@ namespace Spectre.Console
/// </summary>
/// <param name="panel">The panel.</param>
/// <param name="text">The header text.</param>
/// <param name="style">The header style.</param>
/// <param name="alignment">The header alignment.</param>
/// <returns>The same instance so that multiple calls can be chained.</returns>
public static Panel Header(this Panel panel, string text, Style? style = null, Justify? alignment = null)
public static Panel Header(this Panel panel, string text, Justify? alignment = null)
{
if (panel is null)
{
@@ -27,42 +26,8 @@ namespace Spectre.Console
throw new ArgumentNullException(nameof(text));
}
style ??= panel.Header?.Style;
alignment ??= panel.Header?.Alignment;
return Header(panel, new PanelHeader(text, style, alignment));
}
/// <summary>
/// Sets the panel header style.
/// </summary>
/// <param name="panel">The panel.</param>
/// <param name="style">The header style.</param>
/// <returns>The same instance so that multiple calls can be chained.</returns>
public static Panel HeaderStyle(this Panel panel, Style style)
{
if (panel is null)
{
throw new ArgumentNullException(nameof(panel));
}
if (style is null)
{
throw new ArgumentNullException(nameof(style));
}
if (panel.Header != null)
{
// Update existing style
panel.Header.Style = style;
}
else
{
// Create header
Header(panel, string.Empty, style, null);
}
return panel;
return Header(panel, new PanelHeader(text, alignment));
}
/// <summary>
@@ -86,7 +51,7 @@ namespace Spectre.Console
else
{
// Create header
Header(panel, string.Empty, null, alignment);
Header(panel, string.Empty, alignment);
}
return panel;

View File

@@ -1,3 +1,11 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using Spectre.Console.Internal;
using Spectre.Console.Rendering;
namespace Spectre.Console
{
/// <summary>
@@ -5,6 +13,11 @@ namespace Spectre.Console
/// </summary>
public static class StringExtensions
{
// Cache whether or not internally normalized line endings
// already are normalized. No reason to do yet another replace if it is.
private static readonly bool _alreadyNormalized
= Environment.NewLine.Equals("\n", StringComparison.OrdinalIgnoreCase);
/// <summary>
/// Escapes text so that it wont be interpreted as markup.
/// </summary>
@@ -18,8 +31,137 @@ namespace Spectre.Console
}
return text
.Replace("[", "[[")
.Replace("]", "]]");
.ReplaceExact("[", "[[")
.ReplaceExact("]", "]]");
}
internal static int CellLength(this string text, RenderContext context)
{
if (context is null)
{
throw new ArgumentNullException(nameof(context));
}
return Cell.GetCellLength(context, text);
}
internal static string Capitalize(this string text, CultureInfo? culture = null)
{
if (text == null)
{
return string.Empty;
}
culture ??= CultureInfo.InvariantCulture;
if (text.Length > 0 && char.IsLower(text[0]))
{
text = string.Format(culture, "{0}{1}", char.ToUpper(text[0], culture), text.Substring(1));
}
return text;
}
internal static string NormalizeLineEndings(this string? text, bool native = false)
{
text = text?.ReplaceExact("\r\n", "\n");
text = text?.ReplaceExact("\r", string.Empty);
text ??= string.Empty;
if (native && !_alreadyNormalized)
{
text = text.ReplaceExact("\n", Environment.NewLine);
}
return text;
}
internal static string[] SplitLines(this string text)
{
var result = text?.NormalizeLineEndings()?.Split(new[] { '\n' }, StringSplitOptions.None);
return result ?? Array.Empty<string>();
}
internal static string[] SplitWords(this string word, StringSplitOptions options = StringSplitOptions.None)
{
var result = new List<string>();
static string Read(StringBuffer reader, Func<char, bool> criteria)
{
var buffer = new StringBuilder();
while (!reader.Eof)
{
var current = reader.Peek();
if (!criteria(current))
{
break;
}
buffer.Append(reader.Read());
}
return buffer.ToString();
}
using (var reader = new StringBuffer(word))
{
while (!reader.Eof)
{
var current = reader.Peek();
if (char.IsWhiteSpace(current))
{
var x = Read(reader, c => char.IsWhiteSpace(c));
if (options != StringSplitOptions.RemoveEmptyEntries)
{
result.Add(x);
}
}
else
{
result.Add(Read(reader, c => !char.IsWhiteSpace(c)));
}
}
}
return result.ToArray();
}
internal static string Repeat(this string text, int count)
{
if (text is null)
{
throw new ArgumentNullException(nameof(text));
}
if (count <= 0)
{
return string.Empty;
}
if (count == 1)
{
return text;
}
return string.Concat(Enumerable.Repeat(text, count));
}
internal static string ReplaceExact(this string text, string oldValue, string? newValue)
{
#if NET5_0
return text.Replace(oldValue, newValue, StringComparison.Ordinal);
#else
return text.Replace(oldValue, newValue);
#endif
}
internal static bool ContainsExact(this string text, string value)
{
#if NET5_0
return text.Contains(value, StringComparison.Ordinal);
#else
return text.Contains(value);
#endif
}
}
}

View File

@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
namespace Spectre.Console
{
@@ -87,5 +88,26 @@ namespace Spectre.Console
decoration: style.Decoration,
link: link);
}
internal static Style Combine(this Style style, IEnumerable<Style> source)
{
if (style is null)
{
throw new ArgumentNullException(nameof(style));
}
if (source is null)
{
return style;
}
var current = style;
foreach (var item in source)
{
current = current.Combine(item);
}
return current;
}
}
}

View File

@@ -36,6 +36,22 @@ namespace Spectre.Console
return table;
}
/// <summary>
/// Adds a row to the table.
/// </summary>
/// <param name="table">The table to add the row to.</param>
/// <param name="columns">The row columns to add.</param>
/// <returns>The same instance so that multiple calls can be chained.</returns>
public static Table AddRow(this Table table, params IRenderable[] columns)
{
if (table is null)
{
throw new ArgumentNullException(nameof(table));
}
return table.AddRow(columns);
}
/// <summary>
/// Adds an empty row to the table.
/// </summary>
@@ -48,8 +64,8 @@ namespace Spectre.Console
throw new ArgumentNullException(nameof(table));
}
var columns = new IRenderable[table.ColumnCount];
Enumerable.Range(0, table.ColumnCount).ForEach(index => columns[index] = Text.Empty);
var columns = new IRenderable[table.Columns.Count];
Enumerable.Range(0, table.Columns.Count).ForEach(index => columns[index] = Text.Empty);
table.AddRow(columns);
return table;
}

View File

@@ -18,6 +18,11 @@ namespace Spectre.Console
/// </summary>
Encoding Encoding { get; }
/// <summary>
/// Gets the console cursor.
/// </summary>
IAnsiConsoleCursor Cursor { get; }
/// <summary>
/// Gets the buffer width of the console.
/// </summary>
@@ -28,6 +33,12 @@ namespace Spectre.Console
/// </summary>
int Height { get; }
/// <summary>
/// Clears the console.
/// </summary>
/// <param name="home">If the cursor should be moved to the home position.</param>
void Clear(bool home);
/// <summary>
/// Writes a string followed by a line terminator to the console.
/// </summary>

View File

@@ -0,0 +1,28 @@
namespace Spectre.Console
{
/// <summary>
/// Represents the console's cursor.
/// </summary>
public interface IAnsiConsoleCursor
{
/// <summary>
/// Shows or hides the cursor.
/// </summary>
/// <param name="show"><c>true</c> to show the cursor, <c>false</c> to hide it.</param>
void Show(bool show);
/// <summary>
/// Sets the cursor position.
/// </summary>
/// <param name="column">The column to move the cursor to.</param>
/// <param name="line">The line to move the cursor to.</param>
void SetPosition(int column, int line);
/// <summary>
/// Moves the cursor relative to the current position.
/// </summary>
/// <param name="direction">The direction to move the cursor.</param>
/// <param name="steps">The number of steps to move the cursor.</param>
void Move(CursorDirection direction, int steps);
}
}

View File

@@ -10,5 +10,10 @@ namespace Spectre.Console
/// or not wrapping should be prevented.
/// </summary>
bool NoWrap { get; set; }
/// <summary>
/// Gets or sets the width of the column.
/// </summary>
int? Width { get; set; }
}
}

View File

@@ -3,7 +3,7 @@ namespace Spectre.Console
/// <summary>
/// Represents something that has a box border.
/// </summary>
public interface IHasBoxBorder : IHasBorder
public interface IHasBoxBorder
{
/// <summary>
/// Gets or sets the box.

View File

@@ -8,6 +8,6 @@ namespace Spectre.Console
/// <summary>
/// Gets or sets the padding.
/// </summary>
public Padding Padding { get; set; }
public Padding? Padding { get; set; }
}
}

View File

@@ -5,13 +5,15 @@ using Spectre.Console.Rendering;
namespace Spectre.Console.Internal
{
internal sealed class AnsiConsoleRenderer : IAnsiConsole
internal sealed class AnsiBackend : IAnsiConsole
{
private readonly TextWriter _out;
private readonly AnsiBuilder _ansiBuilder;
private readonly AnsiCursor _cursor;
public Capabilities Capabilities { get; }
public Encoding Encoding { get; }
public IAnsiConsoleCursor Cursor => _cursor;
public int Width
{
@@ -39,7 +41,7 @@ namespace Spectre.Console.Internal
}
}
public AnsiConsoleRenderer(TextWriter @out, Capabilities capabilities, ILinkIdentityGenerator? linkHasher)
public AnsiBackend(TextWriter @out, Capabilities capabilities, ILinkIdentityGenerator? linkHasher)
{
_out = @out ?? throw new ArgumentNullException(nameof(@out));
@@ -47,6 +49,17 @@ namespace Spectre.Console.Internal
Encoding = _out.IsStandardOut() ? System.Console.OutputEncoding : Encoding.UTF8;
_ansiBuilder = new AnsiBuilder(Capabilities, linkHasher);
_cursor = new AnsiCursor(this);
}
public void Clear(bool home)
{
Write(Segment.Control("\u001b[2J"));
if (home)
{
Cursor.SetPosition(0, 0);
}
}
public void Write(Segment segment)

View File

@@ -0,0 +1,56 @@
using System;
using Spectre.Console.Rendering;
namespace Spectre.Console.Internal
{
internal sealed class AnsiCursor : IAnsiConsoleCursor
{
private readonly AnsiBackend _renderer;
public AnsiCursor(AnsiBackend renderer)
{
_renderer = renderer ?? throw new ArgumentNullException(nameof(renderer));
}
public void Show(bool show)
{
if (show)
{
_renderer.Write(Segment.Control("\u001b[?25h"));
}
else
{
_renderer.Write(Segment.Control("\u001b[?25l"));
}
}
public void Move(CursorDirection direction, int steps)
{
if (steps == 0)
{
return;
}
switch (direction)
{
case CursorDirection.Up:
_renderer.Write(Segment.Control($"\u001b[{steps}A"));
break;
case CursorDirection.Down:
_renderer.Write(Segment.Control($"\u001b[{steps}B"));
break;
case CursorDirection.Right:
_renderer.Write(Segment.Control($"\u001b[{steps}C"));
break;
case CursorDirection.Left:
_renderer.Write(Segment.Control($"\u001b[{steps}D"));
break;
}
}
public void SetPosition(int column, int line)
{
_renderer.Write(Segment.Control($"\u001b[{line};{column}H"));
}
}
}

View File

@@ -3,7 +3,7 @@ using System.Runtime.InteropServices;
namespace Spectre.Console.Internal
{
internal static class AnsiConsoleBuilder
internal static class BackendBuilder
{
public static IAnsiConsole Build(AnsiConsoleSettings settings)
{
@@ -60,11 +60,11 @@ namespace Spectre.Console.Internal
// Create the renderer
if (supportsAnsi)
{
return new AnsiConsoleRenderer(buffer, capabilities, settings.LinkIdentityGenerator);
return new AnsiBackend(buffer, capabilities, settings.LinkIdentityGenerator);
}
else
{
return new FallbackConsoleRenderer(buffer, capabilities);
return new FallbackBackend(buffer, capabilities);
}
}
}

View File

@@ -0,0 +1,89 @@
using System;
using System.IO;
using System.Text;
using Spectre.Console.Rendering;
namespace Spectre.Console.Internal
{
internal sealed class FallbackBackend : IAnsiConsole
{
private readonly ColorSystem _system;
private readonly FallbackCursor _cursor;
private Style? _lastStyle;
public Capabilities Capabilities { get; }
public Encoding Encoding { get; }
public IAnsiConsoleCursor Cursor => _cursor;
public int Width
{
get { return ConsoleHelper.GetSafeBufferWidth(Constants.DefaultBufferWidth); }
}
public int Height
{
get { return ConsoleHelper.GetSafeBufferHeight(Constants.DefaultBufferHeight); }
}
public FallbackBackend(TextWriter @out, Capabilities capabilities)
{
if (capabilities == null)
{
throw new ArgumentNullException(nameof(capabilities));
}
_system = capabilities.ColorSystem;
_cursor = new FallbackCursor();
if (@out != System.Console.Out)
{
System.Console.SetOut(@out ?? throw new ArgumentNullException(nameof(@out)));
}
Encoding = System.Console.OutputEncoding;
Capabilities = capabilities;
}
public void Clear(bool home)
{
var (x, y) = (System.Console.CursorLeft, System.Console.CursorTop);
System.Console.Clear();
if (!home)
{
// Set the cursor position
System.Console.SetCursorPosition(x, y);
}
}
public void Write(Segment segment)
{
if (_lastStyle?.Equals(segment.Style) != true)
{
SetStyle(segment.Style);
}
System.Console.Write(segment.Text.NormalizeLineEndings(native: true));
}
private void SetStyle(Style style)
{
_lastStyle = style;
System.Console.ResetColor();
var background = Color.ToConsoleColor(style.Background);
if (_system != ColorSystem.NoColors && (int)background != -1)
{
System.Console.BackgroundColor = background;
}
var foreground = Color.ToConsoleColor(style.Foreground);
if (_system != ColorSystem.NoColors && (int)foreground != -1)
{
System.Console.ForegroundColor = foreground;
}
}
}
}

View File

@@ -0,0 +1,40 @@
namespace Spectre.Console.Internal
{
internal sealed class FallbackCursor : IAnsiConsoleCursor
{
public void Show(bool show)
{
System.Console.CursorVisible = show;
}
public void Move(CursorDirection direction, int steps)
{
if (steps == 0)
{
return;
}
switch (direction)
{
case CursorDirection.Up:
System.Console.CursorTop -= steps;
break;
case CursorDirection.Down:
System.Console.CursorTop += steps;
break;
case CursorDirection.Left:
System.Console.CursorLeft -= steps;
break;
case CursorDirection.Right:
System.Console.CursorLeft += steps;
break;
}
}
public void SetPosition(int x, int y)
{
System.Console.CursorLeft = x;
System.Console.CursorTop = y;
}
}
}

View File

@@ -2,9 +2,9 @@ using System;
using System.Collections;
using System.Collections.Generic;
namespace Spectre.Console.Internal.Collections
namespace Spectre.Console.Internal
{
internal sealed class ListWithCallback<T> : IList<T>
internal sealed class ListWithCallback<T> : IList<T>, IReadOnlyList<T>
{
private readonly List<T> _list;
private readonly Action _callback;

View File

@@ -1,10 +1,9 @@
using System;
using System.Linq;
using System.Text;
using Spectre.Console.Internal;
using Spectre.Console.Rendering;
namespace Spectre.Console
namespace Spectre.Console.Internal
{
internal static class ExceptionFormatter
{

View File

@@ -31,7 +31,7 @@ namespace Spectre.Console.Internal
}
var line = lines.Dequeue();
line = line.Replace(" ---> ", string.Empty);
line = line.ReplaceExact(" ---> ", string.Empty);
var match = _messageRegex.Match(line);
if (!match.Success)

View File

@@ -1,127 +0,0 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using Spectre.Console.Rendering;
namespace Spectre.Console.Internal
{
internal static class StringExtensions
{
// Cache whether or not internally normalized line endings
// already are normalized. No reason to do yet another replace if it is.
private static readonly bool _alreadyNormalized
= Environment.NewLine.Equals("\n", StringComparison.OrdinalIgnoreCase);
public static int CellLength(this string text, RenderContext context)
{
if (context is null)
{
throw new ArgumentNullException(nameof(context));
}
return Cell.GetCellLength(context, text);
}
public static string Capitalize(this string text, CultureInfo? culture = null)
{
if (text == null)
{
return string.Empty;
}
culture ??= CultureInfo.InvariantCulture;
if (text.Length > 0 && char.IsLower(text[0]))
{
text = string.Format(culture, "{0}{1}", char.ToUpper(text[0], culture), text.Substring(1));
}
return text;
}
public static string NormalizeLineEndings(this string text, bool native = false)
{
text ??= string.Empty;
var normalized = text?.Replace("\r\n", "\n")?.Replace("\r", string.Empty) ?? string.Empty;
if (native && !_alreadyNormalized)
{
normalized = normalized.Replace("\n", Environment.NewLine);
}
return normalized;
}
public static string[] SplitLines(this string text)
{
var result = text?.NormalizeLineEndings()?.Split(new[] { '\n' }, StringSplitOptions.None);
return result ?? Array.Empty<string>();
}
public static string[] SplitWords(this string word, StringSplitOptions options = StringSplitOptions.None)
{
var result = new List<string>();
static string Read(StringBuffer reader, Func<char, bool> criteria)
{
var buffer = new StringBuilder();
while (!reader.Eof)
{
var current = reader.Peek();
if (!criteria(current))
{
break;
}
buffer.Append(reader.Read());
}
return buffer.ToString();
}
using (var reader = new StringBuffer(word))
{
while (!reader.Eof)
{
var current = reader.Peek();
if (char.IsWhiteSpace(current))
{
var x = Read(reader, c => char.IsWhiteSpace(c));
if (options != StringSplitOptions.RemoveEmptyEntries)
{
result.Add(x);
}
}
else
{
result.Add(Read(reader, c => !char.IsWhiteSpace(c)));
}
}
}
return result.ToArray();
}
public static string Repeat(this string text, int count)
{
if (text is null)
{
throw new ArgumentNullException(nameof(text));
}
if (count <= 0)
{
return string.Empty;
}
if (count == 1)
{
return text;
}
return string.Concat(Enumerable.Repeat(text, count));
}
}
}

View File

@@ -1,29 +0,0 @@
using System;
using System.Collections.Generic;
namespace Spectre.Console.Internal
{
internal static class StyleExtensions
{
public static Style Combine(this Style style, IEnumerable<Style> source)
{
if (style is null)
{
throw new ArgumentNullException(nameof(style));
}
if (source is null)
{
return style;
}
var current = style;
foreach (var item in source)
{
current = current.Combine(item);
}
return current;
}
}
}

View File

@@ -1,97 +0,0 @@
using System;
using System.IO;
using System.Text;
using Spectre.Console.Rendering;
namespace Spectre.Console.Internal
{
internal sealed class FallbackConsoleRenderer : IAnsiConsole
{
private readonly TextWriter _out;
private readonly ColorSystem _system;
private Style? _lastStyle;
public Capabilities Capabilities { get; }
public Encoding Encoding { get; }
public int Width
{
get
{
if (_out.IsStandardOut())
{
return ConsoleHelper.GetSafeBufferWidth(Constants.DefaultBufferWidth);
}
return Constants.DefaultBufferWidth;
}
}
public int Height
{
get
{
if (_out.IsStandardOut())
{
return ConsoleHelper.GetSafeBufferHeight(Constants.DefaultBufferHeight);
}
return Constants.DefaultBufferHeight;
}
}
public FallbackConsoleRenderer(TextWriter @out, Capabilities capabilities)
{
if (capabilities == null)
{
throw new ArgumentNullException(nameof(capabilities));
}
_out = @out ?? throw new ArgumentNullException(nameof(@out));
_system = capabilities.ColorSystem;
if (_out.IsStandardOut())
{
Encoding = System.Console.OutputEncoding;
}
else
{
Encoding = Encoding.UTF8;
}
Capabilities = capabilities;
}
public void Write(Segment segment)
{
if (_lastStyle?.Equals(segment.Style) != true)
{
SetStyle(segment.Style);
}
_out.Write(segment.Text.NormalizeLineEndings(native: true));
}
private void SetStyle(Style style)
{
_lastStyle = style;
if (_out.IsStandardOut())
{
System.Console.ResetColor();
var background = Color.ToConsoleColor(style.Background);
if (_system != ColorSystem.NoColors && _out.IsStandardOut() && (int)background != -1)
{
System.Console.BackgroundColor = background;
}
var foreground = Color.ToConsoleColor(style.Foreground);
if (_system != ColorSystem.NoColors && _out.IsStandardOut() && (int)foreground != -1)
{
System.Console.ForegroundColor = foreground;
}
}
}
}
}

View File

@@ -1,4 +1,5 @@
using System;
using System.Runtime.CompilerServices;
namespace Spectre.Console.Internal
{
@@ -23,9 +24,19 @@ namespace Spectre.Console.Internal
unchecked
{
return Math.Abs(
link.GetHashCode() +
GetLinkHashCode(link) +
_random.Next(0, int.MaxValue));
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static int GetLinkHashCode(string link)
{
#if NET5_0
return link.GetHashCode(StringComparison.Ordinal);
#else
return link.GetHashCode();
#endif
}
}
}

View File

@@ -147,7 +147,7 @@ namespace Spectre.Console.Internal
error = null;
hex ??= string.Empty;
hex = hex.Replace("#", string.Empty).Trim();
hex = hex.ReplaceExact("#", string.Empty).Trim();
try
{

View File

@@ -19,6 +19,9 @@ namespace Spectre.Console
/// <inheritdoc/>
public Encoding Encoding => _console.Encoding;
/// <inheritdoc/>
public IAnsiConsoleCursor Cursor => _console.Cursor;
/// <inheritdoc/>
public int Width => _console.Width;
@@ -41,10 +44,26 @@ namespace Spectre.Console
// Only used for scoping.
}
/// <inheritdoc/>
public void Clear(bool home)
{
_console.Clear(home);
}
/// <inheritdoc/>
public void Write(Segment segment)
{
_recorded.Add(segment);
if (segment is null)
{
throw new ArgumentNullException(nameof(segment));
}
// Don't record control codes.
if (!segment.IsControlCode)
{
_recorded.Add(segment);
}
_console.Write(segment);
}

View File

@@ -63,10 +63,10 @@ namespace Spectre.Console.Rendering
{
var padding = columns[columnIndex].Padding;
if (padding.Left > 0)
if (padding != null && padding.Value.Left > 0)
{
// Left padding
builder.Append(" ".Repeat(padding.Left));
builder.Append(" ".Repeat(padding.Value.Left));
}
var justification = columns[columnIndex].Alignment;
@@ -96,9 +96,9 @@ namespace Spectre.Console.Rendering
}
// Right padding
if (padding.Right > 0)
if (padding != null && padding.Value.Right > 0)
{
builder.Append(" ".Repeat(padding.Right));
builder.Append(" ".Repeat(padding.Value.Right));
}
if (!lastColumn)

View File

@@ -0,0 +1,13 @@
namespace Spectre.Console.Rendering
{
/// <summary>
/// Represents something that can be dirty.
/// </summary>
public interface IHasDirtyState
{
/// <summary>
/// Gets a value indicating whether the object is dirty.
/// </summary>
bool IsDirty { get; }
}
}

View File

@@ -0,0 +1,79 @@
using System;
using System.Collections.Generic;
namespace Spectre.Console.Rendering
{
/// <summary>
/// Represents something renderable that's reconstructed
/// when it's state change in any way.
/// </summary>
public abstract class JustInTimeRenderable : Renderable
{
private bool _dirty;
private IRenderable? _rendered;
/// <inheritdoc/>
protected sealed override Measurement Measure(RenderContext context, int maxWidth)
{
return GetInner().Measure(context, maxWidth);
}
/// <inheritdoc/>
protected sealed override IEnumerable<Segment> Render(RenderContext context, int width)
{
return GetInner().Render(context, width);
}
/// <summary>
/// Builds the inner renderable.
/// </summary>
/// <returns>A new inner renderable.</returns>
protected abstract IRenderable Build();
/// <summary>
/// Checks if there are any children that has changed.
/// If so, the underlying renderable needs rebuilding.
/// </summary>
/// <returns><c>true</c> if the object needs rebuilding, otherwise <c>false</c>.</returns>
protected virtual bool HasDirtyChildren()
{
return false;
}
/// <summary>
/// Marks this instance as dirty.
/// </summary>
protected void MarkAsDirty()
{
_dirty = true;
}
/// <summary>
/// Marks this instance as dirty.
/// </summary>
/// <param name="action">
/// The action to execute before marking the instance as dirty.
/// </param>
protected void MarkAsDirty(Action action)
{
if (action is null)
{
throw new ArgumentNullException(nameof(action));
}
action();
_dirty = true;
}
private IRenderable GetInner()
{
if (_dirty || HasDirtyChildren() || _rendered == null)
{
_rendered = Build();
_dirty = false;
}
return _rendered;
}
}
}

View File

@@ -31,6 +31,12 @@ namespace Spectre.Console.Rendering
/// </summary>
public bool IsWhiteSpace { get; }
/// <summary>
/// Gets a value indicating whether or not his is a
/// control code such as cursor movement.
/// </summary>
public bool IsControlCode { get; }
/// <summary>
/// Gets the segment style.
/// </summary>
@@ -39,12 +45,12 @@ namespace Spectre.Console.Rendering
/// <summary>
/// Gets a segment representing a line break.
/// </summary>
public static Segment LineBreak { get; } = new Segment(Environment.NewLine, Style.Plain, true);
public static Segment LineBreak { get; } = new Segment(Environment.NewLine, Style.Plain, true, false);
/// <summary>
/// Gets an empty segment.
/// </summary>
public static Segment Empty { get; } = new Segment(string.Empty, Style.Plain, false);
public static Segment Empty { get; } = new Segment(string.Empty, Style.Plain, false, false);
/// <summary>
/// Initializes a new instance of the <see cref="Segment"/> class.
@@ -61,16 +67,27 @@ namespace Spectre.Console.Rendering
/// <param name="text">The segment text.</param>
/// <param name="style">The segment style.</param>
public Segment(string text, Style style)
: this(text, style, false)
: this(text, style, false, false)
{
}
private Segment(string text, Style style, bool lineBreak)
private Segment(string text, Style style, bool lineBreak, bool control)
{
Text = text?.NormalizeLineEndings() ?? throw new ArgumentNullException(nameof(text));
Style = style ?? throw new ArgumentNullException(nameof(style));
IsLineBreak = lineBreak;
IsWhiteSpace = string.IsNullOrWhiteSpace(text);
IsControlCode = control;
}
/// <summary>
/// Creates a control segment.
/// </summary>
/// <param name="control">The control code.</param>
/// <returns>A segment representing a control code.</returns>
public static Segment Control(string control)
{
return new Segment(control, Style.Plain, false, true);
}
/// <summary>
@@ -220,7 +237,7 @@ namespace Spectre.Console.Rendering
}
// Does the segment contain a newline?
if (segment.Text.Contains("\n"))
if (segment.Text.ContainsExact("\n"))
{
// Is it a new line?
if (segment.Text == "\n")

View File

@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<TargetFrameworks>net5.0;netstandard2.0</TargetFrameworks>
<Nullable>enable</Nullable>
</PropertyGroup>

View File

@@ -180,6 +180,15 @@ namespace Spectre.Console
/// <inheritdoc/>
public override int GetHashCode()
{
int? GetLinkHashCode()
{
#if NET5_0
return Link?.GetHashCode(StringComparison.Ordinal);
#else
return Link?.GetHashCode();
#endif
}
unchecked
{
var hash = (int)2166136261;
@@ -189,7 +198,7 @@ namespace Spectre.Console
if (Link != null)
{
hash = (hash * 16777619) ^ Link?.GetHashCode() ?? 0;
hash = (hash * 16777619) ^ GetLinkHashCode() ?? 0;
}
return hash;

View File

@@ -55,7 +55,7 @@ namespace Spectre.Console
foreach (var (columnIndex, _, lastColumn, columnWidth) in widths.Enumerate())
{
var padding = columns[columnIndex].Padding;
var centerWidth = padding.Left + columnWidth + padding.Right;
var centerWidth = padding.GetLeftSafe() + columnWidth + padding.GetRightSafe();
builder.Append(center.Repeat(centerWidth));
if (!lastColumn)

View File

@@ -3,7 +3,6 @@ using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using Spectre.Console.Internal;
using Spectre.Console.Internal.Collections;
using Spectre.Console.Rendering;
namespace Spectre.Console
@@ -11,7 +10,7 @@ namespace Spectre.Console
/// <summary>
/// A renderable calendar.
/// </summary>
public sealed class Calendar : Renderable, IHasCulture, IHasTableBorder, IAlignable
public sealed class Calendar : JustInTimeRenderable, IHasCulture, IHasTableBorder, IAlignable
{
private const int NumberOfWeekDays = 7;
private const int ExpectedRowCount = 6;
@@ -21,11 +20,9 @@ namespace Spectre.Console
private int _year;
private int _month;
private int _day;
private IRenderable? _table;
private TableBorder _border;
private bool _useSafeBorder;
private Style? _borderStyle;
private bool _dirty;
private CultureInfo _culture;
private Style _highlightStyle;
private bool _showHeader;
@@ -158,43 +155,17 @@ namespace Spectre.Console
_year = year;
_month = month;
_day = day;
_table = null;
_border = TableBorder.Square;
_useSafeBorder = true;
_borderStyle = null;
_dirty = true;
_culture = CultureInfo.InvariantCulture;
_highlightStyle = new Style(foreground: Color.Blue);
_showHeader = true;
_calendarEvents = new ListWithCallback<CalendarEvent>(() => _dirty = true);
_calendarEvents = new ListWithCallback<CalendarEvent>(() => MarkAsDirty());
}
/// <inheritdoc/>
protected override Measurement Measure(RenderContext context, int maxWidth)
{
var table = GetTable();
return table.Measure(context, maxWidth);
}
/// <inheritdoc/>
protected override IEnumerable<Segment> Render(RenderContext context, int maxWidth)
{
return GetTable().Render(context, maxWidth);
}
private IRenderable GetTable()
{
// Table needs to be built?
if (_dirty || _table == null)
{
_table = BuildTable();
_dirty = false;
}
return _table;
}
private IRenderable BuildTable()
protected override IRenderable Build()
{
var culture = Culture ?? CultureInfo.InvariantCulture;
@@ -264,9 +235,9 @@ namespace Spectre.Console
}
// We want all calendars to have the same height.
if (table.RowCount < ExpectedRowCount)
if (table.Rows.Count < ExpectedRowCount)
{
var diff = Math.Max(0, ExpectedRowCount - table.RowCount);
var diff = Math.Max(0, ExpectedRowCount - table.Rows.Count);
for (var i = 0; i < diff; i++)
{
table.AddEmptyRow();
@@ -276,12 +247,6 @@ namespace Spectre.Console
return table;
}
private void MarkAsDirty(Action action)
{
action();
_dirty = true;
}
private DayOfWeek[] GetWeekdays()
{
var culture = Culture ?? CultureInfo.InvariantCulture;

View File

@@ -13,7 +13,7 @@ namespace Spectre.Console
private readonly List<IRenderable> _items;
/// <inheritdoc/>
public Padding Padding { get; set; } = new Padding(0, 0, 1, 0);
public Padding? Padding { get; set; } = new Padding(0, 0, 1, 0);
/// <summary>
/// Gets or sets a value indicating whether or not the columns should
@@ -62,7 +62,7 @@ namespace Spectre.Console
/// <inheritdoc/>
protected override Measurement Measure(RenderContext context, int maxWidth)
{
var maxPadding = Math.Max(Padding.Left, Padding.Right);
var maxPadding = Math.Max(Padding.GetLeftSafe(), Padding.GetRightSafe());
var itemWidths = _items.Select(item => item.Measure(context, maxWidth).Max).ToArray();
var columnCount = CalculateColumnCount(maxWidth, itemWidths, _items.Count, maxPadding);
@@ -90,7 +90,7 @@ namespace Spectre.Console
/// <inheritdoc/>
protected override IEnumerable<Segment> Render(RenderContext context, int maxWidth)
{
var maxPadding = Math.Max(Padding.Left, Padding.Right);
var maxPadding = Math.Max(Padding.GetLeftSafe(), Padding.GetRightSafe());
var itemWidths = _items.Select(item => item.Measure(context, maxWidth).Max).ToArray();
var columnCount = CalculateColumnCount(maxWidth, itemWidths, _items.Count, maxPadding);

View File

@@ -1,5 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Spectre.Console.Internal;
using Spectre.Console.Rendering;
namespace Spectre.Console
@@ -7,32 +9,37 @@ namespace Spectre.Console
/// <summary>
/// A renderable grid.
/// </summary>
public sealed class Grid : Renderable, IExpandable, IAlignable
public sealed class Grid : JustInTimeRenderable, IExpandable, IAlignable
{
private readonly Table _table;
private readonly ListWithCallback<GridColumn> _columns;
private readonly ListWithCallback<GridRow> _rows;
private bool _expand;
private Justify? _alignment;
private bool _padRightCell;
/// <summary>
/// Gets the number of columns in the table.
/// Gets the grid columns.
/// </summary>
public int ColumnCount => _table.ColumnCount;
public IReadOnlyList<GridColumn> Columns => _columns;
/// <summary>
/// Gets the number of rows in the table.
/// Gets the grid rows.
/// </summary>
public int RowCount => _table.RowCount;
public IReadOnlyList<GridRow> Rows => _rows;
/// <inheritdoc/>
public bool Expand
{
get => _table.Expand;
set => _table.Expand = value;
get => _expand;
set => MarkAsDirty(() => _expand = value);
}
/// <inheritdoc/>
public Justify? Alignment
{
get => _table.Alignment;
set => _table.Alignment = value;
get => _alignment;
set => MarkAsDirty(() => _alignment = value);
}
/// <summary>
@@ -40,25 +47,10 @@ namespace Spectre.Console
/// </summary>
public Grid()
{
_table = new Table
{
Border = TableBorder.None,
ShowHeaders = false,
IsGrid = true,
PadRightCell = false,
};
}
/// <inheritdoc/>
protected override Measurement Measure(RenderContext context, int maxWidth)
{
return ((IRenderable)_table).Measure(context, maxWidth);
}
/// <inheritdoc/>
protected override IEnumerable<Segment> Render(RenderContext context, int width)
{
return ((IRenderable)_table).Render(context, width);
_expand = false;
_alignment = null;
_columns = new ListWithCallback<GridColumn>(() => MarkAsDirty());
_rows = new ListWithCallback<GridRow>(() => MarkAsDirty());
}
/// <summary>
@@ -83,21 +75,15 @@ namespace Spectre.Console
throw new ArgumentNullException(nameof(column));
}
if (_table.RowCount > 0)
if (_rows.Count > 0)
{
throw new InvalidOperationException("Cannot add new columns to grid with existing rows.");
}
// Only pad the most right cell if we've explicitly set a padding.
_table.PadRightCell = column.HasExplicitPadding;
_padRightCell = column.HasExplicitPadding;
_table.AddColumn(new TableColumn(string.Empty)
{
Width = column.Width,
NoWrap = column.NoWrap,
Padding = column.Padding,
Alignment = column.Alignment,
});
_columns.Add(column);
return this;
}
@@ -114,13 +100,49 @@ namespace Spectre.Console
throw new ArgumentNullException(nameof(columns));
}
if (columns.Length > _table.ColumnCount)
if (columns.Length > _columns.Count)
{
throw new InvalidOperationException("The number of row columns are greater than the number of grid columns.");
}
_table.AddRow(columns);
_rows.Add(new GridRow(columns));
return this;
}
/// <inheritdoc/>
protected override bool HasDirtyChildren()
{
return _columns.Any(c => ((IHasDirtyState)c).IsDirty);
}
/// <inheritdoc/>
protected override IRenderable Build()
{
var table = new Table
{
Border = TableBorder.None,
ShowHeaders = false,
IsGrid = true,
PadRightCell = _padRightCell,
};
foreach (var column in _columns)
{
table.AddColumn(new TableColumn(string.Empty)
{
Width = column.Width,
NoWrap = column.NoWrap,
Padding = column.Padding ?? new Padding(0, 0, 2, 0),
Alignment = column.Alignment,
});
}
foreach (var row in _rows)
{
table.AddRow(row);
}
return table;
}
}
}

View File

@@ -1,55 +1,71 @@
using System;
using Spectre.Console.Rendering;
namespace Spectre.Console
{
/// <summary>
/// Represents a grid column.
/// </summary>
public sealed class GridColumn : IColumn
public sealed class GridColumn : IColumn, IHasDirtyState
{
private Padding _padding;
private bool _isDirty;
private int? _width;
private bool _noWrap;
private Padding? _padding;
private Justify? _alignment;
/// <inheritdoc/>
bool IHasDirtyState.IsDirty => _isDirty;
/// <summary>
/// Gets or sets the width of the column.
/// If <c>null</c>, the column will adapt to it's contents.
/// </summary>
public int? Width { get; set; }
public int? Width
{
get => _width;
set => MarkAsDirty(() => _width = value);
}
/// <summary>
/// Gets or sets a value indicating whether wrapping of
/// text within the column should be prevented.
/// </summary>
public bool NoWrap { get; set; }
public bool NoWrap
{
get => _noWrap;
set => MarkAsDirty(() => _noWrap = value);
}
/// <summary>
/// Gets or sets the padding of the column.
/// Vertical padding (top and bottom) is ignored.
/// </summary>
public Padding Padding
public Padding? Padding
{
get => _padding;
set
{
HasExplicitPadding = true;
_padding = value;
}
set => MarkAsDirty(() => _padding = value);
}
/// <summary>
/// Gets or sets the alignment of the column.
/// </summary>
public Justify? Alignment { get; set; }
public Justify? Alignment
{
get => _alignment;
set => MarkAsDirty(() => _alignment = value);
}
/// <summary>
/// Gets a value indicating whether the user
/// has set an explicit padding for this column.
/// </summary>
internal bool HasExplicitPadding { get; private set; }
internal bool HasExplicitPadding => Padding != null;
/// <summary>
/// Initializes a new instance of the <see cref="GridColumn"/> class.
/// </summary>
public GridColumn()
private void MarkAsDirty(Action action)
{
_padding = new Padding(0, 0, 2, 0);
action();
_isDirty = true;
}
}
}

View File

@@ -0,0 +1,56 @@
using System;
using System.Collections;
using System.Collections.Generic;
using Spectre.Console.Rendering;
namespace Spectre.Console
{
/// <summary>
/// Represents a grid row.
/// </summary>
public sealed class GridRow : IEnumerable<IRenderable>
{
private readonly List<IRenderable> _items;
/// <summary>
/// Gets a row item at the specified grid column index.
/// </summary>
/// <param name="index">The grid column index.</param>
/// <returns>The row item at the specified grid column index.</returns>
public IRenderable this[int index]
{
get => _items[index];
}
/// <summary>
/// Initializes a new instance of the <see cref="GridRow"/> class.
/// </summary>
/// <param name="items">The row items.</param>
public GridRow(IEnumerable<IRenderable> items)
{
_items = new List<IRenderable>(items ?? Array.Empty<IRenderable>());
}
internal void Add(IRenderable item)
{
if (item is null)
{
throw new ArgumentNullException(nameof(item));
}
_items.Add(item);
}
/// <inheritdoc/>
public IEnumerator<IRenderable> GetEnumerator()
{
return _items.GetEnumerator();
}
/// <inheritdoc/>
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
}

View File

@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using Spectre.Console.Internal;
using Spectre.Console.Rendering;
@@ -8,6 +9,7 @@ namespace Spectre.Console
/// <summary>
/// A renderable piece of markup text.
/// </summary>
[SuppressMessage("Naming", "CA1724:Type names should not match namespaces")]
public sealed class Markup : Renderable, IAlignable, IOverflowable
{
private readonly Paragraph _paragraph;

View File

@@ -12,7 +12,7 @@ namespace Spectre.Console
private readonly IRenderable _child;
/// <inheritdoc/>
public Padding Padding { get; set; } = new Padding(1, 1, 1, 1);
public Padding? Padding { get; set; } = new Padding(1, 1, 1, 1);
/// <summary>
/// Gets or sets a value indicating whether or not the padding should
@@ -35,7 +35,7 @@ namespace Spectre.Console
/// <inheritdoc/>
protected override Measurement Measure(RenderContext context, int maxWidth)
{
var paddingWidth = Padding.GetWidth();
var paddingWidth = Padding?.GetWidth() ?? 0;
var measurement = _child.Measure(context, maxWidth - paddingWidth);
return new Measurement(
@@ -46,7 +46,7 @@ namespace Spectre.Console
/// <inheritdoc/>
protected override IEnumerable<Segment> Render(RenderContext context, int maxWidth)
{
var paddingWidth = Padding.GetWidth();
var paddingWidth = Padding?.GetWidth() ?? 0;
var childWidth = maxWidth - paddingWidth;
if (!Expand)
@@ -59,7 +59,7 @@ namespace Spectre.Console
var result = new List<Segment>();
// Top padding
for (var i = 0; i < Padding.Top; i++)
for (var i = 0; i < Padding.GetTopSafe(); i++)
{
result.Add(new Segment(new string(' ', width)));
result.Add(Segment.LineBreak);
@@ -69,22 +69,22 @@ namespace Spectre.Console
foreach (var (_, _, _, line) in Segment.SplitLines(context, child).Enumerate())
{
// Left padding
if (Padding.Left != 0)
if (Padding.GetLeftSafe() != 0)
{
result.Add(new Segment(new string(' ', Padding.Left)));
result.Add(new Segment(new string(' ', Padding.GetLeftSafe())));
}
result.AddRange(line);
// Right padding
if (Padding.Right != 0)
if (Padding.GetRightSafe() != 0)
{
result.Add(new Segment(new string(' ', Padding.Right)));
result.Add(new Segment(new string(' ', Padding.GetRightSafe())));
}
// Missing space on right side?
var lineWidth = line.CellCount(context);
var diff = width - lineWidth - Padding.Left - Padding.Right;
var diff = width - lineWidth - Padding.GetLeftSafe() - Padding.GetRightSafe();
if (diff > 0)
{
result.Add(new Segment(new string(' ', diff)));
@@ -94,7 +94,7 @@ namespace Spectre.Console
}
// Bottom padding
for (var i = 0; i < Padding.Bottom; i++)
for (var i = 0; i < Padding.GetBottomSafe(); i++)
{
result.Add(new Segment(new string(' ', width)));
result.Add(Segment.LineBreak);

View File

@@ -9,7 +9,7 @@ namespace Spectre.Console
/// <summary>
/// A renderable panel.
/// </summary>
public sealed class Panel : Renderable, IHasBoxBorder, IExpandable, IPaddable
public sealed class Panel : Renderable, IHasBoxBorder, IHasBorder, IExpandable, IPaddable
{
private const int EdgeWidth = 2;
@@ -34,7 +34,7 @@ namespace Spectre.Console
/// <summary>
/// Gets or sets the padding.
/// </summary>
public Padding Padding { get; set; } = new Padding(1, 0, 1, 0);
public Padding? Padding { get; set; } = new Padding(1, 0, 1, 0);
/// <summary>
/// Gets or sets the header.
@@ -123,62 +123,35 @@ namespace Spectre.Console
}
// Panel bottom
AddBottomBorder(result, border, borderStyle, panelWidth);
return result;
}
private static void AddBottomBorder(List<Segment> result, BoxBorder border, Style borderStyle, int panelWidth)
{
result.Add(new Segment(border.GetPart(BoxBorderPart.BottomLeft), borderStyle));
result.Add(new Segment(border.GetPart(BoxBorderPart.Bottom).Repeat(panelWidth - EdgeWidth), borderStyle));
result.Add(new Segment(border.GetPart(BoxBorderPart.BottomRight), borderStyle));
result.Add(Segment.LineBreak);
return result;
}
private void AddTopBorder(List<Segment> segments, RenderContext context, BoxBorder border, Style borderStyle, int panelWidth)
private void AddTopBorder(List<Segment> result, RenderContext context, BoxBorder border, Style borderStyle, int panelWidth)
{
segments.Add(new Segment(border.GetPart(BoxBorderPart.TopLeft), borderStyle));
if (Header != null)
var rule = new Rule
{
var leftSpacing = 0;
var rightSpacing = 0;
Style = borderStyle,
Border = border,
TitlePadding = 1,
TitleSpacing = 0,
Title = Header?.Text,
Alignment = Header?.Alignment ?? Justify.Left,
};
var headerWidth = panelWidth - (EdgeWidth * 2);
var header = Segment.TruncateWithEllipsis(Header.Text, Header.Style ?? borderStyle, context, headerWidth);
// Top left border
result.Add(new Segment(border.GetPart(BoxBorderPart.TopLeft), borderStyle));
var excessWidth = headerWidth - header.CellCount(context);
if (excessWidth > 0)
{
switch (Header.Alignment ?? Justify.Left)
{
case Justify.Left:
leftSpacing = 0;
rightSpacing = excessWidth;
break;
case Justify.Right:
leftSpacing = excessWidth;
rightSpacing = 0;
break;
case Justify.Center:
leftSpacing = excessWidth / 2;
rightSpacing = (excessWidth / 2) + (excessWidth % 2);
break;
}
}
// Top border (and header text if specified)
result.AddRange(((IRenderable)rule).Render(context, panelWidth - 2).Where(x => !x.IsLineBreak));
segments.Add(new Segment(border.GetPart(BoxBorderPart.Top).Repeat(leftSpacing + 1), borderStyle));
segments.Add(header);
segments.Add(new Segment(border.GetPart(BoxBorderPart.Top).Repeat(rightSpacing + 1), borderStyle));
}
else
{
segments.Add(new Segment(border.GetPart(BoxBorderPart.Top).Repeat(panelWidth - EdgeWidth), borderStyle));
}
segments.Add(new Segment(border.GetPart(BoxBorderPart.TopRight), borderStyle));
segments.Add(Segment.LineBreak);
// Top right border
result.Add(new Segment(border.GetPart(BoxBorderPart.TopRight), borderStyle));
result.Add(Segment.LineBreak);
}
}
}

View File

@@ -1,4 +1,5 @@
using System;
using System.ComponentModel;
namespace Spectre.Console
{
@@ -12,11 +13,6 @@ namespace Spectre.Console
/// </summary>
public string Text { get; }
/// <summary>
/// Gets or sets the panel header style.
/// </summary>
public Style? Style { get; set; }
/// <summary>
/// Gets or sets the panel header alignment.
/// </summary>
@@ -26,12 +22,10 @@ namespace Spectre.Console
/// Initializes a new instance of the <see cref="PanelHeader"/> class.
/// </summary>
/// <param name="text">The panel header text.</param>
/// <param name="style">The panel header style.</param>
/// <param name="alignment">The panel header alignment.</param>
public PanelHeader(string text, Style? style = null, Justify? alignment = null)
public PanelHeader(string text, Justify? alignment = null)
{
Text = text ?? throw new ArgumentNullException(nameof(text));
Style = style;
Alignment = alignment;
}
@@ -40,9 +34,10 @@ namespace Spectre.Console
/// </summary>
/// <param name="style">The panel header style.</param>
/// <returns>The same instance so that multiple calls can be chained.</returns>
[Obsolete("Use markup instead.")]
[EditorBrowsable(EditorBrowsableState.Never)]
public PanelHeader SetStyle(Style? style)
{
Style = style ?? Style.Plain;
return this;
}
@@ -51,14 +46,10 @@ namespace Spectre.Console
/// </summary>
/// <param name="style">The panel header style.</param>
/// <returns>The same instance so that multiple calls can be chained.</returns>
[Obsolete("Use markup instead.")]
[EditorBrowsable(EditorBrowsableState.Never)]
public PanelHeader SetStyle(string style)
{
if (style is null)
{
throw new ArgumentNullException(nameof(style));
}
Style = Style.Parse(style);
return this;
}

Some files were not shown because too many files have changed in this diff Show More