Introduce TextBlock.Empty

This commit is contained in:
ig 2023-08-16 15:13:46 +02:00
parent 2bd20ce4eb
commit ea7a13da06
2 changed files with 27 additions and 16 deletions

View File

@ -132,9 +132,11 @@ public static class StringUtils
.SideBySideWith(text, "");
}
public static IReadOnlyList<String> SplitLines(this String s)
public static IReadOnlyList<String> SplitLines(this String? s)
{
return s.Split(new[] { Environment.NewLine }, StringSplitOptions.None);
return s is null
? Array.Empty<String>()
: s.Split(new[] { Environment.NewLine }, StringSplitOptions.None);
}

View File

@ -8,12 +8,17 @@ public class TextBlock
public override String ToString() => _Lines.JoinLines();
public static TextBlock Empty { get; } = new TextBlock();
public static TextBlock AlignLeft(IReadOnlyList<Object> things)
{
var lines = things
.SelectMany(GetLines)
.ToList();
if (!lines.Any())
return Empty;
var width = lines.Max(l => l.Length);
var alignedLines = lines
@ -29,6 +34,9 @@ public class TextBlock
.SelectMany(GetLines)
.ToList();
if (!lines.Any())
return Empty;
var width = lines.Max(l => l.Length);
var alignedLines = lines
@ -53,8 +61,6 @@ public class TextBlock
return new TextBlock(alignedLines);
}
public static TextBlock AlignTop(IReadOnlyList<Object> things)
{
var columns = things
@ -105,12 +111,12 @@ public class TextBlock
return new TextBlock(alignedLines);
}
public static TextBlock AlignLeft (params Object[] things) => AlignLeft ((IReadOnlyList<Object>) things);
public static TextBlock AlignRight (params Object[] things) => AlignRight ((IReadOnlyList<Object>) things);
public static TextBlock AlignTop (params Object[] things) => AlignTop ((IReadOnlyList<Object>) things);
public static TextBlock AlignBottom (params Object[] things) => AlignBottom ((IReadOnlyList<Object>) things);
public static TextBlock AlignCenterVertical (params Object[] things) => AlignCenterVertical ((IReadOnlyList<Object>) things);
public static TextBlock AlignCenterHorizontal(params Object[] things) => AlignCenterHorizontal((IReadOnlyList<Object>) things);
public static TextBlock AlignLeft (params Object?[] things) => AlignLeft ((IReadOnlyList<Object>) things);
public static TextBlock AlignRight (params Object?[] things) => AlignRight ((IReadOnlyList<Object>) things);
public static TextBlock AlignTop (params Object?[] things) => AlignTop ((IReadOnlyList<Object>) things);
public static TextBlock AlignBottom (params Object?[] things) => AlignBottom ((IReadOnlyList<Object>) things);
public static TextBlock AlignCenterVertical (params Object?[] things) => AlignCenterVertical ((IReadOnlyList<Object>) things);
public static TextBlock AlignCenterHorizontal(params Object?[] things) => AlignCenterHorizontal((IReadOnlyList<Object>) things);
public static TextBlock FromString(String thing) => AlignLeft(thing);
@ -175,11 +181,14 @@ public class TextBlock
}
private static IReadOnlyList<String> GetLines(Object t)
private static IReadOnlyList<String> GetLines(Object? t)
{
return t is TextBlock tb
? tb._Lines
: t.ToString()!.SplitLines();
return t switch
{
TextBlock tb => tb._Lines,
null => Array.Empty<String>(),
_ => t.ToString()!.SplitLines()
};
}
private static String Space(Int32 totalWidth)