优化单文件大小进度显示

This commit is contained in:
nilaoda 2022-12-03 23:59:47 +08:00
parent 72ea3c996f
commit 17f5acde5a
2 changed files with 23 additions and 7 deletions

View File

@ -1,4 +1,5 @@
using N_m3u8DL_RE.Common.Util;
using N_m3u8DL_RE.Entity;
using Spectre.Console;
using Spectre.Console.Rendering;
using System;
@ -12,25 +13,40 @@ namespace N_m3u8DL_RE.Column
{
internal class DownloadStatusColumn : ProgressColumn
{
protected override bool NoWrap => true;
private ConcurrentDictionary<int, long> DownloadedSizeDic = new();
private ConcurrentDictionary<int, SpeedContainer> SpeedContainerDic { get; set; }
public Style MyStyle { get; set; } = new Style(foreground: Color.DarkCyan);
public Style FinishedStyle { get; set; } = new Style(foreground: Color.Green);
public DownloadStatusColumn(ConcurrentDictionary<int, long> downloadedSizeDic)
public DownloadStatusColumn(ConcurrentDictionary<int, long> downloadedSizeDic, ConcurrentDictionary<int, SpeedContainer> speedContainerDic)
{
this.DownloadedSizeDic = downloadedSizeDic;
this.SpeedContainerDic = speedContainerDic;
}
public override IRenderable Render(RenderContext context, ProgressTask task, TimeSpan deltaTime)
{
if (task.Value == 0) return new Text("", MyStyle).RightAligned();
var done = task.IsFinished;
var flag = DownloadedSizeDic.TryGetValue(task.Id, out var size);
var totalSize = flag ? (size / (task.Value / task.MaxValue)) : 0;
var sizeStr = size == 0 ? "" : $"{GlobalUtil.FormatFileSize(flag ? size : 0)}/{GlobalUtil.FormatFileSize(totalSize)}";
if (done) sizeStr = GlobalUtil.FormatFileSize(totalSize);
return new Markup(sizeStr, MyStyle).RightAligned();
//单文件下载汇报进度
var speedContainer = SpeedContainerDic[task.Id];
if (!done && speedContainer.SingleSegment && speedContainer.ResponseLength != null)
{
task.MaxValue = (double)speedContainer.ResponseLength;
task.Value = speedContainer.RDownloaded;
size = speedContainer.RDownloaded;
totalSize = (double)speedContainer.ResponseLength;
}
var sizeStr = $"{GlobalUtil.FormatFileSize(flag ? size : 0)}/{GlobalUtil.FormatFileSize(totalSize)}";
if (done) sizeStr = GlobalUtil.FormatFileSize(totalSize);
return new Text(sizeStr, MyStyle).RightAligned();
}
}
}

View File

@ -643,9 +643,9 @@ namespace N_m3u8DL_RE.DownloadManager
progress.Columns(new ProgressColumn[]
{
new TaskDescriptionColumn() { Alignment = Justify.Left },
new ProgressBarColumn() { Width = 30 },
new ProgressBarColumn(),
new PercentageColumn(),
new DownloadStatusColumn(DownloadedSizeDic),
new DownloadStatusColumn(DownloadedSizeDic, SpeedContainerDic),
new DownloadSpeedColumn(SpeedContainerDic), //速度计算
new RemainingTimeColumn(),
new SpinnerColumn(),