Delay progress bar in Copy-Item and Remove-Item cmdlets (#24013)

This commit is contained in:
SpyGod 2024-08-19 20:18:58 -07:00 committed by GitHub
parent 07f0798026
commit be59e5e80e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -3115,16 +3115,19 @@ namespace Microsoft.PowerShell.Commands
{
_removedFiles++;
_removedBytes += fileBytesSize;
double speed = _removedBytes / 1024 / 1024 / _removeStopwatch.Elapsed.TotalSeconds;
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);
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),
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);
}
}
}
}
@ -3994,16 +3997,19 @@ namespace Microsoft.PowerShell.Commands
{
_copiedFiles++;
_copiedBytes += file.Length;
double speed = (double)(_copiedBytes / 1024 / 1024) / _copyStopwatch.Elapsed.TotalSeconds;
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);
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),
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);
}
}
}
else
@ -4945,6 +4951,7 @@ namespace Microsoft.PowerShell.Commands
private long _removedFiles;
private readonly Stopwatch _removeStopwatch = new();
private const double ProgressBarDurationThreshold = 2.0;
#endregion CopyItem
#endregion ContainerCmdletProvider members