-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Do not review - powershell changes for what if #29291
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -24,6 +24,8 @@ public class ColoredStringBuilder | |
|
|
||
| private readonly Stack<Color> colorStack = new Stack<Color>(); | ||
|
|
||
| private readonly List<string> indentStack = new List<string>(); | ||
|
|
||
| public override string ToString() | ||
| { | ||
| return stringBuilder.ToString(); | ||
|
|
@@ -89,6 +91,78 @@ public AnsiColorScope NewColorScope(Color color) | |
| return new AnsiColorScope(this, color); | ||
| } | ||
|
|
||
| public void Insert(int index, string value) | ||
| { | ||
| if (index >= 0 && index <= this.stringBuilder.Length) | ||
| { | ||
| this.stringBuilder.Insert(index, value); | ||
| } | ||
| } | ||
|
|
||
| public void InsertLine(int index, string value, Color color) | ||
| { | ||
| if (color != Color.Reset) | ||
| { | ||
| this.Insert(index, color.ToString()); | ||
| } | ||
| this.Insert(index, value + Environment.NewLine); | ||
| if (color != Color.Reset) | ||
| { | ||
| this.Insert(index, Color.Reset.ToString()); | ||
| } | ||
| } | ||
|
|
||
| public int GetCurrentIndex() | ||
| { | ||
| return this.stringBuilder.Length; | ||
| } | ||
|
|
||
| public void PushIndent(string indent) | ||
| { | ||
| this.indentStack.Add(indent); | ||
| } | ||
|
|
||
| public void PopIndent() | ||
| { | ||
| if (this.indentStack.Count > 0) | ||
| { | ||
| this.indentStack.RemoveAt(this.indentStack.Count - 1); | ||
| } | ||
| } | ||
|
Comment on lines
+120
to
+131
|
||
|
|
||
| public void EnsureNumNewLines(int numNewLines) | ||
| { | ||
| if (this.stringBuilder.Length == 0) | ||
| { | ||
| for (int i = 0; i < numNewLines; i++) | ||
| { | ||
| this.stringBuilder.AppendLine(); | ||
| } | ||
| return; | ||
| } | ||
|
|
||
| string currentText = this.stringBuilder.ToString(); | ||
| int existingNewlines = 0; | ||
|
|
||
| for (int i = currentText.Length - 1; i >= 0 && currentText[i] == '\n'; i--) | ||
| { | ||
| existingNewlines++; | ||
| } | ||
|
|
||
| int remainingNewlines = numNewLines - existingNewlines; | ||
| for (int i = 0; i < remainingNewlines; i++) | ||
| { | ||
| this.stringBuilder.AppendLine(); | ||
| } | ||
| } | ||
|
|
||
| public void Clear() | ||
| { | ||
| this.stringBuilder.Clear(); | ||
| this.colorStack.Clear(); | ||
| this.indentStack.Clear(); | ||
| } | ||
|
|
||
| private void PushColor(Color color) | ||
| { | ||
| this.colorStack.Push(color); | ||
|
|
@@ -101,7 +175,7 @@ private void PopColor() | |
| this.stringBuilder.Append(this.colorStack.Count > 0 ? this.colorStack.Peek() : Color.Reset); | ||
| } | ||
|
|
||
| public class AnsiColorScope: IDisposable | ||
| public class AnsiColorScope : IDisposable | ||
| { | ||
| private readonly ColoredStringBuilder builder; | ||
|
|
||
|
|
@@ -117,4 +191,4 @@ public void Dispose() | |
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
InsertLineinserts multiple strings at the same index, which reverses the intended order (the reset/color codes end up before the inserted line). This will corrupt ANSI coloring for inserted headers. Consider building the full colored line string once (color + value + newline + reset) and inserting it in a singleStringBuilder.Insert, or adjust the indices so insertion order is correct.