Change ResetCurrentConsoleLine() to use explicit ANSI escape codes (#159)
authorJohn Salem <josalem@microsoft.com>
Wed, 17 Apr 2019 18:16:19 +0000 (11:16 -0700)
committerGitHub <noreply@github.com>
Wed, 17 Apr 2019 18:16:19 +0000 (11:16 -0700)
* Change ResetCurrentConsoleLine() to use explicit ANSI escape codes to rewrite line

* Add System.CommandLine.Rendering and use it to determine when we can use ANSI escape codes.

* Add fallback if vterm is not supported

* Changes in resposne to feedback:
* actually assign prevBufferWidth
* reset cursor to specific row and column instead of just column

src/Tools/dotnet-trace/CommandLine/Commands/CollectCommand.cs
src/Tools/dotnet-trace/dotnet-trace.csproj

index ce5116e520f37e097def8c97b00c678d6aad07c6..d538bbdce677c9c90bbcc5669dba7978820c33c7 100644 (file)
@@ -6,6 +6,7 @@ using Microsoft.Diagnostics.Tools.RuntimeClient;
 using System;
 using System.CommandLine;
 using System.CommandLine.Invocation;
+using System.CommandLine.Rendering;
 using System.Diagnostics;
 using System.IO;
 using System.Threading;
@@ -40,6 +41,7 @@ namespace Microsoft.Diagnostics.Tools.Trace
 
                 ulong sessionId = 0;
                 using (Stream stream = EventPipeClient.CollectTracing(processId, configuration, out sessionId))
+                using (VirtualTerminalMode vTermMode = VirtualTerminalMode.TryEnable())
                 {
                     if (sessionId == 0)
                     {
@@ -51,7 +53,8 @@ namespace Microsoft.Diagnostics.Tools.Trace
                         using (var fs = new FileStream(output, FileMode.Create, FileAccess.Write))
                         {
                             Console.Out.WriteLine($"Recording tracing session to: {fs.Name}");
-                            Console.Out.WriteLine($"  Session Id: 0x{sessionId:X16}");
+                            Console.Out.WriteLine($"\tSession Id: 0x{sessionId:X16}");
+                            lineToClear = Console.CursorTop;
 
                             while (true)
                             {
@@ -61,8 +64,8 @@ namespace Microsoft.Diagnostics.Tools.Trace
                                     break;
                                 fs.Write(buffer, 0, nBytesRead);
 
-                                ResetCurrentConsoleLine();
-                                Console.Out.Write($"  Recording trace {GetSize(fs.Length)}");
+                                ResetCurrentConsoleLine(vTermMode.IsEnabled);
+                                Console.Out.Write($"\tRecording trace {GetSize(fs.Length)}");
 
                                 Debug.WriteLine($"PACKET: {Convert.ToBase64String(buffer, 0, nBytesRead)} (bytes {nBytesRead})");
                             }
@@ -97,12 +100,29 @@ namespace Microsoft.Diagnostics.Tools.Trace
             }
         }
 
-        private static void ResetCurrentConsoleLine()
+        private static int prevBufferWidth = 0;
+        private static string clearLineString = "";
+        private static int lineToClear = 0;
+        private static void ResetCurrentConsoleLine(bool isVTerm)
         {
-            int currentCursorTop = Console.CursorTop;
-            Console.SetCursorPosition(0, Console.CursorTop);
-            Console.Out.Write(new string(' ', Console.WindowWidth));
-            Console.SetCursorPosition(0, currentCursorTop);
+            if (isVTerm)
+            {
+                // ANSI escape codes:
+                //  [2K => clear current line
+                //  [{lineToClear};0H => move cursor to column 0 of row `lineToClear`
+                Console.Out.Write($"\u001b[2K\u001b[{lineToClear};0H");
+            }
+            else
+            {
+                if (prevBufferWidth != Console.BufferWidth)
+                {
+                    prevBufferWidth = Console.BufferWidth;
+                    clearLineString = new string(' ', Console.BufferWidth - 1);
+                }
+                Console.SetCursorPosition(0,lineToClear);
+                Console.Out.Write(clearLineString);
+                Console.SetCursorPosition(0,lineToClear);
+            }
         }
 
         private static string GetSize(long length)
index 14c9d64179178503f0045efcd75eb9cf0048d788..6794d14c6e7d6a415ab7b6b522d055c66e6caef2 100644 (file)
@@ -16,6 +16,7 @@
 
   <ItemGroup>
     <PackageReference Include="System.CommandLine.Experimental" Version="0.2.0-alpha.19179.1" />
+    <PackageReference Include="System.CommandLine.Rendering" Version="0.2.0-alpha.19179.1" />
   </ItemGroup>
 
   <ItemGroup>