using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PSModuleDevelopment.Utility
{
///
/// Object containing information
///
public class TextHeader
{
///
/// The text of the header
///
public string Text { get; set; }
///
/// The number of lines the header text has
///
public int LinesOfText
{
get
{
return Text.Split('\n').Length;
}
}
///
/// The maximum width any line of text has.
///
public int WidthOfText
{
get
{
int result = 0;
foreach (string line in Text.Split('\n'))
if (line.Length > result)
result = line.Length;
return result;
}
}
///
/// The symbol of the left top corner
///
public string CornerLT = "#";
///
/// The symbol of the right top corner
///
public string CornerRT = "#";
///
/// The symbol of the left bottom corner
///
public string CornerLB = "#";
///
/// The symbol of the right bottom corner
///
public string CornerRB = "#";
///
/// The symbol of the right border
///
public string BorderRight = "#";
///
/// The symbol of the left border
///
public string BorderLeft = "#";
///
/// The symbol of the top border
///
public string BorderTop = "-";
///
/// The symbol of the bottom border
///
public string BorderBottom = "-";
///
/// Whether the header should be as wide as the window running the code
///
public bool MaxWidth = true;
///
/// How wide should the header be, if it isn't set to max. Anything shorter than the text width will be ignored
///
public int Width = -1;
///
/// How the text should be aligned within the header
///
public TextAlignment TextAlignment = TextAlignment.Center;
///
/// Whether the text should be padded on the side it is aligned to
///
public int Padding;
///
/// Creates a new header around the specified string
///
/// The string to put in a header
public TextHeader(string Text)
{
this.Text = Text;
}
///
/// Converts the header into its string representation
///
/// The header text to use whereever said header is needed.
public override string ToString()
{
StringBuilder result = new StringBuilder();
// Process Width
int effectivePadding = 0;
if (TextAlignment != TextAlignment.Center)
effectivePadding = Padding;
int width = Width;
if (MaxWidth)
width = UtilityHost.RawUI.WindowSize.Width;
if ((WidthOfText + effectivePadding + 2 + BorderLeft.Length + BorderRight.Length) > width)
width = WidthOfText + effectivePadding + 2 + BorderLeft.Length + BorderRight.Length;
int effectiveContentWidth = width - 2 - BorderLeft.Length - BorderRight.Length;
// Process Top Line
result = result.Append(CornerLT);
for (int n = 0; n < (effectiveContentWidth + 2); n++)
result = result.Append(BorderTop);
result = result.Append(CornerRT + "\n");
// Process Content
foreach (string line in Text.Split('\n'))
{
result = result.Append(BorderLeft + " ");
int spaceLeft = 0;
int spaceRight = 0;
if (TextAlignment == TextAlignment.Left)
{
spaceLeft += Padding;
spaceRight = effectiveContentWidth - line.Length - spaceLeft;
}
else if (TextAlignment == TextAlignment.Right)
{
spaceRight += Padding;
spaceLeft = effectiveContentWidth - line.Length - spaceRight;
}
else
{
spaceLeft = (effectiveContentWidth - line.Length) / 2;
spaceRight = effectiveContentWidth - line.Length - spaceLeft;
}
result = result.Append(' ', spaceLeft);
result = result.Append(line);
result = result.Append(' ', spaceRight);
result = result.Append(" " + BorderRight + "\n");
}
// Process Bottom Line
result = result.Append(CornerLB);
for (int n = 0; n < (effectiveContentWidth + 2); n++)
result = result.Append(BorderBottom);
result = result.Append(CornerRB);
return result.ToString();
}
}
}