|
29 | 29 | * SUCH DAMAGE.
|
30 | 30 | */
|
31 | 31 |
|
32 |
| -using System.Drawing; |
| 32 | +using System; |
| 33 | +using System.Drawing; |
| 34 | +using System.Linq; |
33 | 35 | using System.Text;
|
34 | 36 | using System.Windows.Forms;
|
35 | 37 |
|
36 | 38 |
|
37 | 39 | namespace XenAdmin.Core
|
38 | 40 | {
|
39 |
| - internal static class ExtensionMethods |
40 |
| - { |
41 |
| - /// <summary> |
42 |
| - /// Internationalization of True/False |
43 |
| - /// </summary> |
44 |
| - public static string ToStringI18n(this bool value) |
45 |
| - { |
46 |
| - return value ? Messages.TRUE : Messages.FALSE; |
47 |
| - } |
| 41 | + internal static class ExtensionMethods |
| 42 | + { |
| 43 | + private const int DEFAULT_STRING_INDENTATION = 2; |
| 44 | + /// <summary> |
| 45 | + /// Internationalization of True/False |
| 46 | + /// </summary> |
| 47 | + public static string ToStringI18n(this bool value) |
| 48 | + { |
| 49 | + return value ? Messages.TRUE : Messages.FALSE; |
| 50 | + } |
48 | 51 |
|
49 |
| - /// <summary> |
50 |
| - /// Turns a bool to internationalized Yes/No (on occasion it's user friendlier than True/False) |
51 |
| - /// </summary> |
52 |
| - public static string ToYesNoStringI18n(this bool value) |
53 |
| - { |
54 |
| - return value ? Messages.YES : Messages.NO; |
55 |
| - } |
| 52 | + /// <summary> |
| 53 | + /// Turns a bool to internationalized Yes/No (on occasion it's user friendlier than True/False) |
| 54 | + /// </summary> |
| 55 | + public static string ToYesNoStringI18n(this bool value) |
| 56 | + { |
| 57 | + return value ? Messages.YES : Messages.NO; |
| 58 | + } |
56 | 59 |
|
57 | 60 | /// <summary>
|
58 | 61 | /// This has the same bahvoiur as the standard ellipsise extension but this uses graphics
|
@@ -88,24 +91,81 @@ public static string Ellipsise(this string text, Rectangle rectangle, Font font)
|
88 | 91 | return text.Ellipsise(c);
|
89 | 92 | }
|
90 | 93 |
|
91 |
| - public static StringBuilder AppendIndented(this StringBuilder builder, string value, int indent = 2) |
| 94 | + /// <summary> |
| 95 | + /// Append the input value after it's been prepended with the specified amount of spaces. |
| 96 | + /// If the value spans multiple lines, each line will be indented. |
| 97 | + /// </summary> |
| 98 | + /// <param name="builder">The <see cref="StringBuilder"/> to which the modified value will be appended.</param> |
| 99 | + /// <param name="value">The value to prepend with spaces and then append.</param> |
| 100 | + /// <param name="indent">The amount of spaces to prepend to each line in the input value.</param> |
| 101 | + /// <returns>The input <see cref="StringBuilder"/> after the operation has been completed.</returns> |
| 102 | + public static StringBuilder AppendIndented(this StringBuilder builder, string value, int indent = DEFAULT_STRING_INDENTATION) |
92 | 103 | {
|
93 |
| - var indentString = ""; |
94 |
| - var i = 0; |
95 |
| - while (i++ < indent) |
96 |
| - indentString += " "; |
97 |
| - var newvalue = value.Replace(System.Environment.NewLine, string.Format("{0}{1}", System.Environment.NewLine, indentString)); |
98 |
| - return builder.Append(string.Format("{0}{1}", indentString, newvalue)); |
| 104 | + return builder.Append(PrependIndentation(value, indent)); |
99 | 105 | }
|
100 | 106 |
|
101 |
| - public static StringBuilder AppendIndented(this StringBuilder builder, StringBuilder value, int indent = 2) |
| 107 | + /// <summary> |
| 108 | + /// Add a new line to the input <see cref="StringBuilder"/>, with options to format the input value before it's appended. |
| 109 | + /// timestamps and indentation will be ignored if the input value is an null or whitespace |
| 110 | + /// </summary> |
| 111 | + /// <param name="builder">The <see cref="StringBuilder"/> to which the modified value will be appended.</param> |
| 112 | + /// <param name="value">The value to format before appending.</param> |
| 113 | + /// <param name="timestamp">The timestamp to prepend to each line. If null, no timestamp will be added.</param> |
| 114 | + /// <param name="showTimestamp">Override for the timestamp. If set to false, no timestamp will be shown even if the value is null.</param> |
| 115 | + /// <param name="indent">true if each line should be prepended with indentation. Uses the default indentation defined in <see cref="ExtensionMethods"/>: <see cref="DEFAULT_STRING_INDENTATION"/></param> |
| 116 | + /// <param name="addExtraLine">true to append an extra line.</param> |
| 117 | + /// <returns>The input <see cref="StringBuilder"/> after the operation has been completed.</returns> |
| 118 | + public static StringBuilder AppendFormattedLine(this StringBuilder builder, string value, DateTime? timestamp, bool showTimestamp = true, bool indent = false, bool addExtraLine = false) |
| 119 | + { |
| 120 | + var formattedValue = value; |
| 121 | + if (!string.IsNullOrWhiteSpace(value)) |
| 122 | + { |
| 123 | + if (indent) |
| 124 | + { |
| 125 | + formattedValue = PrependIndentation(formattedValue); |
| 126 | + } |
| 127 | + if (timestamp != null && showTimestamp) |
| 128 | + { |
| 129 | + formattedValue = PrependTimestamps(formattedValue, (DateTime) timestamp); |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + builder.AppendLine(formattedValue); |
| 134 | + |
| 135 | + if (addExtraLine) |
| 136 | + { |
| 137 | + builder.AppendLine(); |
| 138 | + } |
| 139 | + |
| 140 | + return builder; |
| 141 | + } |
| 142 | + |
| 143 | + /// <summary> |
| 144 | + /// Prepend every line in the input value with the specified indentation level. |
| 145 | + /// </summary> |
| 146 | + /// <param name="value">The value to which indentation will be applied</param> |
| 147 | + /// <param name="indent">The level of indentation, i.e. the number of spaces to prepend to every line in the value.</param> |
| 148 | + /// <returns>The input value with prepended indentation./</returns> |
| 149 | + private static string PrependIndentation(string value, int indent = DEFAULT_STRING_INDENTATION) |
| 150 | + { |
| 151 | + var indentString = new string(' ', indent); |
| 152 | + var newValue = value.Replace(Environment.NewLine, $"{Environment.NewLine}{indentString}"); |
| 153 | + return $"{indentString}{newValue}"; |
| 154 | + } |
| 155 | + |
| 156 | + /// <summary> |
| 157 | + /// Prepend every line in the input value with a formatted string of <see cref="DateTime.Now"/>. |
| 158 | + /// </summary> |
| 159 | + /// <param name="value">The input value</param> |
| 160 | + /// <param name="timestamp">The timestamp to show</param> |
| 161 | + /// <param name="localize">true to format the string with the user's locale</param> |
| 162 | + /// <returns>The input value with prepended timestamps/</returns> |
| 163 | + public static string PrependTimestamps(string value, DateTime timestamp, bool localize = true) |
102 | 164 | {
|
103 |
| - var indentString = ""; |
104 |
| - var i = 0; |
105 |
| - while (i++ < indent) |
106 |
| - indentString += " "; |
107 |
| - var newvalue = value.Replace(System.Environment.NewLine, string.Format("{0}{1}", System.Environment.NewLine, indentString)); |
108 |
| - return builder.Append(string.Format("{0}{1}", indentString, newvalue)); |
| 165 | + var timestampString = HelpersGUI.DateTimeToString(timestamp, Messages.DATEFORMAT_DM_HMS, localize); |
| 166 | + // normalise all line endings before splitting |
| 167 | + var lines = value.Replace(Environment.NewLine, "\n").Split('\n'); |
| 168 | + return string.Join(Environment.NewLine, lines.Select(line => $"{timestampString} | {line}")); |
109 | 169 | }
|
110 |
| - } |
| 170 | + } |
111 | 171 | }
|
0 commit comments