mirror of
https://github.com/nsnail/spectre.console.git
synced 2025-04-16 17:02:51 +08:00
Merge segments before rendering
This will reduce the number of segments to render and produce cleaner ANSI escape code sequences. Closes #46
This commit is contained in:
parent
47fd646d21
commit
7fd2efaeb5
@ -12,7 +12,7 @@ namespace Spectre.Console.Tests.Unit
|
|||||||
{
|
{
|
||||||
[Theory]
|
[Theory]
|
||||||
[InlineData("[yellow]Hello[/]", "[93mHello[0m")]
|
[InlineData("[yellow]Hello[/]", "[93mHello[0m")]
|
||||||
[InlineData("[yellow]Hello [italic]World[/]![/]", "[93mHello[0m[93m [0m[3;93mWorld[0m[93m![0m")]
|
[InlineData("[yellow]Hello [italic]World[/]![/]", "[93mHello [0m[3;93mWorld[0m[93m![0m")]
|
||||||
public void Should_Output_Expected_Ansi_For_Markup(string markup, string expected)
|
public void Should_Output_Expected_Ansi_For_Markup(string markup, string expected)
|
||||||
{
|
{
|
||||||
// Given
|
// Given
|
||||||
@ -26,7 +26,7 @@ namespace Spectre.Console.Tests.Unit
|
|||||||
}
|
}
|
||||||
|
|
||||||
[Theory]
|
[Theory]
|
||||||
[InlineData("[yellow]Hello [[ World[/]", "[93mHello[0m[93m [0m[93m[[0m[93m [0m[93mWorld[0m")]
|
[InlineData("[yellow]Hello [[ World[/]", "[93mHello [ World[0m")]
|
||||||
public void Should_Be_Able_To_Escape_Tags(string markup, string expected)
|
public void Should_Be_Able_To_Escape_Tags(string markup, string expected)
|
||||||
{
|
{
|
||||||
// Given
|
// Given
|
||||||
|
@ -30,8 +30,11 @@ namespace Spectre.Console
|
|||||||
|
|
||||||
using (console.PushStyle(Style.Plain))
|
using (console.PushStyle(Style.Plain))
|
||||||
{
|
{
|
||||||
|
var segments = renderable.Render(options, console.Width);
|
||||||
|
segments = Segment.Merge(segments);
|
||||||
|
|
||||||
var current = Style.Plain;
|
var current = Style.Plain;
|
||||||
foreach (var segment in renderable.Render(options, console.Width))
|
foreach (var segment in segments)
|
||||||
{
|
{
|
||||||
if (string.IsNullOrEmpty(segment.Text))
|
if (string.IsNullOrEmpty(segment.Text))
|
||||||
{
|
{
|
||||||
|
@ -1,5 +1,4 @@
|
|||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Runtime.InteropServices;
|
|
||||||
|
|
||||||
namespace Spectre.Console.Internal
|
namespace Spectre.Console.Internal
|
||||||
{
|
{
|
||||||
|
@ -16,7 +16,7 @@ namespace Spectre.Console.Rendering
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets the segment text.
|
/// Gets the segment text.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public string Text { get; }
|
public string Text { get; internal set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets a value indicating whether or not this is an expicit line break
|
/// Gets a value indicating whether or not this is an expicit line break
|
||||||
@ -226,6 +226,41 @@ namespace Spectre.Console.Rendering
|
|||||||
return lines;
|
return lines;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
internal static IEnumerable<Segment> Merge(IEnumerable<Segment> segments)
|
||||||
|
{
|
||||||
|
var result = new List<Segment>();
|
||||||
|
|
||||||
|
var previous = (Segment?)null;
|
||||||
|
foreach (var segment in segments)
|
||||||
|
{
|
||||||
|
if (previous == null)
|
||||||
|
{
|
||||||
|
previous = segment;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Same style?
|
||||||
|
if (previous.Style.Equals(segment.Style))
|
||||||
|
{
|
||||||
|
// Modify the content of the previous segment
|
||||||
|
previous.Text += segment.Text;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// Push the current one to the results.
|
||||||
|
result.Add(previous);
|
||||||
|
previous = segment;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (previous != null)
|
||||||
|
{
|
||||||
|
result.Add(previous);
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
internal static List<List<SegmentLine>> MakeSameHeight(int cellHeight, List<List<SegmentLine>> cells)
|
internal static List<List<SegmentLine>> MakeSameHeight(int cellHeight, List<List<SegmentLine>> cells)
|
||||||
{
|
{
|
||||||
foreach (var cell in cells)
|
foreach (var cell in cells)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user