added option to use ANSI escape codes to write to the Console fixes #2536 (#2656)
authormikelle-rogers <45022607+mikelle-rogers@users.noreply.github.com>
Mon, 11 Oct 2021 16:57:38 +0000 (10:57 -0600)
committerGitHub <noreply@github.com>
Mon, 11 Oct 2021 16:57:38 +0000 (16:57 +0000)
* added option to use ANSI escape codes to write to the Console

* fixed for better style

src/Tools/dotnet-counters/CounterMonitor.cs
src/Tools/dotnet-counters/Exporters/ConsoleWriter.cs
src/Tools/dotnet-counters/dotnet-counters.csproj

index 153f54b780f3b17d09f8ff1a0816f7ffd3dee6a7..45a2e2f265a13e551bc8b9ffed21a3a7338b3b99 100644 (file)
@@ -6,10 +6,13 @@ using Microsoft.Diagnostics.NETCore.Client;
 using Microsoft.Diagnostics.Tools.Counters.Exporters;
 using Microsoft.Diagnostics.Tracing;
 using Microsoft.Internal.Common.Utils;
+using Microsoft.Tools.Common;
 using System;
 using System.Collections.Generic;
 using System.CommandLine;
 using System.CommandLine.IO;
+using System.CommandLine.Binding;
+using System.CommandLine.Rendering;
 using System.Diagnostics;
 using System.Diagnostics.Tracing;
 using System.IO;
@@ -468,7 +471,9 @@ namespace Microsoft.Diagnostics.Tools.Counters
 
                 DiagnosticsClientBuilder builder = new DiagnosticsClientBuilder("dotnet-counters", 10);
                 using (DiagnosticsClientHolder holder = await builder.Build(ct, _processId, diagnosticPort, showChildIO: false, printLaunchCommand: false))
+                using (VirtualTerminalMode vTerm = VirtualTerminalMode.TryEnable())
                 {
+                    bool useAnsi = vTerm.IsEnabled;
                     if (holder == null)
                     {
                         return ReturnCode.Ok;
@@ -483,7 +488,7 @@ namespace Microsoft.Diagnostics.Tools.Counters
                         _interval = refreshInterval;
                         _maxHistograms = maxHistograms;
                         _maxTimeSeries = maxTimeSeries;
-                        _renderer = new ConsoleWriter();
+                        _renderer = new ConsoleWriter(useAnsi);
                         _diagnosticsClient = holder.Client;
                         _resumeRuntime = resumeRuntime;
                         int ret = await Start();
index de578d50351a0a3b6a77e593270fbbed024dd265..5ee820a96218e359964e6095cfcef3054b125d7f 100644 (file)
@@ -66,6 +66,12 @@ namespace Microsoft.Diagnostics.Tools.Counters.Exporters
         private string _errorText = null;
 
         private int maxRow = -1;
+        private bool useAnsi = false;
+
+        public ConsoleWriter(bool useAnsi) 
+        {
+            this.useAnsi = useAnsi;
+        }
 
         public void Initialize()
         {
@@ -83,9 +89,32 @@ namespace Microsoft.Diagnostics.Tools.Counters.Exporters
             AssignRowsAndInitializeDisplay();
         }
 
+        private void SetCursorPosition(int col, int row) 
+        {
+            if (this.useAnsi) 
+            {
+                Console.Write($"\u001b[{row + 1};{col + 1}H");
+            }
+            else 
+            {
+                Console.SetCursorPosition(col, row);
+            }
+        }
+
+        private void Clear() 
+        {
+            if (this.useAnsi) 
+            {
+                Console.Write($"\u001b[H\u001b[J");
+            }
+            else 
+            {
+                Console.Clear();
+            }
+        }
         private void UpdateStatus()
         {
-            Console.SetCursorPosition(0, STATUS_ROW);
+            SetCursorPosition(0, STATUS_ROW);
             Console.Write($"    Status: {GetStatus()}{new string(' ', 40)}"); // Write enough blanks to clear previous status.
         }
 
@@ -94,7 +123,8 @@ namespace Microsoft.Diagnostics.Tools.Counters.Exporters
         /// <summary>Clears display and writes out category and counter name layout.</summary>
         public void AssignRowsAndInitializeDisplay()
         {
-            Console.Clear();
+            Clear();
+            
             int row = Console.CursorTop;
             Console.WriteLine("Press p to pause, r to resume, q to quit."); row++;
             Console.WriteLine($"    Status: {GetStatus()}");                STATUS_ROW = row++;
@@ -196,7 +226,7 @@ namespace Microsoft.Diagnostics.Tools.Counters.Exporters
                 }
 
                 int row = counter.RenderValueInline ? counter.Row : tagSet.Row;
-                Console.SetCursorPosition(Indent + maxNameLength + 1, row);
+                SetCursorPosition(Indent + maxNameLength + 1, row);
                 Console.Write(FormatValue(payload.Value));
             }
         }
@@ -279,7 +309,7 @@ namespace Microsoft.Diagnostics.Tools.Counters.Exporters
 
                     if (row > -1)
                     {
-                        Console.SetCursorPosition(0, row);
+                        SetCursorPosition(0, row);
                         Console.WriteLine();
                     }
                 }
index 654bb034c6f36a7d909fb7b94aa3c0e8bf614078..fb74a948c9aa148f8e0247357dfc07425a16cf3f 100644 (file)
@@ -25,6 +25,7 @@
   <ItemGroup>
     <PackageReference Include="Microsoft.Diagnostics.Tracing.TraceEvent" Version="$(MicrosoftDiagnosticsTracingTraceEventVersion)" GeneratePathProperty="true" />
     <PackageReference Include="System.CommandLine" Version="$(SystemCommandLineVersion)" />
+    <PackageReference Include="System.CommandLine.Rendering" Version="$(SystemCommandLineRenderingVersion)" />
   </ItemGroup>
 
   <ItemGroup>