Innovenergy_trunk/csharp/Lib/Utils/TextBlock.cs

190 lines
6.1 KiB
C#
Raw Normal View History

namespace InnovEnergy.Lib.Utils;
public class TextBlock
{
2023-06-20 12:26:48 +00:00
private readonly IReadOnlyList<String> _Lines;
2023-06-20 12:26:48 +00:00
private TextBlock(params String[] lines) => _Lines = lines;
2023-06-20 12:26:48 +00:00
public override String ToString() => _Lines.JoinLines();
2023-06-13 11:04:07 +00:00
2023-07-03 13:47:34 +00:00
public static TextBlock AlignLeft(IReadOnlyList<Object> things)
2023-06-20 12:26:48 +00:00
{
var lines = things
.SelectMany(GetLines)
.ToList();
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.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-03 13:47:34 +00:00
public static TextBlock AlignRight(IReadOnlyList<Object> things)
2023-06-20 12:26:48 +00:00
{
var lines = things
.SelectMany(GetLines)
.ToList();
2023-06-13 11:04:07 +00:00
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-06-20 12:26:48 +00:00
var lines = things
.SelectMany(GetLines)
.ToList();
2023-06-13 11:04:07 +00:00
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-07-03 13:47:34 +00:00
2023-06-20 12:26:48 +00:00
2023-07-03 13:47:34 +00:00
public static TextBlock AlignTop(IReadOnlyList<Object> things)
{
2023-06-20 12:26:48 +00:00
var columns = things
.Select(GetLines)
.ToList();
var height = columns.Max(l => l.Count);
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)
.ToList();
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)
.ToList();
var height = columns.Max(l => l.Count);
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-06-20 12:26:48 +00:00
return new TextBlock(alignedLines);
}
2023-07-03 13:47:34 +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()
{
var width = _Lines.Max(l => l.Length);
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-06-20 12:26:48 +00:00
return new TextBlock(lines);
}
2023-06-20 12:26:48 +00:00
public TextBlock TitleBox(String title)
{
var linesWidth = _Lines.Max(l => l.Length);
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-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-06-13 11:04:07 +00:00
2023-06-20 12:26:48 +00:00
private static IReadOnlyList<String> GetLines(Object t)
{
return t is TextBlock tb
? tb._Lines
: t.ToString()!.SplitLines();
}
private static String Space(Int32 totalWidth)
{
return "".PadRight(totalWidth);
}
}