2023-02-21 06:27:20 +00:00
|
|
|
namespace InnovEnergy.Lib.Utils;
|
|
|
|
|
|
|
|
public class TextBlock
|
|
|
|
{
|
2023-06-20 12:26:48 +00:00
|
|
|
private readonly IReadOnlyList<String> _Lines;
|
2023-02-21 06:27:20 +00:00
|
|
|
|
2023-06-20 12:26:48 +00:00
|
|
|
private TextBlock(params String[] lines) => _Lines = lines;
|
2023-02-21 06:27:20 +00:00
|
|
|
|
2023-06-20 12:26:48 +00:00
|
|
|
public override String ToString() => _Lines.JoinLines();
|
2023-06-13 11:04:07 +00:00
|
|
|
|
2023-08-16 13:13:46 +00:00
|
|
|
public static TextBlock Empty { get; } = new TextBlock();
|
|
|
|
|
2023-08-23 07:23:50 +00:00
|
|
|
public static TextBlock AlignLeft(IEnumerable<Object> things)
|
2023-06-20 12:26:48 +00:00
|
|
|
{
|
|
|
|
var lines = things
|
|
|
|
.SelectMany(GetLines)
|
2023-08-23 07:23:50 +00:00
|
|
|
.Unless(String.IsNullOrEmpty)
|
2023-06-20 12:26:48 +00:00
|
|
|
.ToList();
|
2023-02-21 06:27:20 +00:00
|
|
|
|
2023-08-16 13:13:46 +00:00
|
|
|
if (!lines.Any())
|
|
|
|
return Empty;
|
|
|
|
|
|
|
|
var width = lines.Max(l => l.Length);
|
2023-06-13 11:04:07 +00:00
|
|
|
|
2023-07-03 13:47:34 +00:00
|
|
|
var alignedLines = lines
|
|
|
|
.Select(l => l.PadRight(width))
|
|
|
|
.ToArray(lines.Count);
|
2023-06-20 12:26:48 +00:00
|
|
|
|
|
|
|
return new TextBlock(alignedLines);
|
2023-06-13 11:04:07 +00:00
|
|
|
}
|
|
|
|
|
2023-07-13 07:53:13 +00:00
|
|
|
private static TextBlock AlignRight(IReadOnlyList<Object> things)
|
2023-06-20 12:26:48 +00:00
|
|
|
{
|
|
|
|
var lines = things
|
|
|
|
.SelectMany(GetLines)
|
2023-08-23 07:23:50 +00:00
|
|
|
.Unless(String.IsNullOrEmpty)
|
2023-06-20 12:26:48 +00:00
|
|
|
.ToList();
|
2023-06-13 11:04:07 +00:00
|
|
|
|
2023-08-16 13:13:46 +00:00
|
|
|
if (!lines.Any())
|
|
|
|
return Empty;
|
|
|
|
|
2023-06-20 12:26:48 +00:00
|
|
|
var width = lines.Max(l => l.Length);
|
2023-06-13 11:04:07 +00:00
|
|
|
|
2023-07-03 13:47:34 +00:00
|
|
|
var alignedLines = lines
|
|
|
|
.Select(l => l.PadLeft(width))
|
|
|
|
.ToArray(lines.Count);
|
2023-06-20 12:26:48 +00:00
|
|
|
|
|
|
|
return new TextBlock(alignedLines);
|
|
|
|
}
|
|
|
|
|
2023-07-03 13:47:34 +00:00
|
|
|
public static TextBlock AlignCenterHorizontal(IReadOnlyList<Object> things)
|
2023-02-21 06:27:20 +00:00
|
|
|
{
|
2023-06-20 12:26:48 +00:00
|
|
|
var lines = things
|
|
|
|
.SelectMany(GetLines)
|
2023-08-18 13:57:00 +00:00
|
|
|
.Where(l => !String.IsNullOrEmpty(l))
|
2023-06-20 12:26:48 +00:00
|
|
|
.ToList();
|
2023-06-13 11:04:07 +00:00
|
|
|
|
2023-08-18 13:57:00 +00:00
|
|
|
if (!lines.Any())
|
|
|
|
return Empty;
|
|
|
|
|
2023-06-20 12:26:48 +00:00
|
|
|
var width = lines.Max(l => l.Length);
|
|
|
|
|
|
|
|
var alignedLines = lines
|
|
|
|
.Select(l => l.PadLeft((width + l.Length) / 2).PadRight(width))
|
|
|
|
.ToArray(lines.Count);
|
|
|
|
|
|
|
|
return new TextBlock(alignedLines);
|
2023-02-21 06:27:20 +00:00
|
|
|
}
|
2023-08-23 07:23:50 +00:00
|
|
|
|
2023-07-03 13:47:34 +00:00
|
|
|
public static TextBlock AlignTop(IReadOnlyList<Object> things)
|
2023-02-21 06:27:20 +00:00
|
|
|
{
|
2023-06-20 12:26:48 +00:00
|
|
|
var columns = things
|
|
|
|
.Select(GetLines)
|
2023-08-18 13:57:00 +00:00
|
|
|
.Where(c => c.Count > 0)
|
2023-06-20 12:26:48 +00:00
|
|
|
.ToList();
|
|
|
|
|
2023-08-18 13:57:00 +00:00
|
|
|
if (!columns.Any())
|
|
|
|
return Empty;
|
|
|
|
|
2023-06-20 12:26:48 +00:00
|
|
|
var height = columns.Max(l => l.Count);
|
2023-02-21 06:27:20 +00:00
|
|
|
|
2023-06-20 12:26:48 +00:00
|
|
|
var alignedLines = Enumerable
|
|
|
|
.Range(0, height)
|
|
|
|
.Select(l => columns.Select(c => c.ElementAtOr(l, Space(c[0].Length))).Join())
|
|
|
|
.ToArray(height);
|
|
|
|
|
|
|
|
return new TextBlock(alignedLines);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-07-03 13:47:34 +00:00
|
|
|
public static TextBlock AlignBottom(IReadOnlyList<Object> things)
|
2023-06-20 12:26:48 +00:00
|
|
|
{
|
|
|
|
var columns = things
|
|
|
|
.Select(GetLines)
|
2023-08-18 13:57:00 +00:00
|
|
|
.Where(c => c.Count > 0)
|
2023-02-21 06:27:20 +00:00
|
|
|
.ToList();
|
2023-06-20 12:26:48 +00:00
|
|
|
|
2023-08-18 13:57:00 +00:00
|
|
|
if (!columns.Any())
|
|
|
|
return Empty;
|
|
|
|
|
2023-06-20 12:26:48 +00:00
|
|
|
var height = columns.Max(l => l.Count);
|
|
|
|
|
|
|
|
var alignedLines = Enumerable
|
|
|
|
.Range(0, height)
|
|
|
|
.Select(l => columns.Select(c => c.ElementAtOr(c.Count - height + l, Space(c[0].Length))).Join())
|
|
|
|
.ToArray(height);
|
|
|
|
|
|
|
|
return new TextBlock(alignedLines);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-07-03 13:47:34 +00:00
|
|
|
public static TextBlock AlignCenterVertical(IReadOnlyList<Object> things)
|
2023-06-20 12:26:48 +00:00
|
|
|
{
|
|
|
|
var columns = things
|
|
|
|
.Select(GetLines)
|
2023-08-18 13:57:00 +00:00
|
|
|
.Where(c => c.Count > 0)
|
2023-06-20 12:26:48 +00:00
|
|
|
.ToList();
|
|
|
|
|
2023-08-18 13:57:00 +00:00
|
|
|
if (!columns.Any())
|
|
|
|
return Empty;
|
|
|
|
|
2023-06-20 12:26:48 +00:00
|
|
|
var height = columns.Max(l => l.Count);
|
2023-02-21 06:27:20 +00:00
|
|
|
|
2023-06-20 12:26:48 +00:00
|
|
|
var alignedLines = Enumerable
|
|
|
|
.Range(0, height)
|
|
|
|
.Select(l => columns.Select(c => c.ElementAtOr((c.Count - height) / 2 + l, Space(c[0].Length))).Join())
|
|
|
|
.ToArray(height);
|
2023-02-21 06:27:20 +00:00
|
|
|
|
2023-06-20 12:26:48 +00:00
|
|
|
return new TextBlock(alignedLines);
|
|
|
|
}
|
|
|
|
|
2023-08-16 13:13:46 +00:00
|
|
|
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);
|
2023-06-20 12:26:48 +00:00
|
|
|
|
2023-07-03 13:47:34 +00:00
|
|
|
public static TextBlock FromString(String thing) => AlignLeft(thing);
|
2023-06-20 12:26:48 +00:00
|
|
|
|
|
|
|
public TextBlock Box()
|
|
|
|
{
|
2023-08-18 13:57:00 +00:00
|
|
|
var width = _Lines.Any() ? _Lines.Max(l => l.Length) : 0;
|
2023-06-20 12:26:48 +00:00
|
|
|
|
|
|
|
var hLine = "".PadRight(width + 2, '─');
|
|
|
|
var top = "┌" + hLine + "┐";
|
|
|
|
var bottom = "└" + hLine + "┘";
|
|
|
|
|
|
|
|
var lines = _Lines
|
|
|
|
.Select(l => l.SurroundWith(" "))
|
|
|
|
.Select(l => l.SurroundWith("│"))
|
|
|
|
.Prepend(top)
|
|
|
|
.Append(bottom)
|
|
|
|
.ToArray(_Lines.Count + 2);
|
2023-02-21 06:27:20 +00:00
|
|
|
|
2023-06-20 12:26:48 +00:00
|
|
|
return new TextBlock(lines);
|
2023-02-21 06:27:20 +00:00
|
|
|
}
|
|
|
|
|
2023-06-20 12:26:48 +00:00
|
|
|
public TextBlock TitleBox(String title)
|
|
|
|
{
|
2023-08-18 13:57:00 +00:00
|
|
|
var linesWidth = _Lines.Any() ? _Lines.Max(l => l.Length) : 0;
|
2023-06-20 12:26:48 +00:00
|
|
|
var titleWidth = title.Length;
|
|
|
|
|
|
|
|
var width = Math.Max(linesWidth, titleWidth);
|
|
|
|
|
|
|
|
var hLine = "".PadRight(width + 2, '─');
|
|
|
|
var top = "┌" + hLine + "┐";
|
|
|
|
var between = "├" + hLine + "┤";
|
|
|
|
var bottom = "└" + hLine + "┘";
|
|
|
|
|
|
|
|
var centeredTitle = "│ " + title.PadLeft((width + title.Length) / 2).PadRight(width) + " │";
|
|
|
|
|
|
|
|
var lines = _Lines
|
|
|
|
.Select(l => l.PadRight(width))
|
|
|
|
.Select(l => l.SurroundWith(" "))
|
|
|
|
.Select(l => l.SurroundWith("│"))
|
|
|
|
.Prepend(between)
|
|
|
|
.Prepend(centeredTitle)
|
|
|
|
.Prepend(top)
|
|
|
|
.Append(bottom)
|
|
|
|
.ToArray();
|
|
|
|
|
|
|
|
return new TextBlock(lines);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static TextBlock Spacer(Int32 n)
|
2023-02-21 06:27:20 +00:00
|
|
|
{
|
2023-06-20 12:26:48 +00:00
|
|
|
return new TextBlock(Enumerable.Repeat("".PadRight(n), n).ToArray(n));
|
|
|
|
}
|
|
|
|
|
|
|
|
public static TextBlock HorizontalSpace(Int32 n)
|
|
|
|
{
|
|
|
|
return new TextBlock("".PadRight(n));
|
|
|
|
}
|
|
|
|
|
|
|
|
public static TextBlock VerticalSpace(Int32 n)
|
|
|
|
{
|
|
|
|
return new TextBlock(Enumerable.Repeat("", n).ToArray(n));
|
2023-02-21 06:27:20 +00:00
|
|
|
}
|
|
|
|
|
2023-06-13 11:04:07 +00:00
|
|
|
|
2023-08-16 13:13:46 +00:00
|
|
|
private static IReadOnlyList<String> GetLines(Object? t)
|
2023-06-20 12:26:48 +00:00
|
|
|
{
|
2023-08-16 13:13:46 +00:00
|
|
|
return t switch
|
|
|
|
{
|
|
|
|
TextBlock tb => tb._Lines,
|
|
|
|
null => Array.Empty<String>(),
|
|
|
|
_ => t.ToString()!.SplitLines()
|
|
|
|
};
|
2023-06-20 12:26:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private static String Space(Int32 totalWidth)
|
|
|
|
{
|
|
|
|
return "".PadRight(totalWidth);
|
|
|
|
}
|
2023-08-16 13:13:46 +00:00
|
|
|
}
|