From 6ba94ff8a49f3e462ab526d648906c8ee5555ea3 Mon Sep 17 00:00:00 2001 From: Cristian Petruta Date: Thu, 13 Oct 2022 14:04:30 -0700 Subject: [PATCH] Fix exceptions in the upsync tool raised while running with redirected console output. Console cursor location cannot be modified while running with console redirection; in such cases, the tool will print a new line instead of clearing the current line. --- src/tools/upsync/ContentSync.cs | 14 +++++++++++++- src/tools/upsync/MetadataSync.cs | 16 ++++++++++++++-- src/tools/upsync/Program.cs | 20 +++++++++++++++++--- 3 files changed, 44 insertions(+), 6 deletions(-) diff --git a/src/tools/upsync/ContentSync.cs b/src/tools/upsync/ContentSync.cs index 36a2792..cb59e3a 100644 --- a/src/tools/upsync/ContentSync.cs +++ b/src/tools/upsync/ContentSync.cs @@ -87,6 +87,18 @@ private static List GetAllUpdateFiles(IMetadataStore metadataSourc static string ContentSyncLastFileDigest = ""; + private static void UpdateConsoleForMessageRefresh() + { + if (!Console.IsOutputRedirected) + { + Console.CursorLeft = 0; + } + else + { + Console.WriteLine(); + } + } + private static void ContentStore_Progress(object sender, ObjectModel.ContentOperationProgress e) { if (e.File.Digest.DigestBase64 != ContentSyncLastFileDigest) @@ -98,7 +110,7 @@ private static void ContentStore_Progress(object sender, ObjectModel.ContentOper switch(e.CurrentOperation) { case ObjectModel.PackagesOperationType.DownloadFileProgress: - Console.CursorLeft = 0; + UpdateConsoleForMessageRefresh(); Console.Write("Sync'ing update content [{0}]: {1:000.00}%", e.Maximum, e.PercentDone); break; } diff --git a/src/tools/upsync/MetadataSync.cs b/src/tools/upsync/MetadataSync.cs index bc1df58..bd82a54 100644 --- a/src/tools/upsync/MetadataSync.cs +++ b/src/tools/upsync/MetadataSync.cs @@ -201,6 +201,18 @@ private static void FetchMicrosoftUpdatePackages(FetchPackagesOptions options, I } } + private static void UpdateConsoleForMessageRefresh() + { + if (!Console.IsOutputRedirected) + { + Console.CursorLeft = 0; + } + else + { + Console.WriteLine(); + } + } + /// /// Handles progress notifications from a metadata query on an upstream server. /// Prints progress information to the console @@ -234,13 +246,13 @@ private static void Server_MetadataQueryProgress(object sender, MetadataQueryPro break; case MetadataQueryStage.GetUpdateMetadataEnd: - Console.CursorLeft = 0; + UpdateConsoleForMessageRefresh(); Console.Write("Retrieving updates metadata [{0}]: 100.00%", e.Maximum); ConsoleOutput.WriteGreen(" Done!"); break; case MetadataQueryStage.GetUpdateMetadataProgress: - Console.CursorLeft = 0; + UpdateConsoleForMessageRefresh(); Console.Write("Retrieving updates metadata [{0}]: {1:000.00}%", e.Maximum, e.PercentDone); break; } diff --git a/src/tools/upsync/Program.cs b/src/tools/upsync/Program.cs index b75779a..e517ca2 100644 --- a/src/tools/upsync/Program.cs +++ b/src/tools/upsync/Program.cs @@ -49,11 +49,23 @@ static void Main(string[] args) private static readonly object ConsoleWriteLock = new(); + private static void UpdateConsoleForMessageRefresh() + { + if (!Console.IsOutputRedirected) + { + Console.CursorLeft = 0; + } + else + { + Console.WriteLine(); + } + } + public static void OnPackageCopyProgress(object sender, PackageStoreEventArgs e) { lock (ConsoleWriteLock) { - Console.CursorLeft = 0; + UpdateConsoleForMessageRefresh(); if (e.Total == 0) { @@ -70,7 +82,7 @@ public static void OnOpenProgress(object sender, PackageStoreEventArgs e) { lock (ConsoleWriteLock) { - Console.CursorLeft = 0; + UpdateConsoleForMessageRefresh(); if (e.Total == 0) { @@ -87,7 +99,9 @@ public static void OnPackageIndexingProgress(object sender, PackageStoreEventArg { lock(ProgressLock) { - Console.CursorLeft = 0; + UpdateConsoleForMessageRefresh(); + + if (e.Total == 0) { Console.Write($"Indexing {e.Total} package(s)");