Skip to content
35 changes: 33 additions & 2 deletions src/Tools/dotnet-counters/Exporters/ConsoleWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,9 @@ public ObservedTagSet(string tags)
private readonly object _lock = new object();
private readonly Dictionary<string, ObservedProvider> _providers = new Dictionary<string, ObservedProvider>(); // Tracks observed providers and counters.
private const int Indent = 4; // Counter name indent size.
private int _maxNameLength = 40; // Allow room for 40 character counter names by default.
private const int CounterValueLength = 15;

private int _maxNameLength = 60; // Allow room for 60 character counter names by default.
private int _statusRow; // Row # of where we print the status of dotnet-counters
private int _topRow;
private bool _paused = false;
Expand All @@ -69,6 +70,9 @@ public ObservedTagSet(string tags)
private int _maxRow = -1;
private bool _useAnsi = false;

private int _consoleHeight = -1;
private int _consoleWidth = -1;

public ConsoleWriter(bool useAnsi)
{
this._useAnsi = useAnsi;
Expand Down Expand Up @@ -126,9 +130,14 @@ public void AssignRowsAndInitializeDisplay()
{
Clear();

_consoleWidth = Console.WindowWidth;
_consoleHeight = Console.WindowHeight;

int row = Console.CursorTop;
_topRow = row;
Console.WriteLine("Press p to pause, r to resume, q to quit."); row++;

string instructions = "Press p to pause, r to resume, q to quit.";
Console.WriteLine((instructions.Length < _consoleWidth) ? instructions : instructions.Substring(0, _consoleWidth)); row++;
Console.WriteLine($" Status: {GetStatus()}"); _statusRow = row++;
if (_errorText != null)
{
Expand All @@ -140,22 +149,39 @@ public void AssignRowsAndInitializeDisplay()
foreach (ObservedProvider provider in _providers.Values.OrderBy(p => p.KnownProvider == null).ThenBy(p => p.Name)) // Known providers first.
{
Console.WriteLine($"[{provider.Name}]"); row++;
int rowCount = 0;

foreach (ObservedCounter counter in provider.Counters.Values.OrderBy(c => c.DisplayName))
{
if(rowCount >= _consoleHeight - 5)
Copy link
Member

Choose a reason for hiding this comment

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

Is this code trying to prevent us from displaying more counters than we have vertical space available? If so should it be refering to row rather than rowCount? For example I could have 100 providers and each provider has 1 counter in it which means rowCount would always be zero.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You're right. Made the change to reference row:

if(row >= _consoleHeight){
    break;
}

{
break;
}

_maxNameLength = Math.Max(Math.Min(80, _consoleWidth) - (CounterValueLength + Indent + 1), 0);
string name = MakeFixedWidth($"{new string(' ', Indent)}{counter.DisplayName}", Indent + _maxNameLength);
counter.Row = row++;
if (counter.RenderValueInline)
{
Console.WriteLine($"{name} {FormatValue(counter.LastValue)}");
rowCount++;
}
else
{
Console.WriteLine(name);
rowCount++;
foreach (ObservedTagSet tagSet in counter.TagSets.Values.OrderBy(t => t.Tags))
{
if(rowCount >= _consoleHeight - 5)
{
break;
}

_maxNameLength = Math.Max(Math.Min(80, _consoleWidth) - (CounterValueLength + Indent + 1), 0);
string tagName = MakeFixedWidth($"{new string(' ', 2 * Indent)}{tagSet.Tags}", Indent + _maxNameLength);
Console.WriteLine($"{tagName} {FormatValue(tagSet.LastValue)}");
tagSet.Row = row++;
rowCount++;
}
}
}
Expand Down Expand Up @@ -222,6 +248,11 @@ public void CounterPayloadReceived(CounterPayload payload, bool pauseCmdSet)
redraw = true;
}

if(Console.WindowWidth != _consoleWidth || Console.WindowHeight != _consoleHeight)
{
redraw=true;
}

if (redraw)
{
AssignRowsAndInitializeDisplay();
Expand Down