diff --git a/csharp/Lib/Utils/StringUtils.cs b/csharp/Lib/Utils/StringUtils.cs index 763a164a0..774706efe 100644 --- a/csharp/Lib/Utils/StringUtils.cs +++ b/csharp/Lib/Utils/StringUtils.cs @@ -132,9 +132,11 @@ public static class StringUtils .SideBySideWith(text, ""); } - public static IReadOnlyList SplitLines(this String s) + public static IReadOnlyList SplitLines(this String? s) { - return s.Split(new[] { Environment.NewLine }, StringSplitOptions.None); + return s is null + ? Array.Empty() + : s.Split(new[] { Environment.NewLine }, StringSplitOptions.None); } diff --git a/csharp/Lib/Utils/TextBlock.cs b/csharp/Lib/Utils/TextBlock.cs index 1f7349d78..1b3ccc709 100644 --- a/csharp/Lib/Utils/TextBlock.cs +++ b/csharp/Lib/Utils/TextBlock.cs @@ -8,13 +8,18 @@ public class TextBlock public override String ToString() => _Lines.JoinLines(); + public static TextBlock Empty { get; } = new TextBlock(); + public static TextBlock AlignLeft(IReadOnlyList things) { var lines = things .SelectMany(GetLines) .ToList(); - var width = lines.Max(l => l.Length); + if (!lines.Any()) + return Empty; + + var width = lines.Max(l => l.Length); var alignedLines = lines .Select(l => l.PadRight(width)) @@ -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 things) { var columns = things @@ -105,12 +111,12 @@ public class TextBlock return new TextBlock(alignedLines); } - public static TextBlock AlignLeft (params Object[] things) => AlignLeft ((IReadOnlyList) things); - public static TextBlock AlignRight (params Object[] things) => AlignRight ((IReadOnlyList) things); - public static TextBlock AlignTop (params Object[] things) => AlignTop ((IReadOnlyList) things); - public static TextBlock AlignBottom (params Object[] things) => AlignBottom ((IReadOnlyList) things); - public static TextBlock AlignCenterVertical (params Object[] things) => AlignCenterVertical ((IReadOnlyList) things); - public static TextBlock AlignCenterHorizontal(params Object[] things) => AlignCenterHorizontal((IReadOnlyList) things); + public static TextBlock AlignLeft (params Object?[] things) => AlignLeft ((IReadOnlyList) things); + public static TextBlock AlignRight (params Object?[] things) => AlignRight ((IReadOnlyList) things); + public static TextBlock AlignTop (params Object?[] things) => AlignTop ((IReadOnlyList) things); + public static TextBlock AlignBottom (params Object?[] things) => AlignBottom ((IReadOnlyList) things); + public static TextBlock AlignCenterVertical (params Object?[] things) => AlignCenterVertical ((IReadOnlyList) things); + public static TextBlock AlignCenterHorizontal(params Object?[] things) => AlignCenterHorizontal((IReadOnlyList) things); public static TextBlock FromString(String thing) => AlignLeft(thing); @@ -175,15 +181,18 @@ public class TextBlock } - private static IReadOnlyList GetLines(Object t) + private static IReadOnlyList GetLines(Object? t) { - return t is TextBlock tb - ? tb._Lines - : t.ToString()!.SplitLines(); + return t switch + { + TextBlock tb => tb._Lines, + null => Array.Empty(), + _ => t.ToString()!.SplitLines() + }; } private static String Space(Int32 totalWidth) { return "".PadRight(totalWidth); } -} +} \ No newline at end of file