@@ -24,6 +24,8 @@ public class ColoredStringBuilder
2424
2525 private readonly Stack < Color > colorStack = new Stack < Color > ( ) ;
2626
27+ private readonly List < string > indentStack = new List < string > ( ) ;
28+
2729 public override string ToString ( )
2830 {
2931 return stringBuilder . ToString ( ) ;
@@ -89,6 +91,78 @@ public AnsiColorScope NewColorScope(Color color)
8991 return new AnsiColorScope ( this , color ) ;
9092 }
9193
94+ public void Insert ( int index , string value )
95+ {
96+ if ( index >= 0 && index <= this . stringBuilder . Length )
97+ {
98+ this . stringBuilder . Insert ( index , value ) ;
99+ }
100+ }
101+
102+ public void InsertLine ( int index , string value , Color color )
103+ {
104+ if ( color != Color . Reset )
105+ {
106+ this . Insert ( index , color . ToString ( ) ) ;
107+ }
108+ this . Insert ( index , value + Environment . NewLine ) ;
109+ if ( color != Color . Reset )
110+ {
111+ this . Insert ( index , Color . Reset . ToString ( ) ) ;
112+ }
113+ }
114+
115+ public int GetCurrentIndex ( )
116+ {
117+ return this . stringBuilder . Length ;
118+ }
119+
120+ public void PushIndent ( string indent )
121+ {
122+ this . indentStack . Add ( indent ) ;
123+ }
124+
125+ public void PopIndent ( )
126+ {
127+ if ( this . indentStack . Count > 0 )
128+ {
129+ this . indentStack . RemoveAt ( this . indentStack . Count - 1 ) ;
130+ }
131+ }
132+
133+ public void EnsureNumNewLines ( int numNewLines )
134+ {
135+ if ( this . stringBuilder . Length == 0 )
136+ {
137+ for ( int i = 0 ; i < numNewLines ; i ++ )
138+ {
139+ this . stringBuilder . AppendLine ( ) ;
140+ }
141+ return ;
142+ }
143+
144+ string currentText = this . stringBuilder . ToString ( ) ;
145+ int existingNewlines = 0 ;
146+
147+ for ( int i = currentText . Length - 1 ; i >= 0 && currentText [ i ] == '\n ' ; i -- )
148+ {
149+ existingNewlines ++ ;
150+ }
151+
152+ int remainingNewlines = numNewLines - existingNewlines ;
153+ for ( int i = 0 ; i < remainingNewlines ; i ++ )
154+ {
155+ this . stringBuilder . AppendLine ( ) ;
156+ }
157+ }
158+
159+ public void Clear ( )
160+ {
161+ this . stringBuilder . Clear ( ) ;
162+ this . colorStack . Clear ( ) ;
163+ this . indentStack . Clear ( ) ;
164+ }
165+
92166 private void PushColor ( Color color )
93167 {
94168 this . colorStack . Push ( color ) ;
@@ -101,7 +175,7 @@ private void PopColor()
101175 this . stringBuilder . Append ( this . colorStack . Count > 0 ? this . colorStack . Peek ( ) : Color . Reset ) ;
102176 }
103177
104- public class AnsiColorScope : IDisposable
178+ public class AnsiColorScope : IDisposable
105179 {
106180 private readonly ColoredStringBuilder builder ;
107181
@@ -117,4 +191,4 @@ public void Dispose()
117191 }
118192 }
119193 }
120- }
194+ }
0 commit comments