Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 76 additions & 2 deletions src/Resources/ResourceManager/Formatters/ColoredStringBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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());
}
Comment on lines +104 to +112
Copy link

Copilot AI Mar 20, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

InsertLine inserts 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 single StringBuilder.Insert, or adjust the indices so insertion order is correct.

Suggested change
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());
}
string line;
if (color != Color.Reset)
{
line = color.ToString() + value + Environment.NewLine + Color.Reset.ToString();
}
else
{
line = value + Environment.NewLine;
}
this.Insert(index, line);

Copilot uses AI. Check for mistakes.
}

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
Copy link

Copilot AI Mar 20, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PushIndent/PopIndent maintain an indentStack, but none of the Append* methods apply the current indent when writing new lines. As a result, DeploymentStackWhatIfFormatter’s indentation calls have no effect. Please wire indentation into AppendLine/newline handling (or remove the indent stack if not intended).

Copilot uses AI. Check for mistakes.

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);
Expand All @@ -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;

Expand All @@ -117,4 +191,4 @@ public void Dispose()
}
}
}
}
}
Loading
Loading