mirror of
https://github.com/nsnail/spectre.console.git
synced 2025-04-16 00:42:51 +08:00
Add support for alignment
This commit is contained in:
parent
5e41a2f505
commit
eb4a7d3bf4
@ -4,7 +4,7 @@
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
<ExampleTitle>Paths</ExampleTitle>
|
||||
<ExampleDescription>Demonstrates how to write paths.</ExampleDescription>
|
||||
<ExampleDescription>Demonstrates how to render paths.</ExampleDescription>
|
||||
<ExampleGroup>Widgets</ExampleGroup>
|
||||
</PropertyGroup>
|
||||
|
||||
|
@ -2,7 +2,7 @@ using System;
|
||||
using System.Threading;
|
||||
using Spectre.Console;
|
||||
|
||||
namespace Live;
|
||||
namespace Paths;
|
||||
|
||||
public static class Program
|
||||
{
|
||||
@ -11,11 +11,37 @@ public static class Program
|
||||
var windowsPath = @"C:\This is\A\Super Long\Windows\Path\That\Goes\On And On\And\Never\Seems\To\Stop\But\At\Some\Point\It\Must\I\Guess.txt";
|
||||
var unixPath = @"//This is/A/Super Long/Unix/Path/That/Goes/On And On/And/Never/Seems/To/Stop/But/At/Some/Point/It/Must/I/Guess.txt";
|
||||
|
||||
var table = new Table().BorderColor(Color.Grey);
|
||||
AnsiConsole.WriteLine();
|
||||
WritePlain(windowsPath, unixPath);
|
||||
|
||||
AnsiConsole.WriteLine();
|
||||
WriteColorized(windowsPath, unixPath);
|
||||
|
||||
AnsiConsole.WriteLine();
|
||||
WriteAligned(windowsPath);
|
||||
}
|
||||
|
||||
private static void WritePlain(string windowsPath, string unixPath)
|
||||
{
|
||||
var table = new Table().BorderColor(Color.Grey).Title("Plain").RoundedBorder();
|
||||
table.AddColumns("[grey]OS[/]", "[grey]Path[/]");
|
||||
table.AddRow(new Text("Windows"), new TextPath(windowsPath));
|
||||
table.AddRow(new Text("Unix"), new TextPath(unixPath));
|
||||
|
||||
AnsiConsole.Write(table);
|
||||
}
|
||||
|
||||
private static void WriteColorized(string windowsPath, string unixPath)
|
||||
{
|
||||
var table = new Table().BorderColor(Color.Grey).Title("Colorized").RoundedBorder();
|
||||
table.AddColumns("[grey]OS[/]", "[grey]Path[/]");
|
||||
|
||||
table.AddRow(new Text("Windows"),
|
||||
new TextPath(windowsPath));
|
||||
new TextPath(windowsPath)
|
||||
.RootColor(Color.Blue)
|
||||
.SeparatorColor(Color.Yellow)
|
||||
.StemStyle(Color.Red)
|
||||
.LeafStyle(Color.Green));
|
||||
|
||||
table.AddRow(new Text("Unix"),
|
||||
new TextPath(unixPath)
|
||||
@ -26,4 +52,16 @@ public static class Program
|
||||
|
||||
AnsiConsole.Write(table);
|
||||
}
|
||||
|
||||
private static void WriteAligned(string path)
|
||||
{
|
||||
var table = new Table().BorderColor(Color.Grey).Title("Aligned").RoundedBorder();
|
||||
table.AddColumns("[grey]Alignment[/]", "[grey]Path[/]");
|
||||
|
||||
table.AddRow(new Text("Left"), new TextPath(path).LeftAligned());
|
||||
table.AddRow(new Text("Center"), new TextPath(path).Centered());
|
||||
table.AddRow(new Text("Right"), new TextPath(path).RightAligned());
|
||||
|
||||
AnsiConsole.Write(table);
|
||||
}
|
||||
}
|
||||
|
@ -1,7 +0,0 @@
|
||||
<SolutionConfiguration>
|
||||
<Settings>
|
||||
<AllowParallelTestExecution>True</AllowParallelTestExecution>
|
||||
<InstrumentationMode>Optimised</InstrumentationMode>
|
||||
<SolutionConfigured>True</SolutionConfigured>
|
||||
</Settings>
|
||||
</SolutionConfiguration>
|
@ -3,7 +3,7 @@ namespace Spectre.Console;
|
||||
/// <summary>
|
||||
/// Representation of a file system path.
|
||||
/// </summary>
|
||||
public sealed class TextPath : IRenderable
|
||||
public sealed class TextPath : IRenderable, IAlignable
|
||||
{
|
||||
private const string Ellipsis = "...";
|
||||
private const string UnicodeEllipsis = "…";
|
||||
@ -32,6 +32,11 @@ public sealed class TextPath : IRenderable
|
||||
/// </summary>
|
||||
public Style? LeafStyle { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the alignment.
|
||||
/// </summary>
|
||||
public Justify? Alignment { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="TextPath"/> class.
|
||||
/// </summary>
|
||||
@ -75,6 +80,8 @@ public sealed class TextPath : IRenderable
|
||||
/// <inheritdoc/>
|
||||
public IEnumerable<Segment> Render(RenderContext context, int maxWidth)
|
||||
{
|
||||
var alignment = Alignment ?? Justify.Left;
|
||||
|
||||
var rootStyle = RootStyle ?? Style.Plain;
|
||||
var separatorStyle = SeparatorStyle ?? Style.Plain;
|
||||
var stemStyle = StemStyle ?? Style.Plain;
|
||||
@ -111,6 +118,10 @@ public sealed class TextPath : IRenderable
|
||||
}
|
||||
}
|
||||
|
||||
// Align the result
|
||||
Aligner.Align(parts, Alignment, maxWidth);
|
||||
|
||||
// Insert a line break
|
||||
parts.Add(Segment.LineBreak);
|
||||
|
||||
return parts;
|
||||
|
@ -1,7 +0,0 @@
|
||||
<ProjectConfiguration>
|
||||
<Settings>
|
||||
<IgnoredTests>
|
||||
<AllTestsSelector />
|
||||
</IgnoredTests>
|
||||
</Settings>
|
||||
</ProjectConfiguration>
|
@ -1,7 +0,0 @@
|
||||
<ProjectConfiguration>
|
||||
<Settings>
|
||||
<IgnoredTests>
|
||||
<AllTestsSelector />
|
||||
</IgnoredTests>
|
||||
</Settings>
|
||||
</ProjectConfiguration>
|
@ -1,3 +0,0 @@
|
||||
<ProjectConfiguration>
|
||||
<Settings />
|
||||
</ProjectConfiguration>
|
@ -14,7 +14,8 @@ public sealed class TextPathTests
|
||||
console.Write(new TextPath(input));
|
||||
|
||||
// Then
|
||||
console.Output.TrimEnd().ShouldBe(expected);
|
||||
console.Output.TrimEnd()
|
||||
.ShouldBe(expected);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
@ -30,7 +31,8 @@ public sealed class TextPathTests
|
||||
console.Write(new TextPath(input));
|
||||
|
||||
// Then
|
||||
console.Output.TrimEnd().ShouldBe(expected);
|
||||
console.Output.TrimEnd()
|
||||
.ShouldBe(expected);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
@ -46,7 +48,8 @@ public sealed class TextPathTests
|
||||
console.Write(new TextPath(input));
|
||||
|
||||
// Then
|
||||
console.Output.TrimEnd().ShouldBe(expected);
|
||||
console.Output.TrimEnd()
|
||||
.ShouldBe(expected);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
@ -66,4 +69,32 @@ public sealed class TextPathTests
|
||||
// Then
|
||||
console.Output.ShouldEndWith("\n");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Should_Right_Align_Correctly()
|
||||
{
|
||||
// Given
|
||||
var console = new TestConsole().Width(40);
|
||||
|
||||
// When
|
||||
console.Write(new TextPath("C:/My documents/Bar/Baz.txt").RightAligned());
|
||||
|
||||
// Then
|
||||
console.Output.TrimEnd('\n')
|
||||
.ShouldBe(" C:/My documents/Bar/Baz.txt");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Should_Center_Align_Correctly()
|
||||
{
|
||||
// Given
|
||||
var console = new TestConsole().Width(40);
|
||||
|
||||
// When
|
||||
console.Write(new TextPath("C:/My documents/Bar/Baz.txt").Centered());
|
||||
|
||||
// Then
|
||||
console.Output.TrimEnd('\n')
|
||||
.ShouldBe(" C:/My documents/Bar/Baz.txt ");
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user