diff --git a/internal/file/uploader/status.go b/internal/file/uploader/status.go index 4f3d121..b7aadb3 100644 --- a/internal/file/uploader/status.go +++ b/internal/file/uploader/status.go @@ -14,6 +14,7 @@ package uploader import ( + "sync/atomic" "time" ) @@ -24,6 +25,7 @@ type ( Uploaded() int64 // 已上传数据 SpeedsPerSecond() int64 // 每秒的上传速度 TimeElapsed() time.Duration // 上传时间 + TimeLeft() time.Duration // 预计剩余时间, 负数代表未知 } // UploadStatus 上传状态 @@ -67,6 +69,18 @@ func (us *UploadStatus) TimeElapsed() time.Duration { return us.timeElapsed } +// TimeLeft 返回预计剩余时间, 负数代表未知 +func (us *UploadStatus) TimeLeft() time.Duration { + var left time.Duration + speeds := atomic.LoadInt64(&us.speedsPerSecond) + if speeds <= 0 { + left = -1 + } else { + left = time.Duration((us.totalSize-us.uploaded)/(speeds)) * time.Second + } + return left +} + // GetStatusChan 获取上传状态 func (u *Uploader) GetStatusChan() <-chan Status { c := make(chan Status) diff --git a/internal/functions/panupload/upload_task_unit.go b/internal/functions/panupload/upload_task_unit.go index ab0d342..fed648f 100644 --- a/internal/functions/panupload/upload_task_unit.go +++ b/internal/functions/panupload/upload_task_unit.go @@ -183,14 +183,23 @@ func (utu *UploadTaskUnit) upload() (result *taskframework.TaskUnitRunResult) { } if utu.ShowProgress { + // 如果下载速度为0, 剩余下载时间未知, 则用 - 代替 + var leftStr string + left := status.TimeLeft() + if left < 0 { + leftStr = "-" + } else { + leftStr = left.String() + } uploadedPercentage := fmt.Sprintf("%.2f%%", float64(status.Uploaded())/float64(status.TotalSize())*100) - fmt.Printf("\r[%s] ↑ %s/%s(%s) %s/s(%s/s) in %s ............", utu.taskInfo.Id(), + fmt.Printf("\r[%s] ↑ %s/%s(%s) %s/s(%s/s) in %s, left %s ............", utu.taskInfo.Id(), converter.ConvertFileSize(status.Uploaded(), 2), converter.ConvertFileSize(status.TotalSize(), 2), uploadedPercentage, converter.ConvertFileSize(status.SpeedsPerSecond(), 2), converter.ConvertFileSize(utu.GlobalSpeedsStat.GetSpeeds(), 2), status.TimeElapsed(), + leftStr, ) } })