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