Add word wrapping for text

Closes #18
This commit is contained in:
Patrik Svensson
2020-08-13 21:12:45 +02:00
committed by Patrik Svensson
parent 0119364728
commit d7bbaf4a85
31 changed files with 646 additions and 643 deletions

View File

@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace Spectre.Console.Internal
@ -33,5 +34,49 @@ namespace Spectre.Console.Internal
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();
}
}
}

View File

@ -10,7 +10,7 @@ namespace Spectre.Console.Internal
{
style ??= Style.Plain;
var result = new Text(string.Empty);
var result = new Text();
using var tokenizer = new MarkupTokenizer(text);
var stack = new Stack<Style>();