From bb51b3e8ad2b9889abdb5fb40730fd358f905d0d Mon Sep 17 00:00:00 2001 From: SpyGod Date: Wed, 3 Jul 2024 22:30:29 +0200 Subject: [PATCH 1/7] removing progressbar for less then 5 files I intend to remove the progressbar for less then or equal to 5 file, to make it less anoying TODO: Check for errors when running Remove-Item --- .../namespaces/FileSystemProvider.cs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/System.Management.Automation/namespaces/FileSystemProvider.cs b/src/System.Management.Automation/namespaces/FileSystemProvider.cs index 63bd7f5f157..b2057dc07d9 100644 --- a/src/System.Management.Automation/namespaces/FileSystemProvider.cs +++ b/src/System.Management.Automation/namespaces/FileSystemProvider.cs @@ -2878,7 +2878,7 @@ protected override void RemoveItem(string path, bool recurse) if (Context != null && Context.ExecutionContext.SessionState.PSVariable.Get(SpecialVariables.ProgressPreferenceVarPath.UserPath).Value is ActionPreference progressPreference - && progressPreference == ActionPreference.Continue) + && progressPreference == ActionPreference.Continue && _totalFiles >= 5) { { Task.Run(() => @@ -2953,8 +2953,9 @@ protected override void RemoveItem(string path, bool recurse) } } - if (Stopping || _removedFiles == _totalFiles) + if (_totalFiles >= 5 && (Stopping || _removedFiles == _totalFiles)) { + _removeStopwatch.Stop(); var progress = new ProgressRecord(REMOVE_FILE_ACTIVITY_ID, " ", " ") { From 2ade18e7296fe4b38f8caa13d56406c7950548d1 Mon Sep 17 00:00:00 2001 From: SpyGod Date: Thu, 4 Jul 2024 19:48:38 +0200 Subject: [PATCH 2/7] Changed the progress bar to be 600ms timed Made it so that the progress bar for removing only appears after the _removeStopwatch reaches 600ms --- .../namespaces/FileSystemProvider.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/System.Management.Automation/namespaces/FileSystemProvider.cs b/src/System.Management.Automation/namespaces/FileSystemProvider.cs index b2057dc07d9..810284b2d65 100644 --- a/src/System.Management.Automation/namespaces/FileSystemProvider.cs +++ b/src/System.Management.Automation/namespaces/FileSystemProvider.cs @@ -2878,7 +2878,7 @@ protected override void RemoveItem(string path, bool recurse) if (Context != null && Context.ExecutionContext.SessionState.PSVariable.Get(SpecialVariables.ProgressPreferenceVarPath.UserPath).Value is ActionPreference progressPreference - && progressPreference == ActionPreference.Continue && _totalFiles >= 5) + && progressPreference == ActionPreference.Continue) { { Task.Run(() => @@ -2953,7 +2953,7 @@ protected override void RemoveItem(string path, bool recurse) } } - if (_totalFiles >= 5 && (Stopping || _removedFiles == _totalFiles)) + if (Stopping || _removedFiles == _totalFiles) { _removeStopwatch.Stop(); @@ -3112,7 +3112,7 @@ void WriteErrorHelper(Exception exception) RemoveFileSystemItem(file, force); } - if (_totalFiles > 0) + if (_removeStopwatch.Elapsed.TotalSeconds > 0.6 && _totalFiles > 0) { _removedFiles++; _removedBytes += fileBytesSize; From 0cc8a9a0096a14ef91df478c84a07c46930c27fd Mon Sep 17 00:00:00 2001 From: SpyGod Date: Thu, 4 Jul 2024 20:27:45 +0200 Subject: [PATCH 3/7] Made both remove and copy display the progress bar after 200ms Made the copy and remove command display the Progress bar only after the process is lasting more then 200ms --- .../namespaces/FileSystemProvider.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/System.Management.Automation/namespaces/FileSystemProvider.cs b/src/System.Management.Automation/namespaces/FileSystemProvider.cs index 810284b2d65..8207e5c6688 100644 --- a/src/System.Management.Automation/namespaces/FileSystemProvider.cs +++ b/src/System.Management.Automation/namespaces/FileSystemProvider.cs @@ -3112,7 +3112,7 @@ void WriteErrorHelper(Exception exception) RemoveFileSystemItem(file, force); } - if (_removeStopwatch.Elapsed.TotalSeconds > 0.6 && _totalFiles > 0) + if (_removeStopwatch.Elapsed.TotalSeconds > 0.2 && _totalFiles > 0) { _removedFiles++; _removedBytes += fileBytesSize; @@ -3991,7 +3991,7 @@ private void CopyFileInfoItem(FileInfo file, string destinationPath, bool force, FileInfo result = new FileInfo(destinationPath); WriteItemObject(result, destinationPath, false); - if (_totalFiles > 0) + if (_copyStopwatch.Elapsed.TotalSeconds > 0.2 && _totalFiles > 0) { _copiedFiles++; _copiedBytes += file.Length; From d26cbb7edb38ef0a9d128f1926814e6f98ef4bf1 Mon Sep 17 00:00:00 2001 From: SpyGod Date: Fri, 5 Jul 2024 14:29:04 +0200 Subject: [PATCH 4/7] Changed it so that the Progressbar Instance is only created after 3 seconds Made it so that instead of skipping it all together, only the instance will not be shown until 3 seconds elapse, the calculations will still continue --- .../namespaces/FileSystemProvider.cs | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/System.Management.Automation/namespaces/FileSystemProvider.cs b/src/System.Management.Automation/namespaces/FileSystemProvider.cs index 8207e5c6688..344bbf9412d 100644 --- a/src/System.Management.Automation/namespaces/FileSystemProvider.cs +++ b/src/System.Management.Automation/namespaces/FileSystemProvider.cs @@ -3112,11 +3112,14 @@ void WriteErrorHelper(Exception exception) RemoveFileSystemItem(file, force); } - if (_removeStopwatch.Elapsed.TotalSeconds > 0.2 && _totalFiles > 0) + if (_totalFiles > 0) { _removedFiles++; _removedBytes += fileBytesSize; double speed = _removedBytes / 1024 / 1024 / _removeStopwatch.Elapsed.TotalSeconds; + + if (_removeStopwatch.Elapsed.TotalSeconds > progressBarDurationThreshold) + { var progress = new ProgressRecord( REMOVE_FILE_ACTIVITY_ID, StringUtil.Format(FileSystemProviderStrings.RemovingLocalFileActivity, _removedFiles, _totalFiles), @@ -3126,6 +3129,7 @@ void WriteErrorHelper(Exception exception) progress.PercentComplete = percentComplete; progress.RecordType = ProgressRecordType.Processing; WriteProgress(progress); + } } } } @@ -3991,11 +3995,14 @@ private void CopyFileInfoItem(FileInfo file, string destinationPath, bool force, FileInfo result = new FileInfo(destinationPath); WriteItemObject(result, destinationPath, false); - if (_copyStopwatch.Elapsed.TotalSeconds > 0.2 && _totalFiles > 0) + if (_totalFiles > 0) { _copiedFiles++; _copiedBytes += file.Length; double speed = (double)(_copiedBytes / 1024 / 1024) / _copyStopwatch.Elapsed.TotalSeconds; + + if (_copyStopwatch.Elapsed.TotalSeconds > progressBarDurationThreshold) + { var progress = new ProgressRecord( COPY_FILE_ACTIVITY_ID, StringUtil.Format(FileSystemProviderStrings.CopyingLocalFileActivity, _copiedFiles, _totalFiles), @@ -4005,6 +4012,7 @@ private void CopyFileInfoItem(FileInfo file, string destinationPath, bool force, progress.PercentComplete = percentComplete; progress.RecordType = ProgressRecordType.Processing; WriteProgress(progress); + } } } else @@ -4945,7 +4953,8 @@ private bool PathIsReservedDeviceName(string destinationPath, string errorId) private long _removedBytes; private long _removedFiles; private readonly Stopwatch _removeStopwatch = new(); - + + private const double progressBarDurationThreshold = 3.0; #endregion CopyItem #endregion ContainerCmdletProvider members From 66d9ae0b92d79c9d0339dae19ad3d5949cd7f896 Mon Sep 17 00:00:00 2001 From: SpyGod Date: Fri, 5 Jul 2024 19:41:29 +0200 Subject: [PATCH 5/7] Fixed Indentation as Guidelines Say and Switched Const to PascalCase Fixed Indentation for IF as Guidelines Say and Switched the Duration const to PascalCase --- .../namespaces/FileSystemProvider.cs | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/System.Management.Automation/namespaces/FileSystemProvider.cs b/src/System.Management.Automation/namespaces/FileSystemProvider.cs index 344bbf9412d..08acd3cd489 100644 --- a/src/System.Management.Automation/namespaces/FileSystemProvider.cs +++ b/src/System.Management.Automation/namespaces/FileSystemProvider.cs @@ -2955,7 +2955,6 @@ protected override void RemoveItem(string path, bool recurse) if (Stopping || _removedFiles == _totalFiles) { - _removeStopwatch.Stop(); var progress = new ProgressRecord(REMOVE_FILE_ACTIVITY_ID, " ", " ") { @@ -3117,8 +3116,7 @@ void WriteErrorHelper(Exception exception) _removedFiles++; _removedBytes += fileBytesSize; double speed = _removedBytes / 1024 / 1024 / _removeStopwatch.Elapsed.TotalSeconds; - - if (_removeStopwatch.Elapsed.TotalSeconds > progressBarDurationThreshold) + if (_removeStopwatch.Elapsed.TotalSeconds > ProgressBarDurationThreshold) { var progress = new ProgressRecord( REMOVE_FILE_ACTIVITY_ID, @@ -4000,8 +3998,7 @@ private void CopyFileInfoItem(FileInfo file, string destinationPath, bool force, _copiedFiles++; _copiedBytes += file.Length; double speed = (double)(_copiedBytes / 1024 / 1024) / _copyStopwatch.Elapsed.TotalSeconds; - - if (_copyStopwatch.Elapsed.TotalSeconds > progressBarDurationThreshold) + if (_copyStopwatch.Elapsed.TotalSeconds > ProgressBarDurationThreshold) { var progress = new ProgressRecord( COPY_FILE_ACTIVITY_ID, @@ -4954,7 +4951,7 @@ private bool PathIsReservedDeviceName(string destinationPath, string errorId) private long _removedFiles; private readonly Stopwatch _removeStopwatch = new(); - private const double progressBarDurationThreshold = 3.0; + private const double ProgressBarDurationThreshold = 3.0; #endregion CopyItem #endregion ContainerCmdletProvider members From 115e1b8b18b6b02fe7b2a3aa2d7b84ee23ed40c5 Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Tue, 13 Aug 2024 11:32:20 -0700 Subject: [PATCH 6/7] fix indentation --- .../namespaces/FileSystemProvider.cs | 38 +++++++++---------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/src/System.Management.Automation/namespaces/FileSystemProvider.cs b/src/System.Management.Automation/namespaces/FileSystemProvider.cs index 08acd3cd489..6027a91af1a 100644 --- a/src/System.Management.Automation/namespaces/FileSystemProvider.cs +++ b/src/System.Management.Automation/namespaces/FileSystemProvider.cs @@ -3118,15 +3118,15 @@ void WriteErrorHelper(Exception exception) double speed = _removedBytes / 1024 / 1024 / _removeStopwatch.Elapsed.TotalSeconds; if (_removeStopwatch.Elapsed.TotalSeconds > ProgressBarDurationThreshold) { - var progress = new ProgressRecord( - REMOVE_FILE_ACTIVITY_ID, - StringUtil.Format(FileSystemProviderStrings.RemovingLocalFileActivity, _removedFiles, _totalFiles), - StringUtil.Format(FileSystemProviderStrings.RemovingLocalBytesStatus, Utils.DisplayHumanReadableFileSize(_removedBytes), Utils.DisplayHumanReadableFileSize(_totalBytes), speed) - ); - var percentComplete = _totalBytes != 0 ? (int)Math.Min(_removedBytes * 100 / _totalBytes, 100) : 100; - progress.PercentComplete = percentComplete; - progress.RecordType = ProgressRecordType.Processing; - WriteProgress(progress); + var progress = new ProgressRecord( + REMOVE_FILE_ACTIVITY_ID, + StringUtil.Format(FileSystemProviderStrings.RemovingLocalFileActivity, _removedFiles, _totalFiles), + StringUtil.Format(FileSystemProviderStrings.RemovingLocalBytesStatus, Utils.DisplayHumanReadableFileSize(_removedBytes), Utils.DisplayHumanReadableFileSize(_totalBytes), speed) + ); + var percentComplete = _totalBytes != 0 ? (int)Math.Min(_removedBytes * 100 / _totalBytes, 100) : 100; + progress.PercentComplete = percentComplete; + progress.RecordType = ProgressRecordType.Processing; + WriteProgress(progress); } } } @@ -4000,15 +4000,15 @@ private void CopyFileInfoItem(FileInfo file, string destinationPath, bool force, double speed = (double)(_copiedBytes / 1024 / 1024) / _copyStopwatch.Elapsed.TotalSeconds; if (_copyStopwatch.Elapsed.TotalSeconds > ProgressBarDurationThreshold) { - var progress = new ProgressRecord( - COPY_FILE_ACTIVITY_ID, - StringUtil.Format(FileSystemProviderStrings.CopyingLocalFileActivity, _copiedFiles, _totalFiles), - StringUtil.Format(FileSystemProviderStrings.CopyingLocalBytesStatus, Utils.DisplayHumanReadableFileSize(_copiedBytes), Utils.DisplayHumanReadableFileSize(_totalBytes), speed) - ); - var percentComplete = _totalBytes != 0 ? (int)Math.Min(_copiedBytes * 100 / _totalBytes, 100) : 100; - progress.PercentComplete = percentComplete; - progress.RecordType = ProgressRecordType.Processing; - WriteProgress(progress); + var progress = new ProgressRecord( + COPY_FILE_ACTIVITY_ID, + StringUtil.Format(FileSystemProviderStrings.CopyingLocalFileActivity, _copiedFiles, _totalFiles), + StringUtil.Format(FileSystemProviderStrings.CopyingLocalBytesStatus, Utils.DisplayHumanReadableFileSize(_copiedBytes), Utils.DisplayHumanReadableFileSize(_totalBytes), speed) + ); + var percentComplete = _totalBytes != 0 ? (int)Math.Min(_copiedBytes * 100 / _totalBytes, 100) : 100; + progress.PercentComplete = percentComplete; + progress.RecordType = ProgressRecordType.Processing; + WriteProgress(progress); } } } @@ -4950,7 +4950,7 @@ private bool PathIsReservedDeviceName(string destinationPath, string errorId) private long _removedBytes; private long _removedFiles; private readonly Stopwatch _removeStopwatch = new(); - + private const double ProgressBarDurationThreshold = 3.0; #endregion CopyItem From 4b7d2a7915bcdc37ca0cae2cdefd8fc3fb941c25 Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Mon, 19 Aug 2024 12:47:22 -0700 Subject: [PATCH 7/7] address Ilya's feedback and reduce threshold to 2 secs --- .../namespaces/FileSystemProvider.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/System.Management.Automation/namespaces/FileSystemProvider.cs b/src/System.Management.Automation/namespaces/FileSystemProvider.cs index 6027a91af1a..9e25e2fae02 100644 --- a/src/System.Management.Automation/namespaces/FileSystemProvider.cs +++ b/src/System.Management.Automation/namespaces/FileSystemProvider.cs @@ -3115,9 +3115,9 @@ void WriteErrorHelper(Exception exception) { _removedFiles++; _removedBytes += fileBytesSize; - double speed = _removedBytes / 1024 / 1024 / _removeStopwatch.Elapsed.TotalSeconds; if (_removeStopwatch.Elapsed.TotalSeconds > ProgressBarDurationThreshold) { + double speed = _removedBytes / 1024 / 1024 / _removeStopwatch.Elapsed.TotalSeconds; var progress = new ProgressRecord( REMOVE_FILE_ACTIVITY_ID, StringUtil.Format(FileSystemProviderStrings.RemovingLocalFileActivity, _removedFiles, _totalFiles), @@ -3997,9 +3997,9 @@ private void CopyFileInfoItem(FileInfo file, string destinationPath, bool force, { _copiedFiles++; _copiedBytes += file.Length; - double speed = (double)(_copiedBytes / 1024 / 1024) / _copyStopwatch.Elapsed.TotalSeconds; if (_copyStopwatch.Elapsed.TotalSeconds > ProgressBarDurationThreshold) { + double speed = (double)(_copiedBytes / 1024 / 1024) / _copyStopwatch.Elapsed.TotalSeconds; var progress = new ProgressRecord( COPY_FILE_ACTIVITY_ID, StringUtil.Format(FileSystemProviderStrings.CopyingLocalFileActivity, _copiedFiles, _totalFiles), @@ -4951,7 +4951,7 @@ private bool PathIsReservedDeviceName(string destinationPath, string errorId) private long _removedFiles; private readonly Stopwatch _removeStopwatch = new(); - private const double ProgressBarDurationThreshold = 3.0; + private const double ProgressBarDurationThreshold = 2.0; #endregion CopyItem #endregion ContainerCmdletProvider members