|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Text; |
| 5 | +using System.Threading.Tasks; |
| 6 | + |
| 7 | +namespace PSModuleDevelopment.Utility |
| 8 | +{ |
| 9 | + /// <summary> |
| 10 | + /// Object containing information |
| 11 | + /// </summary> |
| 12 | + public class TextHeader |
| 13 | + { |
| 14 | + /// <summary> |
| 15 | + /// The text of the header |
| 16 | + /// </summary> |
| 17 | + public string Text { get; set; } |
| 18 | + |
| 19 | + /// <summary> |
| 20 | + /// The number of lines the header text has |
| 21 | + /// </summary> |
| 22 | + public int LinesOfText |
| 23 | + { |
| 24 | + get |
| 25 | + { |
| 26 | + return Text.Split('\n').Length; |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + /// <summary> |
| 31 | + /// The maximum width any line of text has. |
| 32 | + /// </summary> |
| 33 | + public int WidthOfText |
| 34 | + { |
| 35 | + get |
| 36 | + { |
| 37 | + int result = 0; |
| 38 | + foreach (string line in Text.Split('\n')) |
| 39 | + if (line.Length > result) |
| 40 | + result = line.Length; |
| 41 | + return result; |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + /// <summary> |
| 46 | + /// The symbol of the left top corner |
| 47 | + /// </summary> |
| 48 | + public string CornerLT = "#"; |
| 49 | + /// <summary> |
| 50 | + /// The symbol of the right top corner |
| 51 | + /// </summary> |
| 52 | + public string CornerRT = "#"; |
| 53 | + /// <summary> |
| 54 | + /// The symbol of the left bottom corner |
| 55 | + /// </summary> |
| 56 | + public string CornerLB = "#"; |
| 57 | + /// <summary> |
| 58 | + /// The symbol of the right bottom corner |
| 59 | + /// </summary> |
| 60 | + public string CornerRB = "#"; |
| 61 | + /// <summary> |
| 62 | + /// The symbol of the right border |
| 63 | + /// </summary> |
| 64 | + public string BorderRight = "#"; |
| 65 | + /// <summary> |
| 66 | + /// The symbol of the left border |
| 67 | + /// </summary> |
| 68 | + public string BorderLeft = "#"; |
| 69 | + /// <summary> |
| 70 | + /// The symbol of the top border |
| 71 | + /// </summary> |
| 72 | + public string BorderTop = "-"; |
| 73 | + /// <summary> |
| 74 | + /// The symbol of the bottom border |
| 75 | + /// </summary> |
| 76 | + public string BorderBottom = "-"; |
| 77 | + |
| 78 | + /// <summary> |
| 79 | + /// Whether the header should be as wide as the window running the code |
| 80 | + /// </summary> |
| 81 | + public bool MaxWidth = true; |
| 82 | + |
| 83 | + /// <summary> |
| 84 | + /// How wide should the header be, if it isn't set to max. Anything shorter than the text width will be ignored |
| 85 | + /// </summary> |
| 86 | + public int Width = -1; |
| 87 | + |
| 88 | + /// <summary> |
| 89 | + /// How the text should be aligned within the header |
| 90 | + /// </summary> |
| 91 | + public TextAlignment TextAlignment = TextAlignment.Center; |
| 92 | + |
| 93 | + /// <summary> |
| 94 | + /// Whether the text should be padded on the side it is aligned to |
| 95 | + /// </summary> |
| 96 | + public int Padding; |
| 97 | + |
| 98 | + public TextHeader(string Text) |
| 99 | + { |
| 100 | + this.Text = Text; |
| 101 | + } |
| 102 | + |
| 103 | + public override string ToString() |
| 104 | + { |
| 105 | + StringBuilder result = new StringBuilder(); |
| 106 | + |
| 107 | + // Process Width |
| 108 | + int effectivePadding = 0; |
| 109 | + if (TextAlignment != TextAlignment.Center) |
| 110 | + effectivePadding = Padding; |
| 111 | + |
| 112 | + int width = Width; |
| 113 | + if (MaxWidth) |
| 114 | + width = UtilityHost.RawUI.WindowSize.Width; |
| 115 | + if ((WidthOfText + effectivePadding + 2 + BorderLeft.Length + BorderRight.Length) > width) |
| 116 | + width = WidthOfText + effectivePadding + 2 + BorderLeft.Length + BorderRight.Length; |
| 117 | + int effectiveContentWidth = width - 2 - BorderLeft.Length - BorderRight.Length; |
| 118 | + |
| 119 | + // Process Top Line |
| 120 | + result = result.Append(CornerLT); |
| 121 | + for (int n = 0; n < (effectiveContentWidth + 2); n++) |
| 122 | + result = result.Append(BorderTop); |
| 123 | + result = result.Append(CornerRT + "\n"); |
| 124 | + |
| 125 | + // Process Content |
| 126 | + foreach (string line in Text.Split('\n')) |
| 127 | + { |
| 128 | + result = result.Append(BorderLeft + " "); |
| 129 | + int spaceLeft = 0; |
| 130 | + int spaceRight = 0; |
| 131 | + if (TextAlignment == TextAlignment.Left) |
| 132 | + { |
| 133 | + spaceLeft += Padding; |
| 134 | + spaceRight = effectiveContentWidth - line.Length - spaceLeft; |
| 135 | + } |
| 136 | + else if (TextAlignment == TextAlignment.Right) |
| 137 | + { |
| 138 | + spaceRight += Padding; |
| 139 | + spaceLeft = effectiveContentWidth - line.Length - spaceRight; |
| 140 | + } |
| 141 | + else |
| 142 | + { |
| 143 | + spaceLeft = (effectiveContentWidth - line.Length) / 2; |
| 144 | + spaceRight = effectiveContentWidth - line.Length - spaceLeft; |
| 145 | + } |
| 146 | + |
| 147 | + result = result.Append(' ', spaceLeft); |
| 148 | + result = result.Append(line); |
| 149 | + result = result.Append(' ', spaceRight); |
| 150 | + result = result.Append(" " + BorderRight + "\n"); |
| 151 | + } |
| 152 | + |
| 153 | + // Process Bottom Line |
| 154 | + result = result.Append(CornerLB); |
| 155 | + for (int n = 0; n < (effectiveContentWidth + 2); n++) |
| 156 | + result = result.Append(BorderBottom); |
| 157 | + result = result.Append(CornerRB); |
| 158 | + |
| 159 | + return result.ToString(); |
| 160 | + } |
| 161 | + } |
| 162 | +} |
0 commit comments