| | | 1 | | using System.Collections; |
| | | 2 | | using System.Text; |
| | | 3 | | |
| | | 4 | | namespace GDX.Developer |
| | | 5 | | { |
| | | 6 | | /// <summary> |
| | | 7 | | /// A formatted text generator useful for creating text based files with some semblance of organization. |
| | | 8 | | /// </summary> |
| | | 9 | | public class TextGenerator |
| | | 10 | | { |
| | | 11 | | /// <summary> |
| | | 12 | | /// The actual buffer holder used to create the dynamic string. |
| | | 13 | | /// </summary> |
| | | 14 | | readonly StringBuilder m_Builder; |
| | | 15 | | |
| | | 16 | | /// <summary> |
| | | 17 | | /// The content used to indicate the closing of a section that was indented. |
| | | 18 | | /// </summary> |
| | | 19 | | readonly string m_IndentClose; |
| | | 20 | | |
| | | 21 | | /// <summary> |
| | | 22 | | /// The assigned indent content used to indent lines where applicable. |
| | | 23 | | /// </summary> |
| | | 24 | | readonly string m_IndentContent; |
| | | 25 | | |
| | | 26 | | /// <summary> |
| | | 27 | | /// The content used to indicate the opening of a section which should be indented. |
| | | 28 | | /// </summary> |
| | | 29 | | readonly string m_IndentOpen; |
| | | 30 | | |
| | | 31 | | /// <summary> |
| | | 32 | | /// The current level of indentation. |
| | | 33 | | /// </summary> |
| | | 34 | | int m_IndentLevel; |
| | | 35 | | |
| | | 36 | | /// <summary> |
| | | 37 | | /// Create a new <see cref="TextGenerator" /> with the |
| | | 38 | | /// </summary> |
| | | 39 | | /// <param name="indentContent"> |
| | | 40 | | /// The characters used to indent the content when applicable. By default it will use a tab representation, |
| | | 41 | | /// however for code files you may want to use four spaces. |
| | | 42 | | /// </param> |
| | | 43 | | /// <param name="indentOpen"></param> |
| | | 44 | | /// <param name="indentClose"></param> |
| | 18 | 45 | | public TextGenerator(string indentContent = "\t", string indentOpen = null, string indentClose = null) |
| | 18 | 46 | | { |
| | 18 | 47 | | m_IndentContent = indentContent; |
| | 18 | 48 | | m_IndentOpen = indentOpen; |
| | 18 | 49 | | m_IndentClose = indentClose; |
| | 18 | 50 | | m_Builder = new StringBuilder(); |
| | 18 | 51 | | } |
| | | 52 | | |
| | | 53 | | /// <summary> |
| | | 54 | | /// Apply the current level of indent to the current line being operated on. |
| | | 55 | | /// </summary> |
| | | 56 | | public void ApplyIndent() |
| | 740 | 57 | | { |
| | 4260 | 58 | | for (int i = 0; i < m_IndentLevel; i++) |
| | 1390 | 59 | | { |
| | 1390 | 60 | | m_Builder.Append(m_IndentContent); |
| | 1390 | 61 | | } |
| | 740 | 62 | | } |
| | | 63 | | |
| | | 64 | | /// <summary> |
| | | 65 | | /// Append content to the current line being operated on. |
| | | 66 | | /// </summary> |
| | | 67 | | /// <param name="content">The content to append to the current line.</param> |
| | | 68 | | public void Append(string content) |
| | 74 | 69 | | { |
| | 74 | 70 | | m_Builder.Append(content); |
| | 74 | 71 | | } |
| | | 72 | | |
| | | 73 | | /// <summary> |
| | | 74 | | /// Apply the appropriate amount of indentation to the current line, appending content afterwards and then |
| | | 75 | | /// advancing to the next line. |
| | | 76 | | /// </summary> |
| | | 77 | | /// <param name="content">The content to append to the current line.</param> |
| | | 78 | | public void AppendLine(string content = "") |
| | 713 | 79 | | { |
| | 713 | 80 | | ApplyIndent(); |
| | 713 | 81 | | m_Builder.AppendLine(content); |
| | 713 | 82 | | } |
| | | 83 | | |
| | | 84 | | /// <summary> |
| | | 85 | | /// Append an <see cref="IEnumerable" /> set of content as individual lines with proper indentation. |
| | | 86 | | /// </summary> |
| | | 87 | | /// <param name="content">The content to be added.</param> |
| | | 88 | | public void AppendLineRange(IEnumerable content) |
| | 1 | 89 | | { |
| | 7 | 90 | | foreach (string s in content) |
| | 2 | 91 | | { |
| | 2 | 92 | | ApplyIndent(); |
| | 2 | 93 | | m_Builder.AppendLine(s); |
| | 2 | 94 | | } |
| | 1 | 95 | | } |
| | | 96 | | |
| | | 97 | | /// <summary> |
| | | 98 | | /// Gets the current indent level of the <see cref="TextGenerator" />. |
| | | 99 | | /// </summary> |
| | | 100 | | /// <returns>The indent level.</returns> |
| | | 101 | | public int GetIndentLevel() |
| | 5 | 102 | | { |
| | 5 | 103 | | return m_IndentLevel; |
| | 5 | 104 | | } |
| | | 105 | | |
| | | 106 | | /// <summary> |
| | | 107 | | /// Move the builder to the start of the next line. |
| | | 108 | | /// </summary> |
| | | 109 | | public void NextLine() |
| | 15 | 110 | | { |
| | 15 | 111 | | m_Builder.AppendLine(); |
| | 15 | 112 | | } |
| | | 113 | | |
| | | 114 | | /// <summary> |
| | | 115 | | /// Remove a level of indentation from the builder. |
| | | 116 | | /// </summary> |
| | | 117 | | public void PopIndent() |
| | 59 | 118 | | { |
| | 59 | 119 | | if (m_IndentLevel <= 0) |
| | 1 | 120 | | { |
| | 1 | 121 | | return; |
| | | 122 | | } |
| | | 123 | | |
| | 58 | 124 | | m_IndentLevel--; |
| | 58 | 125 | | if (m_IndentOpen == null) |
| | 53 | 126 | | { |
| | 53 | 127 | | return; |
| | | 128 | | } |
| | | 129 | | |
| | 5 | 130 | | ApplyIndent(); |
| | 5 | 131 | | m_Builder.AppendLine(m_IndentClose); |
| | 59 | 132 | | } |
| | | 133 | | |
| | | 134 | | /// <summary> |
| | | 135 | | /// Add a level of indentation to the builder. |
| | | 136 | | /// </summary> |
| | | 137 | | /// <param name="applyOpener">Should the opener be applied?</param> |
| | | 138 | | public void PushIndent(bool applyOpener = true) |
| | 58 | 139 | | { |
| | 58 | 140 | | if (m_IndentOpen != null && applyOpener) |
| | 5 | 141 | | { |
| | 5 | 142 | | ApplyIndent(); |
| | 5 | 143 | | m_Builder.AppendLine(m_IndentOpen); |
| | 5 | 144 | | } |
| | | 145 | | |
| | 58 | 146 | | m_IndentLevel++; |
| | 58 | 147 | | } |
| | | 148 | | |
| | | 149 | | /// <summary> |
| | | 150 | | /// Returns the built string content for the builder. |
| | | 151 | | /// </summary> |
| | | 152 | | /// <remarks>Will automatically reduce the indentation level to 0.</remarks> |
| | | 153 | | public override string ToString() |
| | 11 | 154 | | { |
| | 16 | 155 | | while (m_IndentLevel > 0) |
| | 5 | 156 | | { |
| | 5 | 157 | | PopIndent(); |
| | 5 | 158 | | } |
| | | 159 | | |
| | 11 | 160 | | return m_Builder.ToString().Trim(); |
| | 11 | 161 | | } |
| | | 162 | | } |
| | | 163 | | } |