Skip to content
Merged
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
95 changes: 73 additions & 22 deletions src/Tools/dotnet-counters/Exporters/ConsoleWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,13 @@ public ObservedProvider(string name)
public readonly CounterProvider KnownProvider;
}

private interface ICounterRow
{
int Row { get; set; }
}

/// <summary>Information about an observed counter.</summary>
private class ObservedCounter
private class ObservedCounter : ICounterRow
{
public ObservedCounter(string displayName) => DisplayName = displayName;
public string DisplayName { get; } // Display name for this counter.
Expand All @@ -41,7 +46,7 @@ private class ObservedCounter
public double LastValue { get; set; }
}

private class ObservedTagSet
private class ObservedTagSet : ICounterRow
{
public ObservedTagSet(string tags)
{
Expand Down Expand Up @@ -128,6 +133,19 @@ public void AssignRowsAndInitializeDisplay()
{
Clear();

// clear row data on all counters
foreach (ObservedProvider provider in _providers.Values)
{
foreach (ObservedCounter counter in provider.Counters.Values)
{
counter.Row = -1;
foreach (ObservedTagSet tagSet in counter.TagSets.Values)
{
tagSet.Row = -1;
}
}
}

_consoleWidth = Console.WindowWidth;
_consoleHeight = Console.WindowHeight;
_maxNameLength = Math.Max(Math.Min(80, _consoleWidth) - (CounterValueLength + Indent + 1), 0); // Truncate the name to prevent line wrapping as long as the console width is >= CounterValueLength + Indent + 1 characters
Expand All @@ -144,38 +162,67 @@ public void AssignRowsAndInitializeDisplay()
Console.WriteLine(_errorText);
row += GetLineWrappedLines(_errorText);
}
Console.WriteLine(); row++; // Blank line.

foreach (ObservedProvider provider in _providers.Values.OrderBy(p => p.KnownProvider == null).ThenBy(p => p.Name)) // Known providers first.
bool RenderRow(ref int row, string lineOutput = null, ICounterRow counterRow = null)
{
Console.WriteLine($"[{provider.Name}]"); row++;
if (row >= _consoleHeight + _topRow) // prevents from displaying more counters than vertical space available
{
return false;
}

if (lineOutput != null)
{
Console.Write(lineOutput);
}

foreach (ObservedCounter counter in provider.Counters.Values.OrderBy(c => c.DisplayName))
if (row < _consoleHeight + _topRow - 1) // prevents screen from scrolling due to newline on last line of console
{
Console.WriteLine();
}

if (counterRow != null)
{
counterRow.Row = row;
}

row++;

string name = MakeFixedWidth($"{new string(' ', Indent)}{counter.DisplayName}", Indent + _maxNameLength);
counter.Row = row++;
if (counter.RenderValueInline)
return true;
}

if (RenderRow(ref row)) // Blank line.
{
foreach (ObservedProvider provider in _providers.Values.OrderBy(p => p.KnownProvider == null).ThenBy(p => p.Name)) // Known providers first.
{
if (!RenderRow(ref row, $"[{provider.Name}]"))
{
if (row >= _consoleHeight) // prevents from displaying more counters than vertical space available
{
break;
}
Console.WriteLine($"{name} {FormatValue(counter.LastValue)}");
break;
}
else

foreach (ObservedCounter counter in provider.Counters.Values.OrderBy(c => c.DisplayName))
{
Console.WriteLine(name);
foreach (ObservedTagSet tagSet in counter.TagSets.Values.OrderBy(t => t.Tags))
string name = MakeFixedWidth($"{new string(' ', Indent)}{counter.DisplayName}", Indent + _maxNameLength);
if (counter.RenderValueInline)
{
if (row >= _consoleHeight)
if (!RenderRow(ref row, $"{name} {FormatValue(counter.LastValue)}", counter))
{
break;
}

string tagName = MakeFixedWidth($"{new string(' ', 2 * Indent)}{tagSet.Tags}", Indent + _maxNameLength);
Console.WriteLine($"{tagName} {FormatValue(tagSet.LastValue)}");
tagSet.Row = row++;
}
else
{
if (!RenderRow(ref row, name, counter))
{
break;
}
foreach (ObservedTagSet tagSet in counter.TagSets.Values.OrderBy(t => t.Tags))
{
string tagName = MakeFixedWidth($"{new string(' ', 2 * Indent)}{tagSet.Tags}", Indent + _maxNameLength);
if (!RenderRow(ref row, $"{tagName} {FormatValue(tagSet.LastValue)}", tagSet))
{
break;
}
}
}
}
}
Expand Down Expand Up @@ -253,6 +300,10 @@ public void CounterPayloadReceived(CounterPayload payload, bool pauseCmdSet)
}

int row = counter.RenderValueInline ? counter.Row : tagSet.Row;
if (row < 0)
{
return;
}
SetCursorPosition(Indent + _maxNameLength + 1, row);
Console.Write(FormatValue(payload.Value));
}
Expand Down